diff --git a/cli/cli.go b/cli/cli.go index 814bca0e..9773dd31 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -26,6 +26,7 @@ import ( "os" "sort" + cliUtil "github.com/purpleidea/mgmt/cli/util" "github.com/purpleidea/mgmt/gapi" _ "github.com/purpleidea/mgmt/lang" // import so the GAPI registers _ "github.com/purpleidea/mgmt/langpuppet" @@ -35,18 +36,12 @@ import ( "github.com/urfave/cli/v2" ) -// Flags are some constant flags which are used throughout the program. -type Flags struct { - Debug bool // add additional log messages - Verbose bool // add extra log message output -} - // CLIArgs is a struct of values that we pass to the main CLI function. type CLIArgs struct { Program string Version string Copying string - Flags Flags + Flags cliUtil.Flags } // CLI is the entry point for using mgmt normally from the CLI. diff --git a/cli/deploy.go b/cli/deploy.go index c86faf68..cbf66524 100644 --- a/cli/deploy.go +++ b/cli/deploy.go @@ -23,6 +23,7 @@ import ( "log" "os" + cliUtil "github.com/purpleidea/mgmt/cli/util" "github.com/purpleidea/mgmt/etcd/client" "github.com/purpleidea/mgmt/etcd/deployer" etcdfs "github.com/purpleidea/mgmt/etcd/fs" @@ -43,11 +44,11 @@ func deploy(c *cli.Context, name string, gapiObj gapi.GAPI) error { return fmt.Errorf("could not get cli context") } - program, version := safeProgram(c.App.Name), c.App.Version - var flags Flags + program, version := cliUtil.SafeProgram(c.App.Name), c.App.Version + var flags cliUtil.Flags var debug bool if val, exists := c.App.Metadata["flags"]; exists { - if f, ok := val.(Flags); ok { + if f, ok := val.(cliUtil.Flags); ok { flags = f debug = flags.Debug } @@ -56,7 +57,7 @@ func deploy(c *cli.Context, name string, gapiObj gapi.GAPI) error { log.Printf("deploy: "+format, v...) } - hello(program, version, flags) // say hello! + cliUtil.Hello(program, version, flags) // say hello! defer Logf("goodbye!") var hash, pHash string diff --git a/cli/get.go b/cli/get.go index 5d2c9405..f9eea399 100644 --- a/cli/get.go +++ b/cli/get.go @@ -21,6 +21,7 @@ import ( "fmt" "log" + cliUtil "github.com/purpleidea/mgmt/cli/util" "github.com/purpleidea/mgmt/gapi" "github.com/urfave/cli/v2" @@ -33,16 +34,16 @@ func get(c *cli.Context, name string, gapiObj gapi.GAPI) error { return fmt.Errorf("could not get cli context") } - program, version := safeProgram(c.App.Name), c.App.Version - var flags Flags + program, version := cliUtil.SafeProgram(c.App.Name), c.App.Version + var flags cliUtil.Flags var debug bool if val, exists := c.App.Metadata["flags"]; exists { - if f, ok := val.(Flags); ok { + if f, ok := val.(cliUtil.Flags); ok { flags = f debug = flags.Debug } } - hello(program, version, flags) // say hello! + cliUtil.Hello(program, version, flags) // say hello! gettable, ok := gapiObj.(gapi.GettableGAPI) if !ok { diff --git a/cli/run.go b/cli/run.go index 7dc0e156..3caf0d26 100644 --- a/cli/run.go +++ b/cli/run.go @@ -25,6 +25,7 @@ import ( "sync" "syscall" + cliUtil "github.com/purpleidea/mgmt/cli/util" "github.com/purpleidea/mgmt/gapi" "github.com/purpleidea/mgmt/lib" "github.com/purpleidea/mgmt/util" @@ -43,10 +44,10 @@ func run(c *cli.Context, name string, gapiObj gapi.GAPI) error { main := &lib.Main{} - main.Program, main.Version = safeProgram(c.App.Name), c.App.Version - var flags Flags + main.Program, main.Version = cliUtil.SafeProgram(c.App.Name), c.App.Version + var flags cliUtil.Flags if val, exists := c.App.Metadata["flags"]; exists { - if f, ok := val.(Flags); ok { + if f, ok := val.(cliUtil.Flags); ok { flags = f main.Flags = lib.Flags{ Debug: f.Debug, @@ -58,7 +59,7 @@ func run(c *cli.Context, name string, gapiObj gapi.GAPI) error { log.Printf("main: "+format, v...) } - hello(main.Program, main.Version, flags) // say hello! + cliUtil.Hello(main.Program, main.Version, flags) // say hello! defer Logf("goodbye!") if h := cliContext.String("hostname"); cliContext.IsSet("hostname") && h != "" { diff --git a/cli/hello.go b/cli/util/hello.go similarity index 91% rename from cli/hello.go rename to cli/util/hello.go index ff1f6f1d..2876670a 100644 --- a/cli/hello.go +++ b/cli/util/hello.go @@ -15,7 +15,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -package cli +package util import ( "fmt" @@ -24,7 +24,8 @@ import ( "time" ) -func hello(program, version string, flags Flags) { +// Hello is a simple helper function to print a hello message and time. +func Hello(program, version string, flags Flags) { var start = time.Now().UnixNano() logFlags := log.LstdFlags diff --git a/cli/util.go b/cli/util/util.go similarity index 76% rename from cli/util.go rename to cli/util/util.go index 7289c848..b8d1afcd 100644 --- a/cli/util.go +++ b/cli/util/util.go @@ -15,14 +15,21 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -package cli +// Package util has some CLI related utility code. +package util import ( "strings" ) -// safeProgram returns the correct program string when given a buggy variant. -func safeProgram(program string) string { +// Flags are some constant flags which are used throughout the program. +type Flags struct { + Debug bool // add additional log messages + Verbose bool // add extra log message output +} + +// SafeProgram returns the correct program string when given a buggy variant. +func SafeProgram(program string) string { // FIXME: in sub commands, the cli package appends a space and the sub // command name at the end. hack around this by only using the first bit // see: https://github.com/urfave/cli/issues/783 for more details... diff --git a/main.go b/main.go index d4ae1339..943d34c0 100644 --- a/main.go +++ b/main.go @@ -23,6 +23,7 @@ import ( "os" "github.com/purpleidea/mgmt/cli" + cliUtil "github.com/purpleidea/mgmt/cli/util" "go.etcd.io/etcd/server/v3/etcdmain" ) @@ -59,7 +60,7 @@ func main() { Program: program, Version: version, Copying: copying, - Flags: cli.Flags{ + Flags: cliUtil.Flags{ Debug: Debug, Verbose: Verbose, }, diff --git a/test/test-govet.sh b/test/test-govet.sh index c792eb5a..cc878153 100755 --- a/test/test-govet.sh +++ b/test/test-govet.sh @@ -95,6 +95,10 @@ function consistent-imports() { if grep $'\t"github.com/purpleidea/mgmt/engine/util"' "$1"; then return 1 fi + # import as cliUtil + if grep $'\t"github.com/purpleidea/mgmt/cli/util"' "$1"; then + return 1 + fi if grep '"golang.org/x/net/context"' "$1"; then # use built-in context return 1 fi