cli: Further refactor our input cli data
This commit is contained in:
27
cli/cli.go
27
cli/cli.go
@@ -23,7 +23,6 @@ package cli
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
cliUtil "github.com/purpleidea/mgmt/cli/util"
|
cliUtil "github.com/purpleidea/mgmt/cli/util"
|
||||||
@@ -36,24 +35,16 @@ import (
|
|||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CLIArgs is a struct of values that we pass to the main CLI function.
|
|
||||||
type CLIArgs struct {
|
|
||||||
Program string
|
|
||||||
Version string
|
|
||||||
Copying string
|
|
||||||
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.
|
||||||
func CLI(cliArgs *CLIArgs) error {
|
func CLI(data *cliUtil.Data) error {
|
||||||
// test for sanity
|
// test for sanity
|
||||||
if cliArgs == nil {
|
if data == nil {
|
||||||
return fmt.Errorf("this CLI was not run correctly")
|
return fmt.Errorf("this CLI was not run correctly")
|
||||||
}
|
}
|
||||||
if cliArgs.Program == "" || cliArgs.Version == "" {
|
if data.Program == "" || data.Version == "" {
|
||||||
return fmt.Errorf("program was not compiled correctly, see Makefile")
|
return fmt.Errorf("program was not compiled correctly, see Makefile")
|
||||||
}
|
}
|
||||||
if cliArgs.Copying == "" {
|
if data.Copying == "" {
|
||||||
return fmt.Errorf("program copyrights we're removed, can't run")
|
return fmt.Errorf("program copyrights we're removed, can't run")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -319,18 +310,18 @@ func CLI(cliArgs *CLIArgs) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
app := cli.NewApp()
|
app := cli.NewApp()
|
||||||
app.Name = cliArgs.Program // App.name and App.version pass these values through
|
app.Name = data.Program // App.name and App.version pass these values through
|
||||||
app.Version = cliArgs.Version
|
app.Version = data.Version
|
||||||
app.Usage = "next generation config management"
|
app.Usage = "next generation config management"
|
||||||
app.Metadata = map[string]interface{}{ // additional flags
|
app.Metadata = map[string]interface{}{ // additional flags
|
||||||
"flags": cliArgs.Flags,
|
"flags": data.Flags,
|
||||||
}
|
}
|
||||||
|
|
||||||
// if no app.Command is specified
|
// if no app.Command is specified
|
||||||
app.Action = func(c *cli.Context) error {
|
app.Action = func(c *cli.Context) error {
|
||||||
// print the license
|
// print the license
|
||||||
if c.Bool("license") {
|
if c.Bool("license") {
|
||||||
fmt.Printf("%s", cliArgs.Copying)
|
fmt.Printf("%s", data.Copying)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -406,5 +397,5 @@ func CLI(cliArgs *CLIArgs) error {
|
|||||||
app.Commands = append(app.Commands, commandEtcd)
|
app.Commands = append(app.Commands, commandEtcd)
|
||||||
|
|
||||||
app.EnableBashCompletion = true
|
app.EnableBashCompletion = true
|
||||||
return app.Run(os.Args)
|
return app.Run(data.Args)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,15 @@ type Flags struct {
|
|||||||
Verbose bool // add extra log message output
|
Verbose bool // add extra log message output
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Data is a struct of values that we usually pass to the main CLI function.
|
||||||
|
type Data struct {
|
||||||
|
Program string
|
||||||
|
Version string
|
||||||
|
Copying string
|
||||||
|
Flags Flags
|
||||||
|
Args []string // os.Args usually
|
||||||
|
}
|
||||||
|
|
||||||
// SafeProgram returns the correct program string when given a buggy variant.
|
// SafeProgram returns the correct program string when given a buggy variant.
|
||||||
func SafeProgram(program string) string {
|
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
|
||||||
|
|||||||
5
main.go
5
main.go
@@ -56,7 +56,7 @@ func main() {
|
|||||||
return // for safety
|
return // for safety
|
||||||
}
|
}
|
||||||
|
|
||||||
cliArgs := &cli.CLIArgs{
|
data := &cliUtil.Data{
|
||||||
Program: program,
|
Program: program,
|
||||||
Version: version,
|
Version: version,
|
||||||
Copying: copying,
|
Copying: copying,
|
||||||
@@ -64,8 +64,9 @@ func main() {
|
|||||||
Debug: Debug,
|
Debug: Debug,
|
||||||
Verbose: Verbose,
|
Verbose: Verbose,
|
||||||
},
|
},
|
||||||
|
Args: os.Args,
|
||||||
}
|
}
|
||||||
if err := cli.CLI(cliArgs); err != nil {
|
if err := cli.CLI(data); err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user