mirror of
https://github.com/zrepl/zrepl.git
synced 2025-06-19 17:27:46 +02:00
client/configcheck: build jobs for checking config and allow selecting what to print
This commit is contained in:
parent
a5376913fd
commit
53ac853cb4
@ -2,15 +2,20 @@ package client
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"github.com/kr/pretty"
|
"github.com/kr/pretty"
|
||||||
|
"github.com/pkg/errors"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
"github.com/zrepl/yaml-config"
|
"github.com/zrepl/yaml-config"
|
||||||
"github.com/zrepl/zrepl/cli"
|
"github.com/zrepl/zrepl/cli"
|
||||||
|
"github.com/zrepl/zrepl/config"
|
||||||
|
"github.com/zrepl/zrepl/daemon/job"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
var configcheckArgs struct {
|
var configcheckArgs struct {
|
||||||
format string
|
format string
|
||||||
|
what string
|
||||||
}
|
}
|
||||||
|
|
||||||
var ConfigcheckCmd = &cli.Subcommand{
|
var ConfigcheckCmd = &cli.Subcommand{
|
||||||
@ -18,19 +23,69 @@ var ConfigcheckCmd = &cli.Subcommand{
|
|||||||
Short: "check if config can be parsed without errors",
|
Short: "check if config can be parsed without errors",
|
||||||
SetupFlags: func(f *pflag.FlagSet) {
|
SetupFlags: func(f *pflag.FlagSet) {
|
||||||
f.StringVar(&configcheckArgs.format, "format", "", "dump parsed config object [pretty|yaml|json]")
|
f.StringVar(&configcheckArgs.format, "format", "", "dump parsed config object [pretty|yaml|json]")
|
||||||
|
f.StringVar(&configcheckArgs.what, "what", "all", "what to print [all|config|jobs]")
|
||||||
},
|
},
|
||||||
Run: func(subcommand *cli.Subcommand, args []string) error {
|
Run: func(subcommand *cli.Subcommand, args []string) error {
|
||||||
switch configcheckArgs.format {
|
formatMap := map[string]func(interface{}) {
|
||||||
case "pretty":
|
"": func(i interface{}) {},
|
||||||
_, err := pretty.Println(subcommand.Config())
|
"pretty": func(i interface{}) { pretty.Println(i) },
|
||||||
return err
|
"json": func(i interface{}) {
|
||||||
case "json":
|
json.NewEncoder(os.Stdout).Encode(subcommand.Config())
|
||||||
return json.NewEncoder(os.Stdout).Encode(subcommand.Config())
|
},
|
||||||
case "yaml":
|
"yaml": func(i interface{}) {
|
||||||
return yaml.NewEncoder(os.Stdout).Encode(subcommand.Config())
|
yaml.NewEncoder(os.Stdout).Encode(subcommand.Config())
|
||||||
default: // no output
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
formatter, ok := formatMap[configcheckArgs.format]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("unsupported --format %q", configcheckArgs.format)
|
||||||
|
}
|
||||||
|
|
||||||
|
var hadErr bool
|
||||||
|
// further: try to build jobs
|
||||||
|
confJobs, err := job.JobsFromConfig(subcommand.Config())
|
||||||
|
if err != nil {
|
||||||
|
err := errors.Wrap(err, "cannot build jobs from config")
|
||||||
|
if configcheckArgs.what == "jobs" {
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(os.Stderr, "%s\n", err)
|
||||||
|
confJobs = nil
|
||||||
|
hadErr = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
whatMap := map[string]func() {
|
||||||
|
"all": func() {
|
||||||
|
o := struct {
|
||||||
|
config *config.Config
|
||||||
|
jobs []job.Job
|
||||||
|
}{
|
||||||
|
subcommand.Config(),
|
||||||
|
confJobs,
|
||||||
|
}
|
||||||
|
formatter(o)
|
||||||
|
},
|
||||||
|
"config": func() {
|
||||||
|
formatter(subcommand.Config())
|
||||||
|
},
|
||||||
|
"jobs": func() {
|
||||||
|
formatter(confJobs)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
wf, ok := whatMap[configcheckArgs.what]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("unsupported --format %q", configcheckArgs.what)
|
||||||
|
}
|
||||||
|
wf()
|
||||||
|
|
||||||
|
if hadErr {
|
||||||
|
return fmt.Errorf("config parsing failed")
|
||||||
|
} else {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user