Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a 'flags' subcommand to list global flags #1033

Merged
merged 1 commit into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 60 additions & 0 deletions cmd/common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package common

import (
"github.com/spf13/cobra"
"github.com/wal-g/wal-g/cmd/common/st"
"github.com/wal-g/wal-g/internal"
)

const usageTemplate = `Usage:{{if .Runnable}}
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}

Aliases:
{{.NameAndAliases}}{{end}}{{if .HasExample}}

Examples:
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}

Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}

Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}

Global Flags:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}` +
// additional custom message : cli flags introduced by 'internal.AddConfigFlags()' are hidden by default
`

To get the complete list of all global flags, run: 'wal-g flags'` +
`{{if .HasHelpSubCommands}}

Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}

Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`

func Init(cmd *cobra.Command, dbName string) {
internal.ConfigureSettings(dbName)
cobra.OnInitialize(internal.InitConfig, internal.Configure)

cmd.SetUsageTemplate(usageTemplate)
cmd.InitDefaultVersionFlag()
internal.AddConfigFlags(cmd)

cmd.PersistentFlags().StringVar(&internal.CfgFile, "config", "", "config file (default is $HOME/.walg.json)")

// Init help subcommand
cmd.InitDefaultHelpCmd()
helpCmd, _, _ := cmd.Find([]string{"help"})
// fix to disable the required settings check for the help subcommand
helpCmd.PersistentPreRun = func(*cobra.Command, []string) {}

// Add flags subcommand
cmd.AddCommand(FlagsCmd)

// Add storage tools
cmd.AddCommand(st.StorageToolsCmd)
}
51 changes: 51 additions & 0 deletions cmd/common/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package common

import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

// FlagsCmd represents the flags command
var FlagsCmd = &cobra.Command{
Use: "flags",
Short: "Display the list of available global flags for all wal-g commands",
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
_ = cmd.Usage()
},
}

func init() {
defaultUsageFn := (&cobra.Command{}).UsageFunc()
defaultHelpFn := (&cobra.Command{}).HelpFunc()

FlagsCmd.SetUsageTemplate(flagsUsageTemplate)
FlagsCmd.SetHelpTemplate(flagsHelpTemplate)

FlagsCmd.SetUsageFunc(func(cmd *cobra.Command) error {
cmd.Parent().PersistentFlags().VisitAll(func(f *pflag.Flag) { f.Hidden = false })

return defaultUsageFn(cmd)
})
FlagsCmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
cmd.Parent().PersistentFlags().VisitAll(func(f *pflag.Flag) { f.Hidden = false })

defaultHelpFn(cmd, args)
})

// fix to disable the required settings check for the help subcommand
FlagsCmd.PersistentPreRun = func(*cobra.Command, []string) {}
}

const flagsHelpTemplate = `{{with (or .Long .Short)}}{{. | trimTrailingWhitespaces}}{{end}}

Usage:
{{.UseLine}}

Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}

{{.Usage}}`
const flagsUsageTemplate = `Global Flags:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}
`
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 3 additions & 6 deletions cmd/fdb/fdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"strings"

"github.com/wal-g/wal-g/cmd/common"

"github.com/spf13/cobra"
"github.com/wal-g/tracelog"
"github.com/wal-g/wal-g/internal"
Expand Down Expand Up @@ -37,10 +39,5 @@ func Execute() {
}

func init() {
internal.ConfigureSettings(internal.FDB)
cobra.OnInitialize(internal.InitConfig, internal.Configure)

cmd.PersistentFlags().StringVar(&internal.CfgFile, "config", "", "config file (default is $HOME/.wal-g.yaml)")
cmd.InitDefaultVersionFlag()
internal.AddConfigFlags(cmd)
common.Init(cmd, internal.FDB)
}
11 changes: 2 additions & 9 deletions cmd/gp/gp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"strings"

"github.com/wal-g/wal-g/cmd/st"
"github.com/wal-g/wal-g/cmd/common"

"github.com/wal-g/wal-g/cmd/pg"

Expand Down Expand Up @@ -41,13 +41,9 @@ func Execute() {
}

func init() {
internal.ConfigureSettings(internal.GP)
cobra.OnInitialize(internal.InitConfig, internal.Configure)
common.Init(cmd, internal.GP)

cmd.PersistentFlags().StringVar(&internal.CfgFile, "config", "", "config file (default is $HOME/.wal-g.yaml)")
_ = cmd.MarkFlagRequired("config") // config is required for Greenplum WAL-G
cmd.InitDefaultVersionFlag()
internal.AddConfigFlags(cmd)

// wrap the Postgres command so it can be used in the same binary
wrappedPgCmd := pg.Cmd
Expand All @@ -59,7 +55,4 @@ func init() {
wrappedPreRun(cmd, args)
}
cmd.AddCommand(wrappedPgCmd)

// Storage tools
cmd.AddCommand(st.StorageToolsCmd)
}
11 changes: 2 additions & 9 deletions cmd/mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"strings"

"github.com/wal-g/wal-g/cmd/st"
"github.com/wal-g/wal-g/cmd/common"

"github.com/spf13/cobra"
"github.com/wal-g/tracelog"
Expand Down Expand Up @@ -41,14 +41,7 @@ func Execute() {
}

func init() {
internal.ConfigureSettings(internal.MONGO)
cobra.OnInitialize(internal.InitConfig, internal.Configure)
common.Init(cmd, internal.MONGO)

internal.RequiredSettings[internal.MongoDBUriSetting] = true
cmd.PersistentFlags().StringVar(&internal.CfgFile, "config", "", "config file (default is $HOME/.wal-g.yaml)")
cmd.InitDefaultVersionFlag()
internal.AddConfigFlags(cmd)

// Storage tools
cmd.AddCommand(st.StorageToolsCmd)
}
12 changes: 2 additions & 10 deletions cmd/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"strings"

