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:
James Shubin
2024-02-25 19:03:14 -05:00
parent c37ff3efce
commit 71c54ab212
8 changed files with 36 additions and 25 deletions

View File

@@ -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.

View File

@@ -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

View File

@@ -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 {

View File

@@ -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 != "" {

View File

@@ -15,7 +15,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
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

View File

@@ -15,14 +15,21 @@
// You should have received a copy of the GNU General Public License
// 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 (
"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...