2018-09-05 17:32:38 +02:00
|
|
|
package client
|
|
|
|
|
2018-10-13 15:07:50 +02:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
2018-10-18 16:35:29 +02:00
|
|
|
"fmt"
|
2018-10-13 15:07:50 +02:00
|
|
|
"github.com/kr/pretty"
|
2018-10-18 16:35:29 +02:00
|
|
|
"github.com/pkg/errors"
|
2018-10-13 15:07:50 +02:00
|
|
|
"github.com/spf13/pflag"
|
|
|
|
"github.com/zrepl/yaml-config"
|
|
|
|
"github.com/zrepl/zrepl/cli"
|
2018-10-18 16:35:29 +02:00
|
|
|
"github.com/zrepl/zrepl/config"
|
|
|
|
"github.com/zrepl/zrepl/daemon/job"
|
2018-10-19 17:15:24 +02:00
|
|
|
"github.com/zrepl/zrepl/daemon/logging"
|
|
|
|
"github.com/zrepl/zrepl/logger"
|
2018-10-13 15:07:50 +02:00
|
|
|
"os"
|
|
|
|
)
|
2018-09-05 17:32:38 +02:00
|
|
|
|
2018-10-13 15:07:50 +02:00
|
|
|
var configcheckArgs struct {
|
|
|
|
format string
|
2018-10-18 16:35:29 +02:00
|
|
|
what string
|
2018-09-05 17:32:38 +02:00
|
|
|
}
|
2018-10-13 15:07:50 +02:00
|
|
|
|
|
|
|
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]")
|
2018-10-19 17:15:24 +02:00
|
|
|
f.StringVar(&configcheckArgs.what, "what", "all", "what to print [all|config|jobs|logging]")
|
2018-10-13 15:07:50 +02:00
|
|
|
},
|
|
|
|
Run: func(subcommand *cli.Subcommand, args []string) error {
|
2018-10-18 16:35:29 +02:00
|
|
|
formatMap := map[string]func(interface{}) {
|
|
|
|
"": func(i interface{}) {},
|
|
|
|
"pretty": func(i interface{}) { pretty.Println(i) },
|
|
|
|
"json": func(i interface{}) {
|
|
|
|
json.NewEncoder(os.Stdout).Encode(subcommand.Config())
|
|
|
|
},
|
|
|
|
"yaml": func(i interface{}) {
|
|
|
|
yaml.NewEncoder(os.Stdout).Encode(subcommand.Config())
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-19 17:15:24 +02:00
|
|
|
// further: try to build logging outlets
|
|
|
|
outlets, err := logging.OutletsFromConfig(*subcommand.Config().Global.Logging)
|
|
|
|
if err != nil {
|
|
|
|
err := errors.Wrap(err, "cannot build logging from config")
|
|
|
|
if configcheckArgs.what == "logging" {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err)
|
|
|
|
outlets = nil
|
|
|
|
hadErr = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-18 16:35:29 +02:00
|
|
|
whatMap := map[string]func() {
|
|
|
|
"all": func() {
|
|
|
|
o := struct {
|
|
|
|
config *config.Config
|
|
|
|
jobs []job.Job
|
2018-10-19 17:15:24 +02:00
|
|
|
logging *logger.Outlets
|
2018-10-18 16:35:29 +02:00
|
|
|
}{
|
|
|
|
subcommand.Config(),
|
|
|
|
confJobs,
|
2018-10-19 17:15:24 +02:00
|
|
|
outlets,
|
2018-10-18 16:35:29 +02:00
|
|
|
}
|
|
|
|
formatter(o)
|
|
|
|
},
|
|
|
|
"config": func() {
|
|
|
|
formatter(subcommand.Config())
|
|
|
|
},
|
|
|
|
"jobs": func() {
|
|
|
|
formatter(confJobs)
|
|
|
|
},
|
2018-10-19 17:15:24 +02:00
|
|
|
"logging": func() {
|
|
|
|
formatter(outlets)
|
|
|
|
},
|
2018-10-18 16:35:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2018-10-13 15:07:50 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|