"github.com/wal-g/wal-g/cmd/st"
"github.com/wal-g/wal-g/cmd/common"

"github.com/spf13/cobra"
"github.com/wal-g/tracelog"
Expand Down Expand Up @@ -41,14 +41,6 @@ func Execute() {
}

func init() {
internal.ConfigureSettings(internal.MYSQL)
cobra.OnInitialize(internal.InitConfig, internal.Configure)

cmd.PersistentFlags().StringVar(&internal.CfgFile, "config", "", "config file (default is $HOME/.walg.json)")
common.Init(cmd, internal.MYSQL)
cmd.PersistentFlags().BoolVarP(&internal.Turbo, "turbo", "", false, "Ignore all kinds of throttling defined in config")
cmd.InitDefaultVersionFlag()
internal.AddConfigFlags(cmd)

// Storage tools
cmd.AddCommand(st.StorageToolsCmd)
}
13 changes: 3 additions & 10 deletions cmd/pg/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"strings"

"github.com/wal-g/wal-g/cmd/st"
"github.com/wal-g/wal-g/cmd/common"

"github.com/wal-g/wal-g/internal/databases/postgres"

Expand All @@ -30,6 +30,7 @@ var (
PersistentPreRun: func(cmd *cobra.Command, args []string) {
err := internal.AssertRequiredSettingsSet()
tracelog.ErrorLogger.FatalOnError(err)

if viper.IsSet(internal.PgWalSize) {
postgres.SetWalSize(viper.GetUint64(internal.PgWalSize))
}
Expand All @@ -48,14 +49,6 @@ func Execute() {
}

func configureCommand() {
internal.ConfigureSettings(internal.PG)
cobra.OnInitialize(internal.InitConfig, internal.Configure)

Cmd.PersistentFlags().StringVar(&internal.CfgFile, "config", "", "config file (default is $HOME/.walg.json)")
common.Init(Cmd, internal.PG)
Cmd.PersistentFlags().BoolVarP(&internal.Turbo, "turbo", "", false, "Ignore all kinds of throttling defined in config")
Cmd.InitDefaultVersionFlag()
internal.AddConfigFlags(Cmd)

// Storage tools
Cmd.AddCommand(st.StorageToolsCmd)
}
12 changes: 2 additions & 10 deletions cmd/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"strings"

"github.com/wal-g/wal-g/cmd/st"
"github.com/wal-g/wal-g/cmd/common"

"github.com/spf13/cobra"
"github.com/wal-g/tracelog"
Expand Down Expand Up @@ -39,13 +39,5 @@ func Execute() {
}

func init() {
internal.ConfigureSettings(internal.REDIS)
cobra.OnInitialize(internal.InitConfig, internal.Configure)

cmd.PersistentFlags().StringVar(&internal.CfgFile, "config", "", "config file (default is $HOME/.walg.json)")
cmd.InitDefaultVersionFlag()
internal.AddConfigFlags(cmd)

// Storage tools
cmd.AddCommand(st.StorageToolsCmd)
common.Init(cmd, internal.REDIS)
}
10 changes: 2 additions & 8 deletions cmd/sqlserver/sqlserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"strings"

"github.com/wal-g/wal-g/cmd/st"
"github.com/wal-g/wal-g/cmd/common"

"github.com/spf13/cobra"
"github.com/wal-g/wal-g/internal"
Expand Down Expand Up @@ -34,11 +34,5 @@ func Execute() {
}

func init() {
internal.ConfigureSettings(internal.SQLSERVER)
cobra.OnInitialize(internal.InitConfig, internal.Configure)
cmd.PersistentFlags().StringVar(&internal.CfgFile, "config", "", "config file (default is $HOME/.walg.json)")
cmd.InitDefaultVersionFlag()

// Storage tools
cmd.AddCommand(st.StorageToolsCmd)
common.Init(cmd, internal.SQLSERVER)
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ require (
github.com/pkg/errors v0.9.1
github.com/pkg/sftp v1.11.0
github.com/spf13/cobra v0.0.5
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.6.1
github.com/stretchr/testify v1.7.0
github.com/ulikunitz/xz v0.5.8
Expand Down Expand Up @@ -112,8 +114,6 @@ require (
github.com/siddontang/go-log v0.0.0-20180807004314-8d05993dda07 // indirect
github.com/spf13/afero v1.2.2 // indirect
github.com/spf13/cast v1.3.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.2.0 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/tinylib/msgp v1.1.0 // indirect
Expand Down
9 changes: 7 additions & 2 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/wal-g/tracelog"
"github.com/wal-g/wal-g/internal/webserver"
Expand Down Expand Up @@ -506,6 +507,7 @@ func ConfigureAndRunDefaultWebServer() error {
}

func AddConfigFlags(Cmd *cobra.Command) {
cfgFlags := &pflag.FlagSet{}
for k := range AllowedSettings {
flagName := toFlagName(k)
isRequired, exist := RequiredSettings[k]
Expand All @@ -514,9 +516,12 @@ func AddConfigFlags(Cmd *cobra.Command) {
flagUsage = "Required, can be set though this flag or " + k + " variable"
}

Cmd.PersistentFlags().String(flagName, "", flagUsage)
_ = viper.BindPFlag(k, Cmd.PersistentFlags().Lookup(flagName))
cfgFlags.String(flagName, "", flagUsage)
_ = viper.BindPFlag(k, cfgFlags.Lookup(flagName))
}
cfgFlags.VisitAll(func(f *pflag.Flag) { f.Hidden = true })

Cmd.PersistentFlags().AddFlagSet(cfgFlags)
}

// InitConfig reads config file and ENV variables if set.
Expand Down