Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
feature: add 'config default' command to print the default configurat…
Browse files Browse the repository at this point in the history
…ion of components

Signed-off-by: SataQiu <[email protected]>
  • Loading branch information
SataQiu committed Dec 3, 2019
1 parent f8ecf02 commit 992496d
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cmd/dfdaemon/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions cmd/supernode/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand Down
67 changes: 67 additions & 0 deletions pkg/cmd/config.go
Original file line number Diff line number Diff line change
@@ -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 "<component> 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 "<component> 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
}
14 changes: 14 additions & 0 deletions pkg/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 992496d

Please sign in to comment.