-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from bpineau/split_cmd
Split cmd for readability sake
- Loading branch information
Showing
6 changed files
with
166 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
|
||
"github.com/spf13/viper" | ||
"k8s.io/client-go/util/homedir" | ||
) | ||
|
||
func loadConfigFile() { | ||
viper.SetConfigType("yaml") | ||
viper.SetConfigName(appName) | ||
|
||
// all possible config file paths, by priority | ||
viper.AddConfigPath("/etc/katafygio/") | ||
if home := homedir.HomeDir(); home != "" { | ||
viper.AddConfigPath(home) | ||
} | ||
viper.AddConfigPath(".") | ||
|
||
// prefer the config file path provided by cli flag, if any | ||
if _, err := os.Stat(cfgFile); !os.IsNotExist(err) { | ||
viper.SetConfigFile(cfgFile) | ||
} | ||
|
||
// allow config params through prefixed env variables | ||
viper.SetEnvPrefix("KF") | ||
replacer := strings.NewReplacer("-", "_", ".", "_DOT_") | ||
viper.SetEnvKeyReplacer(replacer) | ||
viper.AutomaticEnv() | ||
|
||
if err := viper.ReadInConfig(); err == nil { | ||
RootCmd.Printf("Using config file: %s", viper.ConfigFileUsed()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package cmd | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var ( | ||
cfgFile string | ||
apiServer string | ||
kubeConf string | ||
dryRun bool | ||
dumpMode bool | ||
logLevel string | ||
logOutput string | ||
logServer string | ||
filter string | ||
localDir string | ||
gitURL string | ||
healthP int | ||
resync int | ||
exclkind []string | ||
exclobj []string | ||
) | ||
|
||
func bindPFlag(key string, cmd string) { | ||
if err := viper.BindPFlag(key, RootCmd.PersistentFlags().Lookup(cmd)); err != nil { | ||
log.Fatal("Failed to bind cli argument:", err) | ||
} | ||
} | ||
|
||
func init() { | ||
cobra.OnInitialize(loadConfigFile) | ||
RootCmd.AddCommand(versionCmd) | ||
|
||
defaultCfg := "/etc/katafygio/" + appName + ".yaml" | ||
RootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", defaultCfg, "Configuration file") | ||
|
||
RootCmd.PersistentFlags().StringVarP(&apiServer, "api-server", "s", "", "Kubernetes api-server url") | ||
bindPFlag("api-server", "api-server") | ||
|
||
RootCmd.PersistentFlags().StringVarP(&kubeConf, "kube-config", "k", "", "Kubernetes config path") | ||
bindPFlag("kube-config", "kube-config") | ||
if err := viper.BindEnv("kube-config", "KUBECONFIG"); err != nil { | ||
log.Fatal("Failed to bind cli argument:", err) | ||
} | ||
|
||
RootCmd.PersistentFlags().BoolVarP(&dryRun, "dry-run", "d", false, "Dry-run mode: don't store anything") | ||
bindPFlag("dry-run", "dry-run") | ||
|
||
RootCmd.PersistentFlags().BoolVarP(&dumpMode, "dump-only", "m", false, "Dump mode: dump everything once and exit") | ||
bindPFlag("dump-only", "dump-only") | ||
|
||
RootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "v", "info", "Log level") | ||
bindPFlag("log.level", "log-level") | ||
|
||
RootCmd.PersistentFlags().StringVarP(&logOutput, "log-output", "o", "stderr", "Log output") | ||
bindPFlag("log.output", "log-output") | ||
|
||
RootCmd.PersistentFlags().StringVarP(&logServer, "log-server", "r", "", "Log server (if using syslog)") | ||
bindPFlag("log.server", "log-server") | ||
|
||
RootCmd.PersistentFlags().StringVarP(&localDir, "local-dir", "e", "./kubernetes-backup", "Where to dump yaml files") | ||
bindPFlag("local-dir", "local-dir") | ||
|
||
RootCmd.PersistentFlags().StringVarP(&gitURL, "git-url", "g", "", "Git repository URL") | ||
bindPFlag("git-url", "git-url") | ||
|
||
RootCmd.PersistentFlags().StringSliceVarP(&exclkind, "exclude-kind", "x", nil, "Ressource kind to exclude. Eg. 'deployment'") | ||
bindPFlag("exclude-kind", "exclude-kind") | ||
|
||
RootCmd.PersistentFlags().StringSliceVarP(&exclobj, "exclude-object", "y", nil, "Object to exclude. Eg. 'configmap:kube-system/kube-dns'") | ||
bindPFlag("exclude-object", "exclude-object") | ||
|
||
RootCmd.PersistentFlags().StringVarP(&filter, "filter", "l", "", "Label filter. Select only objects matching the label.") | ||
bindPFlag("filter", "filter") | ||
|
||
RootCmd.PersistentFlags().IntVarP(&healthP, "healthcheck-port", "p", 0, "Port for answering healthchecks on /health url") | ||
bindPFlag("healthcheck-port", "healthcheck-port") | ||
|
||
RootCmd.PersistentFlags().IntVarP(&resync, "resync-interval", "i", 900, "Full resync interval in seconds (0 to disable)") | ||
bindPFlag("resync-interval", "resync-interval") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package cmd | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
var ( | ||
version = "0.3.0" | ||
|
||
versionCmd = &cobra.Command{ | ||
Use: "version", | ||
Short: "Print the version number", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
RootCmd.Printf("%s version %s\n", appName, version) | ||
}, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters