cli: Refactor even more code out of cli package
Quite honestly, I'm trying to clean things up, and removing anything non-consequential will help!
This commit is contained in:
@@ -26,6 +26,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
|
cliUtil "github.com/purpleidea/mgmt/cli/util"
|
||||||
"github.com/purpleidea/mgmt/gapi"
|
"github.com/purpleidea/mgmt/gapi"
|
||||||
_ "github.com/purpleidea/mgmt/lang" // import so the GAPI registers
|
_ "github.com/purpleidea/mgmt/lang" // import so the GAPI registers
|
||||||
_ "github.com/purpleidea/mgmt/langpuppet"
|
_ "github.com/purpleidea/mgmt/langpuppet"
|
||||||
@@ -35,18 +36,12 @@ import (
|
|||||||
"github.com/urfave/cli/v2"
|
"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.
|
// CLIArgs is a struct of values that we pass to the main CLI function.
|
||||||
type CLIArgs struct {
|
type CLIArgs struct {
|
||||||
Program string
|
Program string
|
||||||
Version string
|
Version string
|
||||||
Copying string
|
Copying string
|
||||||
Flags Flags
|
Flags cliUtil.Flags
|
||||||
}
|
}
|
||||||
|
|
||||||
// CLI is the entry point for using mgmt normally from the CLI.
|
// CLI is the entry point for using mgmt normally from the CLI.
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
cliUtil "github.com/purpleidea/mgmt/cli/util"
|
||||||
"github.com/purpleidea/mgmt/etcd/client"
|
"github.com/purpleidea/mgmt/etcd/client"
|
||||||
"github.com/purpleidea/mgmt/etcd/deployer"
|
"github.com/purpleidea/mgmt/etcd/deployer"
|
||||||
etcdfs "github.com/purpleidea/mgmt/etcd/fs"
|
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")
|
return fmt.Errorf("could not get cli context")
|
||||||
}
|
}
|
||||||
|
|
||||||
program, version := safeProgram(c.App.Name), c.App.Version
|
program, version := cliUtil.SafeProgram(c.App.Name), c.App.Version
|
||||||
var flags Flags
|
var flags cliUtil.Flags
|
||||||
var debug bool
|
var debug bool
|
||||||
if val, exists := c.App.Metadata["flags"]; exists {
|
if val, exists := c.App.Metadata["flags"]; exists {
|
||||||
if f, ok := val.(Flags); ok {
|
if f, ok := val.(cliUtil.Flags); ok {
|
||||||
flags = f
|
flags = f
|
||||||
debug = flags.Debug
|
debug = flags.Debug
|
||||||
}
|
}
|
||||||
@@ -56,7 +57,7 @@ func deploy(c *cli.Context, name string, gapiObj gapi.GAPI) error {
|
|||||||
log.Printf("deploy: "+format, v...)
|
log.Printf("deploy: "+format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
hello(program, version, flags) // say hello!
|
cliUtil.Hello(program, version, flags) // say hello!
|
||||||
defer Logf("goodbye!")
|
defer Logf("goodbye!")
|
||||||
|
|
||||||
var hash, pHash string
|
var hash, pHash string
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
|
cliUtil "github.com/purpleidea/mgmt/cli/util"
|
||||||
"github.com/purpleidea/mgmt/gapi"
|
"github.com/purpleidea/mgmt/gapi"
|
||||||
|
|
||||||
"github.com/urfave/cli/v2"
|
"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")
|
return fmt.Errorf("could not get cli context")
|
||||||
}
|
}
|
||||||
|
|
||||||
program, version := safeProgram(c.App.Name), c.App.Version
|
program, version := cliUtil.SafeProgram(c.App.Name), c.App.Version
|
||||||
var flags Flags
|
var flags cliUtil.Flags
|
||||||
var debug bool
|
var debug bool
|
||||||
if val, exists := c.App.Metadata["flags"]; exists {
|
if val, exists := c.App.Metadata["flags"]; exists {
|
||||||
if f, ok := val.(Flags); ok {
|
if f, ok := val.(cliUtil.Flags); ok {
|
||||||
flags = f
|
flags = f
|
||||||
debug = flags.Debug
|
debug = flags.Debug
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
hello(program, version, flags) // say hello!
|
cliUtil.Hello(program, version, flags) // say hello!
|
||||||
|
|
||||||
gettable, ok := gapiObj.(gapi.GettableGAPI)
|
gettable, ok := gapiObj.(gapi.GettableGAPI)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
cliUtil "github.com/purpleidea/mgmt/cli/util"
|
||||||
"github.com/purpleidea/mgmt/gapi"
|
"github.com/purpleidea/mgmt/gapi"
|
||||||
"github.com/purpleidea/mgmt/lib"
|
"github.com/purpleidea/mgmt/lib"
|
||||||
"github.com/purpleidea/mgmt/util"
|
"github.com/purpleidea/mgmt/util"
|
||||||
@@ -43,10 +44,10 @@ func run(c *cli.Context, name string, gapiObj gapi.GAPI) error {
|
|||||||
|
|
||||||
main := &lib.Main{}
|
main := &lib.Main{}
|
||||||
|
|
||||||
main.Program, main.Version = safeProgram(c.App.Name), c.App.Version
|
main.Program, main.Version = cliUtil.SafeProgram(c.App.Name), c.App.Version
|
||||||
var flags Flags
|
var flags cliUtil.Flags
|
||||||
if val, exists := c.App.Metadata["flags"]; exists {
|
if val, exists := c.App.Metadata["flags"]; exists {
|
||||||
if f, ok := val.(Flags); ok {
|
if f, ok := val.(cliUtil.Flags); ok {
|
||||||
flags = f
|
flags = f
|
||||||
main.Flags = lib.Flags{
|
main.Flags = lib.Flags{
|
||||||
Debug: f.Debug,
|
Debug: f.Debug,
|
||||||
@@ -58,7 +59,7 @@ func run(c *cli.Context, name string, gapiObj gapi.GAPI) error {
|
|||||||
log.Printf("main: "+format, v...)
|
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!")
|
defer Logf("goodbye!")
|
||||||
|
|
||||||
if h := cliContext.String("hostname"); cliContext.IsSet("hostname") && h != "" {
|
if h := cliContext.String("hostname"); cliContext.IsSet("hostname") && h != "" {
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package cli
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -24,7 +24,8 @@ import (
|
|||||||
"time"
|
"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()
|
var start = time.Now().UnixNano()
|
||||||
|
|
||||||
logFlags := log.LstdFlags
|
logFlags := log.LstdFlags
|
||||||
@@ -15,14 +15,21 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package cli
|
// Package util has some CLI related utility code.
|
||||||
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// safeProgram returns the correct program string when given a buggy variant.
|
// Flags are some constant flags which are used throughout the program.
|
||||||
func safeProgram(program string) string {
|
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
|
// 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
|
// 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...
|
// see: https://github.com/urfave/cli/issues/783 for more details...
|
||||||
3
main.go
3
main.go
@@ -23,6 +23,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/purpleidea/mgmt/cli"
|
"github.com/purpleidea/mgmt/cli"
|
||||||
|
cliUtil "github.com/purpleidea/mgmt/cli/util"
|
||||||
"go.etcd.io/etcd/server/v3/etcdmain"
|
"go.etcd.io/etcd/server/v3/etcdmain"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -59,7 +60,7 @@ func main() {
|
|||||||
Program: program,
|
Program: program,
|
||||||
Version: version,
|
Version: version,
|
||||||
Copying: copying,
|
Copying: copying,
|
||||||
Flags: cli.Flags{
|
Flags: cliUtil.Flags{
|
||||||
Debug: Debug,
|
Debug: Debug,
|
||||||
Verbose: Verbose,
|
Verbose: Verbose,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -95,6 +95,10 @@ function consistent-imports() {
|
|||||||
if grep $'\t"github.com/purpleidea/mgmt/engine/util"' "$1"; then
|
if grep $'\t"github.com/purpleidea/mgmt/engine/util"' "$1"; then
|
||||||
return 1
|
return 1
|
||||||
fi
|
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
|
if grep '"golang.org/x/net/context"' "$1"; then # use built-in context
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user