Skip to content

Commit

Permalink
Add pflag binding and fix loading of config (flyteorg#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
EngHabu authored Jul 10, 2021
1 parent de66a38 commit 1571415
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 209 deletions.
9 changes: 5 additions & 4 deletions cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
"github.com/flyteorg/flytectl/pkg/printer"
)

//go:generate pflags Config --bind-default-var

var (
defaultConfig = &Config{}
section = config.MustRegisterSection("root", defaultConfig)
defaultConfig = &Config{
Output: printer.OutputFormatTABLE.String(),
}

section = config.MustRegisterSection("root", defaultConfig)
)

// Config hold configration for flytectl flag
Expand Down
57 changes: 0 additions & 57 deletions cmd/config/config_flags.go

This file was deleted.

144 changes: 0 additions & 144 deletions cmd/config/config_flags_test.go

This file was deleted.

15 changes: 14 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ func newRootCmd() *cobra.Command {

rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.flyte/config.yaml)")

configAccessor.InitializePflags(rootCmd.PersistentFlags())

// Due to https://github.com/flyteorg/flyte/issues/341, project flag will have to be specified as
// --root.project, this adds a convenience on top to allow --project to be used
rootCmd.PersistentFlags().StringVarP(&(config.GetConfig().Project), "project", "p", "", "Specifies the Flyte project.")
Expand All @@ -69,19 +71,30 @@ func newRootCmd() *cobra.Command {
return rootCmd
}

func initConfig(_ *cobra.Command, _ []string) error {
func initConfig(cmd *cobra.Command, _ []string) error {
configFile := f.FilePathJoin(f.UserHomeDir(), configFileDir, configFileName)
if len(os.Getenv("FLYTECTL_CONFIG")) > 0 {
configFile = os.Getenv("FLYTECTL_CONFIG")
}

if len(cfgFile) > 0 {
configFile = cfgFile
}

configAccessor = viper.NewAccessor(stdConfig.Options{
StrictMode: true,
SearchPaths: []string{configFile},
})

// persistent flags were initially bound to the root command so we must bind to the same command to avoid
// overriding those initial ones. We need to traverse up to the root command and initialize pflags for that.
rootCmd := cmd
for rootCmd.Parent() != nil {
rootCmd = rootCmd.Parent()
}

configAccessor.InitializePflags(rootCmd.PersistentFlags())

err := configAccessor.UpdateConfig(context.TODO())
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ admin:
# For GRPC endpoints you might want to use dns:///flyte.myexample.com
endpoint: dns:///localhost:30081
insecure: true
authType: Pkce
logger:
show-source: true
level: 0
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/docker/go-connections v0.4.0
github.com/enescakir/emoji v1.0.0
github.com/flyteorg/flyteidl v0.19.9
github.com/flyteorg/flytestdlib v0.3.24
github.com/flyteorg/flytestdlib v0.3.28
github.com/ghodss/yaml v1.0.0
github.com/golang/protobuf v1.4.3
github.com/google/go-cmp v0.5.6 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,8 @@ github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4
github.com/flyteorg/flyteidl v0.19.9 h1:1j4/YbV/G1m2hrK017F9K0JYZYxCCwf4qtEkiNnUiEw=
github.com/flyteorg/flyteidl v0.19.9/go.mod h1:576W2ViEyjTpT+kEVHAGbrTP3HARNUZ/eCwrNPmdx9U=
github.com/flyteorg/flytestdlib v0.3.13/go.mod h1:Tz8JCECAbX6VWGwFT6cmEQ+RJpZ/6L9pswu3fzWs220=
github.com/flyteorg/flytestdlib v0.3.24 h1:Eu5TMKch9ihOavPKufgTBI677eVYjJpOAPPg9hfZIzU=
github.com/flyteorg/flytestdlib v0.3.24/go.mod h1:1XG0DwYTUm34Yrffm1Qy9Tdr/pWQypEqTq5dUxw3/cM=
github.com/flyteorg/flytestdlib v0.3.28 h1:bvyldApjlUy9ETxSFpYvLhYLJxxndnMZTf93rVG6a00=
github.com/flyteorg/flytestdlib v0.3.28/go.mod h1:7cDWkY3v7xsoesFcDdu6DSW5Q2U2W5KlHUbUHSwBG1Q=
github.com/form3tech-oss/jwt-go v3.2.2+incompatible h1:TcekIExNqud5crz4xD2pavyTgWiPvpYe4Xau31I0PRk=
github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k=
github.com/franela/goblin v0.0.0-20200105215937-c9ffbefa60db/go.mod h1:7dvUGVsVBjqR7JHJk0brhHOZYGmfBYOrK0ZhYMEtBr4=
Expand Down

0 comments on commit 1571415

Please sign in to comment.