github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/cmdconfig/diagnostics.go (about) 1 package cmdconfig 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "os" 7 "sort" 8 "strings" 9 10 "github.com/spf13/viper" 11 "github.com/turbot/go-kit/helpers" 12 "github.com/turbot/steampipe/pkg/constants" 13 "github.com/turbot/steampipe/pkg/error_helpers" 14 ) 15 16 // DisplayConfig prints all config set via WorkspaceProfile or HCL options 17 func DisplayConfig() { 18 diagnostics, ok := os.LookupEnv(constants.EnvConfigDump) 19 if !ok { 20 // shouldn't happen 21 return 22 } 23 diagnostics = strings.ToLower(diagnostics) 24 configFormats := []string{"config", "config_json"} 25 if !helpers.StringSliceContains(configFormats, diagnostics) { 26 error_helpers.ShowWarning("invalid value for STEAMPIPE_CONFIG_DUMP, expected values: config,config_json") 27 return 28 } 29 30 var configArgNames = viper.AllKeys() 31 res := make(map[string]interface{}, len(configArgNames)) 32 33 maxLength := 0 34 for _, a := range configArgNames { 35 if l := len(a); l > maxLength { 36 maxLength = l 37 } 38 res[a] = viper.Get(a) 39 } 40 41 switch diagnostics { 42 case "config": 43 // write config lines into array then sort them 44 lines := make([]string, len(res)) 45 idx := 0 46 fmtStr := `%-` + fmt.Sprintf("%d", maxLength) + `s: %v` + "\n" 47 for k, v := range res { 48 lines[idx] = fmt.Sprintf(fmtStr, k, v) 49 idx++ 50 } 51 sort.Strings(lines) 52 53 var b strings.Builder 54 b.WriteString("\n================\nSteampipe Config\n================\n\n") 55 56 for _, line := range lines { 57 b.WriteString(line) 58 } 59 fmt.Println(b.String()) 60 case "config_json": 61 // iterate once more for the non-serializable values 62 for k, v := range res { 63 if _, err := json.Marshal(v); err != nil { 64 res[k] = fmt.Sprintf("%v", v) 65 } 66 } 67 jsonBytes, err := json.MarshalIndent(res, "", " ") 68 error_helpers.FailOnError(err) 69 fmt.Println(string(jsonBytes)) 70 } 71 72 }