Skip to content

Commit

Permalink
Print provider config in provider get, if any
Browse files Browse the repository at this point in the history
We used to only print provider config if there was any on `provider
get`. That's not very user-friendly in case the user enables
auto-registration and then runs provider get to see what had changed.

In the usual case where there's no config, we don't print anything. If
there is configuration, we print key-value pairs for each config option.

The key-value pairs work fine for now since we have a low number of
config options. In the future, once our config gets more complicated, we
should just switch to printing the config as YAML, but for now this
looks awkward and the table gets too vertically long for just one
option.

Fixes: #3816
  • Loading branch information
jhrozek committed Jul 12, 2024
1 parent 0342f5a commit 9840bdc
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions cmd/cli/app/provider/provider_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package provider
import (
"context"
"fmt"
"reflect"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -97,6 +98,10 @@ func GetProviderCommand(ctx context.Context, cmd *cobra.Command, _ []string, con
t.AddRow("Version", p.GetVersion())
t.AddRow("Implements", strings.Join(impls, ", "))
t.AddRow("Auth Flows", strings.Join(afs, ", "))
config := configAsKeyValues(p)
if config != "" {
t.AddRow("Config", config)
}

t.Render()
return nil
Expand All @@ -106,3 +111,47 @@ func GetProviderCommand(ctx context.Context, cmd *cobra.Command, _ []string, con

return nil
}

// mapToKvPairs converts a map to a list of key-value pairs
// TODO(jakub): This works OK now that we have a low-number of config options
// if we have more elaborate configs, we might want to just dump the config as YAML, but for the usual
// case now (1 option..) that would not be very readable
func mapToKvPairs(m map[string]any, parentKey string, result *[]string, nesting int) {
// just in case
if nesting > 10 {
return
}

for key, value := range m {
fullKey := key
if parentKey != "" {
fullKey = parentKey + "." + key
}

v := reflect.ValueOf(value)
switch v.Kind() { // nolint:exhaustive
case reflect.Map:
nestedMap := value.(map[string]any)
mapToKvPairs(nestedMap, fullKey, result, nesting+1)
default:
// this should work for most types, if not, we'll likely just switch to printing YAML
*result = append(*result, fmt.Sprintf("%s=%v", fullKey, value))
}
}
}

func configAsKeyValues(p *minderv1.Provider) string {
if p == nil {
return ""
}

conf := p.GetConfig().AsMap()
if conf == nil {
return ""
}

var result []string
mapToKvPairs(conf, "", &result, 0)

return strings.Join(result, "\n")
}

0 comments on commit 9840bdc

Please sign in to comment.