Skip to content

Commit

Permalink
fix: enable setting idp-protocol via app config
Browse files Browse the repository at this point in the history
Previously it was not possible to set the `idp-protocol` default via
the app config. This has been changed so that a default can be set.
  • Loading branch information
richardcase committed Sep 28, 2020
1 parent 6fc73be commit 91c5a15
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
21 changes: 12 additions & 9 deletions internal/commands/use/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,20 @@ func cmdSetup(cmd *cobra.Command, args []string, params *app.UseParams, logger *
return err
}

idpProtocol := findIdpProtocolFromArgs(args)
idpProtocol, err := getIdpProtocol(args, params)
if err != nil {
return fmt.Errorf("getting idp-protocol: %w", err)
}
if idpProtocol == "" {
return ErrMissingIdpProtocol
}
idProvider, err := provider.GetIdentityProvider(idpProtocol)
if err != nil {
return fmt.Errorf("creating identity provider %s: %w", idpProtocol, err)
}
if err := params.Context.ConfigurationItems().SetValue("idp-protocol", idpProtocol); err != nil {
return fmt.Errorf("setting idp-protocol value: %w", err)
}
params.IdentityProvider = idProvider
logger.Infof("using identity provider %s", idProvider.Name())
idProviderCfg := idProvider.ConfigurationItems()
Expand Down Expand Up @@ -186,19 +192,16 @@ func preRun(params *app.UseParams, logger *logrus.Entry) error {
return params.Provider.ConfigurationResolver().Resolve(params.Context.ConfigurationItems())
}

func findIdpProtocolFromArgs(args []string) string {
index := -1
func getIdpProtocol(args []string, params *app.UseParams) (string, error) {
// look for a flag first
for i, arg := range args {
if arg == "--idp-protocol" {
index = i
break
return args[i+1], nil
}
}
if index == -1 {
return ""
}

return args[index+1]
// look in app config
return config.GetValue("idp-protocol", params.Provider.Name())
}

func isInteractive(cs config.ConfigurationSet) bool {
Expand Down
29 changes: 29 additions & 0 deletions pkg/config/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,35 @@ func NewAppConfigurationWithPath(path string) (AppConfiguration, error) {
return &appConfiguration{path: configPath}, nil
}

func GetValue(name string, provider string) (string, error) {
appCfg, err := NewAppConfiguration()
if err != nil {
return "", fmt.Errorf("creating application configuration: %w", err)
}

cfg, err := appCfg.Get()
if err != nil {
return "", fmt.Errorf("getting application configuration: %w", err)
}

if provider != "" {
providerValues, hasProvider := cfg.Spec.Providers[provider]
if hasProvider {
providerVal, hasProviderVal := providerValues[name]
if hasProviderVal {
return providerVal, nil
}
}
}

globalVal, hasGlobalVal := cfg.Spec.Global[name]
if hasGlobalVal {
return globalVal, nil
}

return "", nil
}

type appConfiguration struct {
path string
}
Expand Down

0 comments on commit 91c5a15

Please sign in to comment.