From 38d4a028dbe244656ce63553cd0605e5e48608da Mon Sep 17 00:00:00 2001 From: Azanul Haque <42029519+Azanul@users.noreply.github.com> Date: Sat, 6 Apr 2024 15:26:47 +0530 Subject: [PATCH] Create config utils (#1398) * feat: mark successful triggering of workflow even without any supported resources Signed-off-by: Azanul * feat: engines entry Signed-off-by: Azanul * feat: reduce code duplication Signed-off-by: Azanul * feat: update codeowners Signed-off-by: Azanul * feat: Azure cost PoC Signed-off-by: Azanul * feat: log schema setup err Signed-off-by: Azanul * feat: log usage err Signed-off-by: Azanul * fix: aws config env vars support Signed-off-by: Azanul * feat: log schema setup err Signed-off-by: Azanul * feat: separate create config Signed-off-by: Azanul * feat: use util to create config Signed-off-by: Azanul --------- Signed-off-by: Azanul --- cmd/config.go | 28 +++------------------------- cmd/start.go | 5 +++-- handlers/helper.go | 6 +++--- internal/config/create.go | 38 ++++++++++++++++++++++++++++++++++++++ internal/config/load.go | 9 ++++----- 5 files changed, 51 insertions(+), 35 deletions(-) create mode 100644 internal/config/create.go diff --git a/cmd/config.go b/cmd/config.go index f50eebdb2..5fd8d1e77 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -1,12 +1,10 @@ package cmd import ( - "os" + "log" - "github.com/BurntSushi/toml" - log "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "github.com/tailwarden/komiser/models" + "github.com/tailwarden/komiser/internal/config" ) var configCmd = &cobra.Command{ @@ -14,30 +12,10 @@ var configCmd = &cobra.Command{ Short: "Create configuration file", Long: ``, Run: func(cmd *cobra.Command, args []string) { - c := models.Config{ - AWS: []models.AWSConfig{ - { - Name: "Demo", - Source: "CREDENTIALS_FILE", - Profile: "default", - }, - }, - SQLite: models.SQLiteConfig{ - File: "komiser.db", - }, - } - - f, err := os.Create("config.toml") + err := config.Create(nil) if err != nil { log.Fatal(err) } - if err := toml.NewEncoder(f).Encode(c); err != nil { - log.Fatal(err) - } - if err := f.Close(); err != nil { - log.Fatal(err) - - } }, } diff --git a/cmd/start.go b/cmd/start.go index d88d83319..5d00dc4f7 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -10,6 +10,7 @@ import ( log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/tailwarden/komiser/internal" + "github.com/tailwarden/komiser/internal/config" "github.com/tailwarden/komiser/utils" ) @@ -38,8 +39,8 @@ var startCmd = &cobra.Command{ if _, err := os.Stat(filename); errors.Is(err, os.ErrNotExist) { log.Info("unable to use given config file:", err) - log.Info("Creating default config.toml") - err = os.WriteFile("config.toml", []byte{}, 0644) + log.Info("Creating default config file:", config.DefaultFileName) + err = config.Create(nil) if err != nil { return err } diff --git a/handlers/helper.go b/handlers/helper.go index e26ccfbd9..9fc840ac9 100644 --- a/handlers/helper.go +++ b/handlers/helper.go @@ -189,7 +189,7 @@ func makeClientFromAccount(account models.Account) (*providers.ProviderClient, e Name: account.Name, }, err } - } else if account.Credentials["source"] == "environment-variables" { + } else { cfg, err := awsConfig.LoadDefaultConfig(context.Background()) if err != nil { return nil, err @@ -370,8 +370,8 @@ func populateConfigFromAccount(account models.Account, config *models.Config) er switch account.Provider { case "aws": awsConfig := models.AWSConfig{ - Name: account.Name, - Source: account.Credentials["source"], + Name: account.Name, + Source: account.Credentials["source"], } if account.Credentials["source"] == "credentials-file" { awsConfig.Profile = account.Credentials["profile"] diff --git a/internal/config/create.go b/internal/config/create.go new file mode 100644 index 000000000..1ac780e6d --- /dev/null +++ b/internal/config/create.go @@ -0,0 +1,38 @@ +package config + +import ( + "os" + + "github.com/BurntSushi/toml" + "github.com/tailwarden/komiser/models" +) + +const DefaultFileName = "config.toml" + +var demoConfig = models.Config{ + AWS: []models.AWSConfig{ + { + Name: "Demo", + Source: "CREDENTIALS_FILE", + Profile: "default", + }, + }, + SQLite: models.SQLiteConfig{ + File: "komiser.db", + }, +} + +func Create(c *models.Config) error { + if c == nil { + c = &demoConfig + } + + f, err := os.Create(DefaultFileName) + if err != nil { + return err + } + if err := toml.NewEncoder(f).Encode(*c); err != nil { + return err + } + return f.Close() +} diff --git a/internal/config/load.go b/internal/config/load.go index 9bef16fd5..aadbfc8e9 100644 --- a/internal/config/load.go +++ b/internal/config/load.go @@ -21,7 +21,6 @@ import ( "github.com/ovh/go-ovh/ovh" "github.com/scaleway/scaleway-sdk-go/scw" "github.com/tailwarden/komiser/models" - . "github.com/tailwarden/komiser/models" "github.com/tailwarden/komiser/providers" "github.com/tailwarden/komiser/utils" tccommon "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common" @@ -36,7 +35,7 @@ import ( "k8s.io/client-go/tools/clientcmd" ) -func loadConfigFromFile(path string) (*Config, error) { +func loadConfigFromFile(path string) (*models.Config, error) { filename, err := filepath.Abs(path) if err != nil { return nil, err @@ -54,8 +53,8 @@ func loadConfigFromFile(path string) (*Config, error) { return loadConfigFromBytes(yamlFile) } -func loadConfigFromBytes(b []byte) (*Config, error) { - var config Config +func loadConfigFromBytes(b []byte) (*models.Config, error) { + var config models.Config err := toml.Unmarshal([]byte(b), &config) if err != nil { @@ -65,7 +64,7 @@ func loadConfigFromBytes(b []byte) (*Config, error) { return &config, nil } -func Load(configPath string, telemetry bool, analytics utils.Analytics, db *bun.DB) (*Config, []providers.ProviderClient, []models.Account, error) { +func Load(configPath string, telemetry bool, analytics utils.Analytics, db *bun.DB) (*models.Config, []providers.ProviderClient, []models.Account, error) { config, err := loadConfigFromFile(configPath) if err != nil { return nil, nil, nil, err