diff --git a/providers-sdk/v1/util/configure/configure.go b/providers-sdk/v1/util/configure/configure.go index 48b13a27eb..2123ea8f8d 100644 --- a/providers-sdk/v1/util/configure/configure.go +++ b/providers-sdk/v1/util/configure/configure.go @@ -10,6 +10,7 @@ import ( "os" "os/exec" "regexp" + "slices" "strconv" "strings" @@ -24,6 +25,24 @@ type ProvidersConf struct { Builtin []string `json:"builtin"` } +func init() { + rootCmd.Flags().StringP("file", "f", "providers.yaml", "config file for providers") + rootCmd.Flags().StringP("output", "o", "providers/builtin_dev.go", "output go-file for builtin dev providers") + + editProvidersCmd.Flags().StringP("file", "f", "providers.yaml", "config file for providers") + rootCmd.AddCommand(editProvidersCmd) +} + +func main() { + logger.CliCompactLogger(logger.LogOutputWriter) + zerolog.SetGlobalLevel(zerolog.DebugLevel) + + if err := rootCmd.Execute(); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} + var rootCmd = &cobra.Command{ Use: "configure [-f config] [-o file]", Short: "configure providers for cnquery", @@ -66,6 +85,49 @@ var rootCmd = &cobra.Command{ }, } +var editProvidersCmd = &cobra.Command{ + Use: "edit-providers NAME1 NAME2 [-f config]", + Short: "adds a provider to the config", + Run: func(cmd *cobra.Command, args []string) { + confPath, err := cmd.Flags().GetString("file") + if err != nil { + log.Fatal().Err(err).Msg("Can't get --file") + } + + raw, err := os.ReadFile(confPath) + if err != nil { + log.Fatal().Err(err).Str("path", confPath).Msg("failed to read config file") + } + + var conf ProvidersConf + err = yaml.Unmarshal(raw, &conf) + if err != nil { + log.Fatal().Err(err).Str("path", confPath).Msg("failed to parse config file") + } + + if len(conf.Builtin) > 0 { + log.Warn().Strs("providers", conf.Builtin).Msg("overwrite existing providers in config") + } + + // set new providers + conf.Builtin = args + slices.Sort(conf.Builtin) + conf.Builtin = slices.Compact(conf.Builtin) + + log.Info().Strs("providers", conf.Builtin).Msg("configured providers") + + raw, err = yaml.Marshal(conf) + if err != nil { + log.Fatal().Err(err).Str("path", confPath).Msg("failed to marshal updated config") + } + + err = os.WriteFile(confPath, raw, 0700) + if err != nil { + log.Fatal().Err(err).Str("path", confPath).Msg("failed to write config file") + } + }, +} + func genBuiltinGo(conf ProvidersConf) ([]byte, error) { var imports string var infos string @@ -202,18 +264,3 @@ func rewireDependencies(providers []string) { log.Fatal().Err(err).Msg("failed to go mod tidy") } } - -func init() { - rootCmd.Flags().StringP("file", "f", "providers.yaml", "config file for providers") - rootCmd.Flags().StringP("output", "o", "providers/builtin_dev.go", "output go-file for builtin dev providers") -} - -func main() { - logger.CliCompactLogger(logger.LogOutputWriter) - zerolog.SetGlobalLevel(zerolog.DebugLevel) - - if err := rootCmd.Execute(); err != nil { - fmt.Fprintln(os.Stderr, err) - os.Exit(1) - } -}