cli: Add license flag

This patch adds the option to print the license with a cli flag. It
uses go-bindata to store the license file. The file is generated by
running `make bindata` and the result is stored in the bindata
directory.
This commit is contained in:
Jonathan Gold
2017-12-08 00:01:38 -05:00
committed by James Shubin
parent 406aa55667
commit 62ca12608d
8 changed files with 76 additions and 11 deletions

1
.gitignore vendored
View File

@@ -6,6 +6,7 @@
old/
tmp/
*_stringer.go
bindata/*.go
mgmt
mgmt.static
mgmt.iml

View File

@@ -16,8 +16,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
SHELL = /usr/bin/env bash
.PHONY: all art cleanart version program path deps run race generate build clean test gofmt yamlfmt format docs rpmbuild mkdirs rpm srpm spec tar upload upload-sources upload-srpms upload-rpms copr
.SILENT: clean
.PHONY: all art cleanart version program path deps run race bindata generate build clean test gofmt yamlfmt format docs rpmbuild mkdirs rpm srpm spec tar upload upload-sources upload-srpms upload-rpms copr
.SILENT: clean bindata
GO_FILES := $(shell find . -name '*.go')
SVERSION := $(or $(SVERSION),$(shell git describe --match '[0-9]*\.[0-9]*\.[0-9]*' --tags --dirty --always))
@@ -101,10 +101,14 @@ run:
race:
find . -maxdepth 1 -type f -name '*.go' -not -name '*_test.go' | xargs go run -race -ldflags "-X main.program=$(PROGRAM) -X main.version=$(SVERSION)"
# generate go files from non-go source
bindata:
$(MAKE) --quiet -C bindata
generate:
go generate
build: $(PROGRAM)
build: bindata $(PROGRAM)
$(PROGRAM): $(GO_FILES)
@echo "Building: $(PROGRAM), version: $(SVERSION)..."
@@ -120,7 +124,7 @@ clean:
rm -f *_stringer.go # generated by `go generate`
rm -f *_mock.go # generated by `go generate`
test:
test: bindata
./test.sh
gofmt:

33
bindata/Makefile Normal file
View File

@@ -0,0 +1,33 @@
# Mgmt
# Copyright (C) 2013-2017+ James Shubin and the project contributors
# Written by James Shubin <james@shubin.ca> and the project contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# The bindata target generates go files from any source defined below. To use
# the files, import the "bindata" package and use:
# `bytes, err := bindata.Asset("FILEPATH")`
# where FILEPATH is the path of the original input file relative to `bindata/`.
.PHONY: build
default: build
build: bindata.go
# add more input files as dependencies at the end here...
bindata.go: ../COPYING
# go-bindata --pkg bindata -o {OUTPUT} {INPUT}
go-bindata --pkg bindata -o ./$@ $^
# gofmt the output file
gofmt -s -w $@

View File

@@ -24,6 +24,7 @@ import (
"os/signal"
"syscall"
"github.com/purpleidea/mgmt/bindata"
"github.com/purpleidea/mgmt/hcl"
"github.com/purpleidea/mgmt/puppet"
"github.com/purpleidea/mgmt/yamlgraph"
@@ -188,7 +189,32 @@ func CLI(program, version string, flags Flags) error {
app.Metadata = map[string]interface{}{ // additional flags
"flags": flags,
}
//app.Action = ... // without a default action, help runs
// if no app.Command is specified
app.Action = func(c *cli.Context) error {
// print the license
if c.Bool("license") {
license, err := bindata.Asset("../COPYING") // use go-bindata to get the bytes
if err != nil {
return err
}
fmt.Printf("%s", license)
return nil
}
// print help if no flags are set
cli.ShowAppHelp(c)
return nil
}
// global flags
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "license",
Usage: "prints the software license",
},
}
app.Commands = []cli.Command{
{

View File

@@ -79,6 +79,7 @@ if [[ $ret != 0 ]]; then
go get golang.org/x/tools/cmd/vet # add in `go vet` for travis
fi
go get golang.org/x/tools/cmd/stringer # for automatic stringer-ing
go get github.com/jteeuwen/go-bindata/go-bindata # for compiling in non golang files
go get github.com/golang/lint/golint # for `golint`-ing
go get -u gopkg.in/alecthomas/gometalinter.v1 && mv "$(dirname $(which gometalinter.v1))/gometalinter.v1" "$(dirname $(which gometalinter.v1))/gometalinter" && gometalinter --install # bonus
cd "$XPWD" >/dev/null

View File

@@ -24,7 +24,7 @@ if [ "$COMMITS" != "" ] && [ "$COMMITS" -gt "1" ]; then
HACK="yes"
fi
LINT=`find . -maxdepth 3 -iname '*.go' -not -path './old/*' -not -path './tmp/*' -exec golint {} \;` # current golint output
LINT=`find . -maxdepth 3 -iname '*.go' -not -path './old/*' -not -path './tmp/*' -not -path './bindata/*' -exec golint {} \;` # current golint output
COUNT=`echo -e "$LINT" | wc -l` # number of golint problems in current branch
[ "$LINT" = "" ] && echo PASS && exit # everything is "perfect"
echo "$LINT" # display the issues
@@ -51,7 +51,7 @@ while read -r line; do
done <<< "$NUMSTAT1" # three < is the secret to putting a variable into read
git checkout "$PREVIOUS" &>/dev/null # previous commit
LINT1=`find . -maxdepth 3 -iname '*.go' -not -path './old/*' -not -path './tmp/*' -exec golint {} \;`
LINT1=`find . -maxdepth 3 -iname '*.go' -not -path './old/*' -not -path './tmp/*' -not -path './bindata/*' -exec golint {} \;`
COUNT1=`echo -e "$LINT1" | wc -l` # number of golint problems in older branch
# clean up

View File

@@ -43,7 +43,7 @@ gometalinter="$gml"
# loop through directories in an attempt to scan each go package
# TODO: lint the *.go examples as individual files and not as a single *.go
for dir in `find . -maxdepth 5 -type d -not -path './old/*' -not -path './old' -not -path './tmp/*' -not -path './tmp' -not -path './.*' -not -path './vendor/*' -not -path './examples/*' -not -path './test/*'`; do
for dir in `find . -maxdepth 5 -type d -not -path './old/*' -not -path './old' -not -path './tmp/*' -not -path './tmp' -not -path './.*' -not -path './vendor/*' -not -path './bindata/*'-not -path ' ./examples/*' -not -path './test/*'`; do
match="$dir/*.go"
#echo "match is: $match"
if ! ls $match &>/dev/null; then

View File

@@ -14,7 +14,7 @@ done < "$FILE"
cd "${ROOT}"
find_files() {
git ls-files | grep '\.go$' | grep -v '^examples/' | grep -v '^test/'
git ls-files | grep '\.go$' | grep -v '^bindata/' | grep -v '^examples/' | grep -v '^test/'
}
bad_files=$(