cli: refactor to allow definition of subcommands next to their implementation

This commit is contained in:
Christian Schwarz
2018-10-13 15:07:50 +02:00
parent aeb87ffbcf
commit 5c3c83b2cb
9 changed files with 255 additions and 191 deletions
+32 -4
View File
@@ -1,8 +1,36 @@
package client
import "github.com/zrepl/zrepl/config"
import (
"encoding/json"
"github.com/kr/pretty"
"github.com/spf13/pflag"
"github.com/zrepl/yaml-config"
"github.com/zrepl/zrepl/cli"
"os"
)
func RunConfigcheck(conf *config.Config, args []string) error {
// TODO: do the 'build' steps, e.g. build the jobs and see if that fails
return nil
var configcheckArgs struct {
format string
}
var ConfigcheckCmd = &cli.Subcommand{
Use: "configcheck",
Short: "check if config can be parsed without errors",
SetupFlags: func(f *pflag.FlagSet) {
f.StringVar(&configcheckArgs.format, "format", "", "dump parsed config object [pretty|yaml|json]")
},
Run: func(subcommand *cli.Subcommand, args []string) error {
switch configcheckArgs.format {
case "pretty":
_, err := pretty.Println(subcommand.Config())
return err
case "json":
return json.NewEncoder(os.Stdout).Encode(subcommand.Config())
case "yaml":
return yaml.NewEncoder(os.Stdout).Encode(subcommand.Config())
default: // no output
}
return nil
},
}