diff --git a/cmd/dfdaemon/app/root.go b/cmd/dfdaemon/app/root.go index 9955b92cf..152a30a2d 100644 --- a/cmd/dfdaemon/app/root.go +++ b/cmd/dfdaemon/app/root.go @@ -106,7 +106,10 @@ func init() { rf.StringSlice("node", nil, "specify the addresses(host:port) of supernodes that will be passed to dfget.") exitOnError(bindRootFlags(viper.GetViper()), "bind root command flags") + + // add sub commands rootCmd.AddCommand(cmd.NewGenDocCommand("dfdaemon")) + rootCmd.AddCommand(cmd.NewConfigCommand("dfdaemon", getDefaultConfig)) } // bindRootFlags binds flags on rootCmd to the given viper instance. @@ -159,6 +162,11 @@ func Execute() { } } +// getDefaultConfig returns the default configuration of dfdaemon +func getDefaultConfig() (interface{}, error) { + return getConfigFromViper(rootCmd, viper.GetViper()) +} + // getConfigFromViper returns dfdaemon config from the given viper instance func getConfigFromViper(cmd *cobra.Command, v *viper.Viper) (*config.Properties, error) { // override supernodes in config file if --node is specified in cli. diff --git a/cmd/supernode/app/root.go b/cmd/supernode/app/root.go index 817b3c878..639b6fb21 100644 --- a/cmd/supernode/app/root.go +++ b/cmd/supernode/app/root.go @@ -122,7 +122,10 @@ var rootCmd = &cobra.Command{ func init() { setupFlags(rootCmd) + + // add sub commands rootCmd.AddCommand(cmd.NewGenDocCommand("supernode")) + rootCmd.AddCommand(cmd.NewConfigCommand("supernode", getDefaultConfig)) } // setupFlags setups flags for command line. @@ -297,6 +300,11 @@ func readConfigFile(v *viper.Viper, cmd *cobra.Command) error { return nil } +// getDefaultConfig returns the default configuration of supernode +func getDefaultConfig() (interface{}, error) { + return getConfigFromViper(viper.GetViper()) +} + // getConfigFromViper returns supernode config from the given viper instance func getConfigFromViper(v *viper.Viper) (*config.Config, error) { cfg := config.NewConfig() diff --git a/pkg/cmd/config.go b/pkg/cmd/config.go new file mode 100644 index 000000000..5b3088d41 --- /dev/null +++ b/pkg/cmd/config.go @@ -0,0 +1,67 @@ +/* + * Copyright The Dragonfly Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cmd + +import ( + "fmt" + + "github.com/dragonflyoss/Dragonfly/pkg/printer" + + "github.com/pkg/errors" + "github.com/spf13/cobra" + "gopkg.in/yaml.v2" +) + +// NewConfigCommand returns cobra.Command for " config" command +func NewConfigCommand(componentName string, defaultComponentConfigFunc func() (interface{}, error)) *cobra.Command { + cmd := &cobra.Command{ + Use: "config", + Short: fmt.Sprintf("Manage the configurations of %s", componentName), + Args: cobra.NoArgs, + } + + cmd.AddCommand(newConfigPrintDefaultCommand(componentName, defaultComponentConfigFunc)) + return cmd +} + +// newConfigPrintDefaultCommand returns cobra.Command for " config default" command +func newConfigPrintDefaultCommand(componentName string, defaultComponentConfigFunc func() (interface{}, error)) *cobra.Command { + cmd := &cobra.Command{ + Use: "default", + Short: fmt.Sprintf("Print the default configurations of %s in yaml format", componentName), + Args: cobra.NoArgs, + SilenceErrors: true, + SilenceUsage: true, + RunE: func(cmd *cobra.Command, args []string) error { + return runConfigPrintDefault(defaultComponentConfigFunc) + }, + } + return cmd +} + +func runConfigPrintDefault(defaultComponentConfigFunc func() (interface{}, error)) error { + cfg, err := defaultComponentConfigFunc() + if err != nil { + return errors.Wrap(err, "failed to get component default configurations") + } + d, err := yaml.Marshal(cfg) + if err != nil { + return errors.Wrap(err, "failed to marshal component default configurations") + } + printer.Print(string(d)) + return nil +} diff --git a/pkg/printer/printer.go b/pkg/printer/printer.go index 4f272e475..20bcba6bf 100644 --- a/pkg/printer/printer.go +++ b/pkg/printer/printer.go @@ -34,6 +34,13 @@ type StdPrinter struct { Out io.Writer } +// Println outputs info to console directly. +func (sp *StdPrinter) Print(msg string) { + if sp.Out != nil { + fmt.Fprint(sp.Out, msg) + } +} + // Println outputs info to console directly. func (sp *StdPrinter) Println(msg string) { if sp.Out != nil { @@ -48,6 +55,13 @@ func (sp *StdPrinter) Printf(format string, a ...interface{}) { } } +// Print outputs info to console directly. +func Print(msg string) { + if Printer.Out != nil { + fmt.Fprint(Printer.Out, msg) + } +} + // Println outputs info to console directly. func Println(msg string) { if Printer.Out != nil {