2018-10-13 15:07:50 +02:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2020-04-11 15:49:41 +02:00
|
|
|
"context"
|
2018-10-13 15:07:50 +02:00
|
|
|
"fmt"
|
2019-03-22 19:41:12 +01:00
|
|
|
"os"
|
|
|
|
|
2018-10-13 15:07:50 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/pflag"
|
2020-04-11 15:49:41 +02:00
|
|
|
"github.com/zrepl/zrepl/daemon/logging/trace"
|
2019-03-22 19:41:12 +01:00
|
|
|
|
2018-10-13 15:07:50 +02:00
|
|
|
"github.com/zrepl/zrepl/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
var rootArgs struct {
|
|
|
|
configPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "zrepl",
|
|
|
|
Short: "One-stop ZFS replication solution",
|
|
|
|
}
|
|
|
|
|
2020-04-18 19:09:49 +02:00
|
|
|
func init() {
|
|
|
|
rootCmd.PersistentFlags().StringVar(&rootArgs.configPath, "config", "", "config file path")
|
|
|
|
}
|
|
|
|
|
|
|
|
var genCompletionCmd = &cobra.Command{
|
|
|
|
Use: "gencompletion",
|
|
|
|
Short: "generate shell auto-completions",
|
|
|
|
}
|
|
|
|
|
|
|
|
type completionCmdInfo struct {
|
|
|
|
genFunc func(outpath string) error
|
|
|
|
help string
|
|
|
|
}
|
|
|
|
|
|
|
|
var completionCmdMap = map[string]completionCmdInfo{
|
|
|
|
"zsh": {
|
|
|
|
rootCmd.GenZshCompletionFile,
|
|
|
|
" save to file `_zrepl` in your zsh's $fpath",
|
|
|
|
},
|
|
|
|
"bash": {
|
|
|
|
rootCmd.GenBashCompletionFile,
|
|
|
|
" save to a path and source that path in your .bashrc",
|
2018-10-13 15:07:50 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2020-04-18 19:09:49 +02:00
|
|
|
for sh, info := range completionCmdMap {
|
|
|
|
sh, info := sh, info
|
|
|
|
genCompletionCmd.AddCommand(&cobra.Command{
|
|
|
|
Use: fmt.Sprintf("%s path/to/out/file", sh),
|
|
|
|
Short: fmt.Sprintf("generate %s completions", sh),
|
|
|
|
Example: info.help,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
if len(args) != 1 {
|
|
|
|
fmt.Fprintf(os.Stderr, "specify exactly one positional agument\n")
|
|
|
|
err := cmd.Usage()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
if err := info.genFunc(args[0]); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error generating %s completion: %s", sh, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
rootCmd.AddCommand(genCompletionCmd)
|
2018-10-13 15:07:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Subcommand struct {
|
2019-03-22 19:41:12 +01:00
|
|
|
Use string
|
|
|
|
Short string
|
|
|
|
Example string
|
|
|
|
NoRequireConfig bool
|
2020-04-11 15:49:41 +02:00
|
|
|
Run func(ctx context.Context, subcommand *Subcommand, args []string) error
|
2019-03-22 19:41:12 +01:00
|
|
|
SetupFlags func(f *pflag.FlagSet)
|
|
|
|
SetupSubcommands func() []*Subcommand
|
|
|
|
|
|
|
|
config *config.Config
|
2018-10-13 15:07:50 +02:00
|
|
|
configErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Subcommand) ConfigParsingError() error {
|
|
|
|
return s.configErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Subcommand) Config() *config.Config {
|
|
|
|
if !s.NoRequireConfig && s.config == nil {
|
|
|
|
panic("command that requires config is running and has no config set")
|
|
|
|
}
|
|
|
|
return s.config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Subcommand) run(cmd *cobra.Command, args []string) {
|
|
|
|
s.tryParseConfig()
|
2020-04-11 15:49:41 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
endTask := trace.WithTaskFromStackUpdateCtx(&ctx)
|
|
|
|
defer endTask()
|
|
|
|
err := s.Run(ctx, s, args)
|
|
|
|
endTask()
|
2018-10-13 15:07:50 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Subcommand) tryParseConfig() {
|
|
|
|
config, err := config.ParseConfig(rootArgs.configPath)
|
|
|
|
s.configErr = err
|
|
|
|
if err != nil {
|
|
|
|
if s.NoRequireConfig {
|
|
|
|
// doesn't matter
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(os.Stderr, "could not parse config: %s\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.config = config
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddSubcommand(s *Subcommand) {
|
2018-10-13 15:53:52 +02:00
|
|
|
addSubcommandToCobraCmd(rootCmd, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func addSubcommandToCobraCmd(c *cobra.Command, s *Subcommand) {
|
2018-10-13 15:07:50 +02:00
|
|
|
cmd := cobra.Command{
|
2019-03-22 19:41:12 +01:00
|
|
|
Use: s.Use,
|
|
|
|
Short: s.Short,
|
2018-11-16 11:25:48 +01:00
|
|
|
Example: s.Example,
|
2018-10-13 15:53:52 +02:00
|
|
|
}
|
|
|
|
if s.SetupSubcommands == nil {
|
|
|
|
cmd.Run = s.run
|
|
|
|
} else {
|
|
|
|
for _, sub := range s.SetupSubcommands() {
|
|
|
|
addSubcommandToCobraCmd(&cmd, sub)
|
|
|
|
}
|
2018-10-13 15:07:50 +02:00
|
|
|
}
|
|
|
|
if s.SetupFlags != nil {
|
|
|
|
s.SetupFlags(cmd.Flags())
|
|
|
|
}
|
2018-10-13 15:53:52 +02:00
|
|
|
c.AddCommand(&cmd)
|
2018-10-13 15:07:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Run() {
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-11-16 11:25:48 +01:00
|
|
|
}
|