Skip to content

Commit

Permalink
⭐️ new command to configure the provider build config (#3273)
Browse files Browse the repository at this point in the history
* ⭐️ new command to configure the provider build config

* 🧹 rename to edit-providers
  • Loading branch information
chris-rock authored Feb 12, 2024
1 parent 9cfc122 commit 91d211d
Showing 1 changed file with 62 additions and 15 deletions.
77 changes: 62 additions & 15 deletions providers-sdk/v1/util/configure/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"os/exec"
"regexp"
"slices"
"strconv"
"strings"

Expand All @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 91d211d

Please sign in to comment.