Skip to content

Commit

Permalink
"fix" persistent flags from viper
Browse files Browse the repository at this point in the history
This can't be the right way to do this, but its the only way I can
get the token and factory to be read from a config file or environment
w/o cobra missing them.

Signed-off-by: Andy Doan <[email protected]>
  • Loading branch information
doanac committed Sep 12, 2019
1 parent d67f75e commit 1c4319a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
7 changes: 6 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func init() {
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "Print verbose logging")

rootCmd.PersistentFlags().StringP("token", "t", "", "API token from https://app.foundries.io/settings/tokens/")
rootCmd.MarkPersistentFlagRequired("token")

if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
fmt.Println(err)
Expand Down Expand Up @@ -66,6 +65,12 @@ func initConfig() {
if viper.GetBool("verbose") {
logrus.SetLevel(logrus.DebugLevel)
}
if len(viper.GetString("token")) == 0 {
rootCmd.MarkPersistentFlagRequired("token")
}
if len(viper.GetString("factory")) == 0 {
targetsCmd.MarkPersistentFlagRequired("factory")
}

api = client.NewApiClient("https://api.foundries.io", viper.GetString("token"))
}
1 change: 0 additions & 1 deletion cmd/targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ var targetsCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(targetsCmd)
targetsCmd.PersistentFlags().StringP("factory", "f", "", "Factory to list targets for")
targetsCmd.MarkPersistentFlagRequired("factory")

if err := viper.BindPFlags(targetsCmd.PersistentFlags()); err != nil {
fmt.Println(err)
Expand Down

0 comments on commit 1c4319a

Please sign in to comment.