Skip to content

Commit

Permalink
chore(consts): Add consts for flag and config keys
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Oct 25, 2023
1 parent ab9488d commit 82c1162
Show file tree
Hide file tree
Showing 17 changed files with 169 additions and 96 deletions.
13 changes: 7 additions & 6 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/clevyr/kubedb/cmd/port_forward"
"github.com/clevyr/kubedb/cmd/restore"
"github.com/clevyr/kubedb/internal/config/flags"
"github.com/clevyr/kubedb/internal/consts"
"github.com/clevyr/kubedb/internal/util"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -69,14 +70,14 @@ func preRun(cmd *cobra.Command, args []string) error {
cmd.PersistentPostRun = func(cmd *cobra.Command, args []string) { cancel() }
cmd.SetContext(ctx)

kubeconfig := viper.GetString("kubernetes.kubeconfig")
kubeconfig := viper.GetString(consts.KubeconfigKey)
if kubeconfig == "$HOME" || strings.HasPrefix(kubeconfig, "$HOME"+string(os.PathSeparator)) {
home, err := os.UserHomeDir()
if err != nil {
return err
}
kubeconfig = home + kubeconfig[5:]
viper.Set("kubernetes.kubeconfig", kubeconfig)
viper.Set(consts.KubeconfigKey, kubeconfig)
}

initLog(cmd)
Expand All @@ -88,24 +89,24 @@ func preRun(cmd *cobra.Command, args []string) error {
}

func initLog(cmd *cobra.Command) {
logLevel := viper.GetString("log.level")
logLevel := viper.GetString(consts.LogLevelKey)
parsedLevel, err := log.ParseLevel(logLevel)
if err != nil {
log.WithField("log-level", logLevel).Warn("invalid log level. defaulting to info.")
viper.Set("log.level", "info")
viper.Set(consts.LogLevelKey, "info")
parsedLevel = log.InfoLevel
}
log.SetLevel(parsedLevel)

logFormat := viper.GetString("log.format")
logFormat := viper.GetString(consts.LogFormatKey)
switch logFormat {
case "text", "txt", "t":
log.SetFormatter(&log.TextFormatter{})
case "json", "j":
log.SetFormatter(&log.JSONFormatter{})
default:
log.WithField("log-format", logFormat).Warn("invalid log formatter. defaulting to text.")
viper.Set("log.format", "text")
viper.Set(consts.LogFormatKey, "text")
}
}

Expand Down
9 changes: 5 additions & 4 deletions cmd/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/clevyr/kubedb/internal/actions/dump"
"github.com/clevyr/kubedb/internal/config"
"github.com/clevyr/kubedb/internal/config/flags"
"github.com/clevyr/kubedb/internal/consts"
"github.com/clevyr/kubedb/internal/util"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -64,7 +65,7 @@ func validArgs(cmd *cobra.Command, args []string, toComplete string) ([]string,
return nil, cobra.ShellCompDirectiveNoFileComp
}

viper.Set("kubernetes.no-job", true)
viper.Set(consts.NoJobKey, true)

err := preRun(cmd, args)
if err != nil {
Expand All @@ -83,9 +84,9 @@ func preRun(cmd *cobra.Command, args []string) (err error) {
flags.BindJobPodLabels(cmd)
flags.BindNoJob(cmd)
flags.BindRemoteGzip(cmd)
action.RemoteGzip = viper.GetBool("remote-gzip")
action.RemoteGzip = viper.GetBool(consts.RemoteGzipKey)
flags.BindSpinner(cmd)
action.Spinner = viper.GetString("spinner.name")
action.Spinner = viper.GetString(consts.SpinnerKey)

if len(args) > 0 {
action.Filename = args[0]
Expand All @@ -107,7 +108,7 @@ func preRun(cmd *cobra.Command, args []string) (err error) {
return err
}

if action.Filename != "" && !cmd.Flags().Lookup("format").Changed {
if action.Filename != "" && !cmd.Flags().Lookup(consts.FormatFlag).Changed {
action.Format = action.Dialect.FormatFromFilename(action.Filename)
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package exec
import (
"github.com/clevyr/kubedb/internal/actions/exec"
"github.com/clevyr/kubedb/internal/config/flags"
"github.com/clevyr/kubedb/internal/consts"
"github.com/clevyr/kubedb/internal/util"
"github.com/spf13/cobra"
_ "k8s.io/client-go/plugin/pkg/client/auth"
Expand All @@ -26,7 +27,7 @@ func NewCommand() *cobra.Command {
flags.Database(cmd)
flags.Username(cmd)
flags.Password(cmd)
cmd.Flags().StringVarP(&action.Command, "command", "c", "", "Run a single command and exit")
cmd.Flags().StringVarP(&action.Command, consts.CommandFlag, "c", "", "Run a single command and exit")
return cmd
}

Expand Down
17 changes: 9 additions & 8 deletions cmd/port_forward/port_forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/clevyr/kubedb/internal/actions/port_forward"
"github.com/clevyr/kubedb/internal/config/flags"
"github.com/clevyr/kubedb/internal/consts"
"github.com/clevyr/kubedb/internal/util"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -25,18 +26,18 @@ func NewCommand() *cobra.Command {

flags.Port(cmd)

cmd.Flags().StringSlice("address", []string{"127.0.0.1", "::1"}, "Local listen address")
cmd.Flags().StringSlice(consts.AddrFlag, []string{"127.0.0.1", "::1"}, "Local listen address")
if err := cmd.RegisterFlagCompletionFunc(
"address",
consts.AddrFlag,
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"127.0.0.1\tprivate", "::1\tprivate", "0.0.0.0\tpublic", "::\tpublic"}, cobra.ShellCompDirectiveNoFileComp
}); err != nil {
panic(err)
}

cmd.Flags().Uint16("listen-port", 0, "Local listen port (default discovered)")
cmd.Flags().Uint16(consts.ListenPortFlag, 0, "Local listen port (default discovered)")
if err := cmd.RegisterFlagCompletionFunc(
"listen-port",
consts.ListenPortFlag,
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
err := preRun(cmd, args)
if err != nil {
Expand All @@ -58,22 +59,22 @@ func NewCommand() *cobra.Command {
}

func preRun(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlag("port-forward.address", cmd.Flags().Lookup("address")); err != nil {
if err := viper.BindPFlag(consts.PortForwardAddrKey, cmd.Flags().Lookup(consts.AddrFlag)); err != nil {
panic(err)
}
action.Addresses = viper.GetStringSlice("port-forward.address")
action.Addresses = viper.GetStringSlice(consts.PortForwardAddrKey)

err := util.DefaultSetup(cmd, &action.Global, util.SetupOptions{DisableAuthFlags: true})
if err != nil {
return err
}

if len(args) != 0 {
if err := cmd.Flags().Set("listen-port", args[0]); err != nil {
if err := cmd.Flags().Set(consts.ListenPortFlag, args[0]); err != nil {
return err
}
}
action.LocalPort, err = cmd.Flags().GetUint16("listen-port")
action.LocalPort, err = cmd.Flags().GetUint16(consts.ListenPortFlag)
if err != nil {
util.Teardown(cmd, &action.Global)
panic(err)
Expand Down
13 changes: 7 additions & 6 deletions cmd/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/AlecAivazis/survey/v2"
"github.com/clevyr/kubedb/internal/actions/restore"
"github.com/clevyr/kubedb/internal/config/flags"
"github.com/clevyr/kubedb/internal/consts"
"github.com/clevyr/kubedb/internal/util"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -50,7 +51,7 @@ Supported Input Filetypes:
flags.RemoteGzip(cmd)
flags.Analyze(cmd)
flags.Spinner(cmd, &action.Spinner)
cmd.Flags().BoolVarP(&action.Force, "force", "f", false, "Do not prompt before restore")
cmd.Flags().BoolVarP(&action.Force, consts.ForceFlag, "f", false, "Do not prompt before restore")

return cmd
}
Expand All @@ -60,7 +61,7 @@ func validArgs(cmd *cobra.Command, args []string, toComplete string) ([]string,
return nil, cobra.ShellCompDirectiveNoFileComp
}

viper.Set("kubernetes.no-job", true)
viper.Set(consts.NoJobKey, true)
action.Force = true

err := preRun(cmd, args)
Expand All @@ -78,13 +79,13 @@ func validArgs(cmd *cobra.Command, args []string, toComplete string) ([]string,

func preRun(cmd *cobra.Command, args []string) (err error) {
flags.BindRemoteGzip(cmd)
action.RemoteGzip = viper.GetBool("remote-gzip")
action.RemoteGzip = viper.GetBool(consts.RemoteGzipKey)
flags.BindAnalyze(cmd)
action.Analyze = viper.GetBool("restore.analyze")
action.Analyze = viper.GetBool(consts.AnalyzeKey)
flags.BindJobPodLabels(cmd)
flags.BindNoJob(cmd)
flags.BindSpinner(cmd)
action.Spinner = viper.GetString("spinner.name")
action.Spinner = viper.GetString(consts.SpinnerKey)

if len(args) > 0 {
action.Filename = args[0]
Expand Down Expand Up @@ -117,7 +118,7 @@ func preRun(cmd *cobra.Command, args []string) (err error) {
return err
}

if !cmd.Flags().Lookup("format").Changed {
if !cmd.Flags().Lookup(consts.FormatFlag).Changed {
action.Format = action.Dialect.FormatFromFilename(action.Filename)
}

Expand Down
43 changes: 22 additions & 21 deletions internal/config/flags/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/clevyr/kubedb/internal/config"
"github.com/clevyr/kubedb/internal/consts"
"github.com/clevyr/kubedb/internal/database/dialect"
"github.com/clevyr/kubedb/internal/database/sqlformat"
"github.com/clevyr/kubedb/internal/util"
Expand All @@ -14,9 +15,9 @@ import (
)

func Dialect(cmd *cobra.Command) {
cmd.PersistentFlags().String("dialect", "", "Database dialect. One of (postgres|mariadb|mongodb) (default discovered)")
cmd.PersistentFlags().String(consts.DialectFlag, "", "Database dialect. One of (postgres|mariadb|mongodb) (default discovered)")
err := cmd.RegisterFlagCompletionFunc(
"dialect",
consts.DialectFlag,
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{
dialect.Postgres{}.Name(),
Expand All @@ -31,9 +32,9 @@ func Dialect(cmd *cobra.Command) {

func Format(cmd *cobra.Command, p *sqlformat.Format) {
*p = sqlformat.Gzip
cmd.Flags().VarP(p, "format", "F", `Output file format One of (gzip|custom|plain)`)
cmd.Flags().VarP(p, consts.FormatFlag, "F", `Output file format One of (gzip|custom|plain)`)
err := cmd.RegisterFlagCompletionFunc(
"format",
consts.FormatFlag,
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{
sqlformat.Gzip.String(),
Expand All @@ -47,71 +48,71 @@ func Format(cmd *cobra.Command, p *sqlformat.Format) {
}

func Port(cmd *cobra.Command) {
cmd.PersistentFlags().Uint16("port", 0, "Database port (default discovered)")
cmd.PersistentFlags().Uint16(consts.PortFlag, 0, "Database port (default discovered)")
}

func Database(cmd *cobra.Command) {
cmd.Flags().StringP("dbname", "d", "", "Database name to use (default discovered)")
err := cmd.RegisterFlagCompletionFunc("dbname", listDatabases)
cmd.Flags().StringP(consts.DbnameFlag, "d", "", "Database name to use (default discovered)")
err := cmd.RegisterFlagCompletionFunc(consts.DbnameFlag, listDatabases)
if err != nil {
panic(err)
}
}

func Username(cmd *cobra.Command) {
cmd.Flags().StringP("username", "U", "", "Database username (default discovered)")
cmd.Flags().StringP(consts.UsernameFlag, "U", "", "Database username (default discovered)")
}

func Password(cmd *cobra.Command) {
cmd.Flags().StringP("password", "p", "", "Database password (default discovered)")
cmd.Flags().StringP(consts.PasswordFlag, "p", "", "Database password (default discovered)")
}

func SingleTransaction(cmd *cobra.Command, p *bool) {
cmd.Flags().BoolVarP(p, "single-transaction", "1", true, "Restore as a single transaction")
cmd.Flags().BoolVarP(p, consts.SingleTransactionFlag, "1", true, "Restore as a single transaction")
}

func Clean(cmd *cobra.Command, p *bool) {
cmd.Flags().BoolVarP(p, "clean", "c", true, "Clean (drop) database objects before recreating")
cmd.Flags().BoolVarP(p, consts.CleanFlag, "c", true, "Clean (drop) database objects before recreating")
}

func IfExists(cmd *cobra.Command, p *bool) {
cmd.Flags().BoolVar(p, "if-exists", true, "Use IF EXISTS when dropping objects")
cmd.Flags().BoolVar(p, consts.IfExistsFlag, true, "Use IF EXISTS when dropping objects")
}

func NoOwner(cmd *cobra.Command, p *bool) {
cmd.Flags().BoolVarP(p, "no-owner", "O", true, "Skip restoration of object ownership in plain-text format")
cmd.Flags().BoolVarP(p, consts.NoOwnerFlag, "O", true, "Skip restoration of object ownership in plain-text format")
}

func Tables(cmd *cobra.Command, p *[]string) {
cmd.Flags().StringSliceVarP(p, "table", "t", []string{}, "Dump the specified table(s) only")
err := cmd.RegisterFlagCompletionFunc("table", listTables)
cmd.Flags().StringSliceVarP(p, consts.TableFlag, "t", []string{}, "Dump the specified table(s) only")
err := cmd.RegisterFlagCompletionFunc(consts.TableFlag, listTables)
if err != nil {
panic(err)
}
}

func ExcludeTable(cmd *cobra.Command, p *[]string) {
cmd.Flags().StringSliceVarP(p, "exclude-table", "T", []string{}, "Do NOT dump the specified table(s)")
err := cmd.RegisterFlagCompletionFunc("exclude-table", listTables)
cmd.Flags().StringSliceVarP(p, consts.ExcludeTableFlag, "T", []string{}, "Do NOT dump the specified table(s)")
err := cmd.RegisterFlagCompletionFunc(consts.ExcludeTableFlag, listTables)
if err != nil {
panic(err)
}
}

func ExcludeTableData(cmd *cobra.Command, p *[]string) {
cmd.Flags().StringSliceVarP(p, "exclude-table-data", "D", []string{}, "Do NOT dump data for the specified table(s)")
err := cmd.RegisterFlagCompletionFunc("exclude-table-data", listTables)
cmd.Flags().StringSliceVarP(p, consts.ExcludeTableDataFlag, "D", []string{}, "Do NOT dump data for the specified table(s)")
err := cmd.RegisterFlagCompletionFunc(consts.ExcludeTableDataFlag, listTables)
if err != nil {
panic(err)
}
}

func Analyze(cmd *cobra.Command) {
cmd.Flags().Bool("analyze", true, "Run an analyze query after restore")
cmd.Flags().Bool(consts.AnalyzeFlag, true, "Run an analyze query after restore")
}

func BindAnalyze(cmd *cobra.Command) {
if err := viper.BindPFlag("restore.analyze", cmd.Flags().Lookup("analyze")); err != nil {
if err := viper.BindPFlag(consts.AnalyzeKey, cmd.Flags().Lookup(consts.AnalyzeFlag)); err != nil {
panic(err)
}
}
Expand Down
5 changes: 3 additions & 2 deletions internal/config/flags/filesystem.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package flags

import (
"github.com/clevyr/kubedb/internal/consts"
"github.com/spf13/cobra"
)

func Directory(cmd *cobra.Command, p *string) {
cmd.Flags().StringVarP(p, "directory", "C", ".", "Directory to dump to")
cmd.Flags().StringVarP(p, consts.DirectoryFlag, "C", ".", "Directory to dump to")
err := cmd.RegisterFlagCompletionFunc(
"directory",
consts.DirectoryFlag,
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return nil, cobra.ShellCompDirectiveFilterDirs
})
Expand Down
7 changes: 4 additions & 3 deletions internal/config/flags/generic.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package flags

import (
"github.com/clevyr/kubedb/internal/consts"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

const DefaultSpinner = "sand"

func Spinner(cmd *cobra.Command, p *string) {
cmd.Flags().StringVar(p, "spinner", DefaultSpinner, "Spinner from https://jsfiddle.net/sindresorhus/2eLtsbey/embedded/result/")
if err := cmd.Flags().MarkHidden("spinner"); err != nil {
cmd.Flags().StringVar(p, consts.SpinnerFlag, DefaultSpinner, "Spinner from https://jsfiddle.net/sindresorhus/2eLtsbey/embedded/result/")
if err := cmd.Flags().MarkHidden(consts.SpinnerFlag); err != nil {
panic(err)
}
}

func BindSpinner(cmd *cobra.Command) {
if err := viper.BindPFlag("spinner.name", cmd.Flags().Lookup("spinner")); err != nil {
if err := viper.BindPFlag(consts.SpinnerKey, cmd.Flags().Lookup(consts.SpinnerFlag)); err != nil {
panic(err)
}
}
Loading

0 comments on commit 82c1162

Please sign in to comment.