cli, lib, lang: Port to new cli library
The new version of the urfave/cli library is moving to generics, and it's completely unclear to me why this is an improvement. Their new API is very complicated to understand, which for me, defeats the purpose of golang. In parallel, I needed to do some upcoming cli API refactoring, so this was a good time to look into new libraries. After a review of the landscape, I found the alexflint/go-arg library which has a delightfully elegant API. It does have a few rough edges, but it's otherwise very usable, and I think it would be straightforward to add features and fix issues. Thanks Alex!
This commit is contained in:
@@ -26,13 +26,12 @@ import (
|
||||
"github.com/purpleidea/mgmt/gapi"
|
||||
"github.com/purpleidea/mgmt/pgraph"
|
||||
"github.com/purpleidea/mgmt/util/errwrap"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
// Name is the name of this frontend.
|
||||
Name = "yaml"
|
||||
|
||||
// Start is the entry point filename that we use. It is arbitrary.
|
||||
Start = "/start.yaml"
|
||||
)
|
||||
@@ -41,6 +40,12 @@ func init() {
|
||||
gapi.Register(Name, func() gapi.GAPI { return &GAPI{} }) // register
|
||||
}
|
||||
|
||||
// Args is the CLI parsing structure and type of the parsed result.
|
||||
type Args struct {
|
||||
// Input is the input yaml code or file path or any input specification.
|
||||
Input string `arg:"positional,required"`
|
||||
}
|
||||
|
||||
// GAPI implements the main yamlgraph GAPI interface.
|
||||
type GAPI struct {
|
||||
InputURI string // input URI of file system containing yaml graph to use
|
||||
@@ -51,56 +56,36 @@ type GAPI struct {
|
||||
wg sync.WaitGroup // sync group for tunnel go routines
|
||||
}
|
||||
|
||||
// CliFlags returns a list of flags used by the specified subcommand.
|
||||
func (obj *GAPI) CliFlags(command string) []cli.Flag {
|
||||
switch command {
|
||||
case gapi.CommandRun:
|
||||
fallthrough
|
||||
case gapi.CommandDeploy:
|
||||
return []cli.Flag{}
|
||||
//case gapi.CommandGet:
|
||||
default:
|
||||
return []cli.Flag{}
|
||||
// Cli takes an *Info struct, and returns our deploy if activated, and if there
|
||||
// are any validation problems, you should return an error. If there is no
|
||||
// deploy, then you should return a nil deploy and a nil error.
|
||||
func (obj *GAPI) Cli(info *gapi.Info) (*gapi.Deploy, error) {
|
||||
args, ok := info.Args.(*Args)
|
||||
if !ok {
|
||||
// programming error
|
||||
return nil, fmt.Errorf("could not convert to our struct")
|
||||
}
|
||||
}
|
||||
|
||||
// Cli takes a cli.Context, and returns our GAPI if activated. All arguments
|
||||
// should take the prefix of the registered name. On activation, if there are
|
||||
// any validation problems, you should return an error. If this was not
|
||||
// activated, then you should return a nil GAPI and a nil error.
|
||||
func (obj *GAPI) Cli(cliInfo *gapi.CliInfo) (*gapi.Deploy, error) {
|
||||
c := cliInfo.CliContext
|
||||
fs := cliInfo.Fs
|
||||
//debug := cliInfo.Debug
|
||||
fs := info.Fs
|
||||
//debug := info.Debug
|
||||
//logf := func(format string, v ...interface{}) {
|
||||
// cliInfo.Logf(Name + ": "+format, v...)
|
||||
// info.Logf(Name + ": "+format, v...)
|
||||
//}
|
||||
|
||||
if l := c.NArg(); l != 1 {
|
||||
if l > 1 {
|
||||
return nil, fmt.Errorf("input program must be a single arg")
|
||||
}
|
||||
return nil, fmt.Errorf("must specify input program")
|
||||
}
|
||||
s := c.Args().Get(0)
|
||||
if s == "" {
|
||||
return nil, fmt.Errorf("input yaml is empty")
|
||||
}
|
||||
|
||||
writeableFS, ok := fs.(engine.WriteableFS)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("the FS was not writeable")
|
||||
}
|
||||
|
||||
// single file input only
|
||||
if err := gapi.CopyFileToFs(writeableFS, s, Start); err != nil {
|
||||
return nil, errwrap.Wrapf(err, "can't copy yaml from `%s` to `%s`", s, Start)
|
||||
if err := gapi.CopyFileToFs(writeableFS, args.Input, Start); err != nil {
|
||||
return nil, errwrap.Wrapf(err, "can't copy yaml from `%s` to `%s`", args.Input, Start)
|
||||
}
|
||||
|
||||
return &gapi.Deploy{
|
||||
Name: Name,
|
||||
Noop: c.Bool("noop"),
|
||||
Sema: c.Int("sema"),
|
||||
Noop: info.Flags.Noop,
|
||||
Sema: info.Flags.Sema,
|
||||
GAPI: &GAPI{
|
||||
InputURI: fs.URI(),
|
||||
// TODO: add properties here...
|
||||
|
||||
Reference in New Issue
Block a user