Skip to content

Commit c1ee3df

Browse files
authored
feat: migrate to governor-extension-sdk and add WIF (#311)
1 parent 70a171a commit c1ee3df

16 files changed

Lines changed: 584 additions & 1000 deletions

cmd/root.go

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,22 @@ package cmd
44
import (
55
"strings"
66

7-
"github.com/metal-toolbox/governor-api/pkg/configs"
7+
sdkcfg "github.com/metal-toolbox/governor-extension-sdk/pkg/configs"
88
homedir "github.com/mitchellh/go-homedir"
99
"github.com/spf13/cobra"
10-
"github.com/spf13/pflag"
1110
"github.com/spf13/viper"
1211
"go.uber.org/zap"
12+
13+
"github.com/metal-toolbox/gov-okta-addon/internal/configs"
1314
)
1415

1516
const (
16-
appName = "gov-okta-addon"
17-
defaultIAMRuntimeTimeoutSeconds = 15
17+
appName = "gov-okta-addon"
1818
)
1919

2020
var (
21-
cfgFile string
22-
logger *zap.SugaredLogger
23-
appConfig configs.Configs
21+
cfgFile string
22+
logger *zap.SugaredLogger
2423
)
2524

2625
// rootCmd represents the base command when called without any subcommands
@@ -39,13 +38,17 @@ func Execute() {
3938
func init() {
4039
cobra.OnInitialize(initConfig)
4140

42-
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.gov-okta-addon.yaml)")
41+
v := viper.GetViper()
42+
flags := rootCmd.PersistentFlags()
4343

44-
rootCmd.PersistentFlags().Bool("debug", false, "enable debug logging")
45-
viperBindFlag("logging.debug", rootCmd.PersistentFlags().Lookup("debug"))
44+
flags.StringVar(&cfgFile, "config", "", "config file (default is $HOME/."+appName+".yaml)")
4645

47-
rootCmd.PersistentFlags().Bool("pretty", false, "enable pretty (human readable) logging output")
48-
viperBindFlag("logging.pretty", rootCmd.PersistentFlags().Lookup("pretty"))
46+
sdkcfg.MustLoggingFlags(v, flags)
47+
sdkcfg.MustTracingFlags(v, flags)
48+
sdkcfg.MustAuditFlags(v, flags)
49+
sdkcfg.MustGovernorFlags(v, flags)
50+
configs.MustOktaFlags(v, flags)
51+
configs.MustAddonFlags(v, flags)
4952
}
5053

5154
// initConfig reads in config file and ENV variables if set.
@@ -60,7 +63,7 @@ func initConfig() {
6063

6164
// Search config in home directory with name ".gov-okta-addon" (without extension).
6265
viper.AddConfigPath(home)
63-
viper.SetConfigName(".gov-okta-addon")
66+
viper.SetConfigName("." + appName)
6467
}
6568

6669
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
@@ -77,7 +80,7 @@ func initConfig() {
7780
)
7881
}
7982

80-
if err := viper.Unmarshal(&appConfig); err != nil {
83+
if err := viper.Unmarshal(&configs.AppConfig); err != nil {
8184
logger.Fatalw("failed to unmarshal config", "error", err)
8285
}
8386
}
@@ -99,13 +102,6 @@ func setupLogging() {
99102
panic(err)
100103
}
101104

102-
logger = l.Sugar().With("app", "gov-okta-addon")
105+
logger = l.Sugar().With("app", appName)
103106
defer logger.Sync() //nolint:errcheck
104107
}
105-
106-
// viperBindFlag provides a wrapper around the viper bindings that handles error checks
107-
func viperBindFlag(name string, flag *pflag.Flag) {
108-
if err := viper.BindPFlag(name, flag); err != nil {
109-
panic(err)
110-
}
111-
}

0 commit comments

Comments
 (0)