Skip to content

Commit

Permalink
adding ability to autogenerate witness config yaml
Browse files Browse the repository at this point in the history
Signed-off-by: chaosinthecrd <[email protected]>
  • Loading branch information
ChaosInTheCRD committed Jan 3, 2024
1 parent 88881fa commit 01fe435
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
26 changes: 24 additions & 2 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ func initConfig(rootCmd *cobra.Command, rootOptions *options.RootOptions) error
return fmt.Errorf("failed to read config file: %w", err)
}

//Currently we do not accept configuration for root commands
// Currently we do not accept configuration for root commands
commands := rootCmd.Commands()
for _, cm := range commands {
//Check which command we are running
// Check which command we are running
if !contains(os.Args, cm.Name()) {
continue
}
Expand Down Expand Up @@ -82,6 +82,28 @@ func initConfig(rootCmd *cobra.Command, rootOptions *options.RootOptions) error
return nil
}

func genConfig(rootCmd *cobra.Command, path string) error {
v := viper.New()

// Currently we do not accept configuration for root commands
commands := rootCmd.Commands()
for _, cm := range commands {
v.Set(cm.Name(), nil)
flags := cm.Flags()
flags.VisitAll(func(f *pflag.Flag) {
configKey := fmt.Sprintf("%s.%s", cm.Name(), f.Name)
if f.Value.Type() == "stringSlice" {
v.Set(configKey, []string{})
} else {
v.Set(configKey, "")
}
})
}

v.WriteConfigAs(path)
return nil
}

func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func New() *cobra.Command {
cmd.AddCommand(VerifyCmd())
cmd.AddCommand(RunCmd())
cmd.AddCommand(CompletionCmd())
cmd.AddCommand(GenConfigCmd())

Check failure on line 44 in cmd/root.go

View workflow job for this annotation

GitHub Actions / unit-test / witness

undefined: GenConfigCmd

Check failure on line 44 in cmd/root.go

View workflow job for this annotation

GitHub Actions / sast / witness

undefined: GenConfigCmd
cmd.AddCommand(versionCmd())
cobra.OnInitialize(func() { preRoot(cmd, ro, logger) })
return cmd
Expand Down

0 comments on commit 01fe435

Please sign in to comment.