Skip to content

Commit

Permalink
Create config utils (#1398)
Browse files Browse the repository at this point in the history
* feat: mark successful triggering of workflow even without any supported resources

Signed-off-by: Azanul <azanulhaque@gmail.com>

* feat: engines entry

Signed-off-by: Azanul <azanulhaque@gmail.com>

* feat: reduce code duplication

Signed-off-by: Azanul <azanulhaque@gmail.com>

* feat: update codeowners

Signed-off-by: Azanul <azanulhaque@gmail.com>

* feat: Azure cost PoC

Signed-off-by: Azanul <azanulhaque@gmail.com>

* feat: log schema setup err

Signed-off-by: Azanul <azanulhaque@gmail.com>

* feat: log usage err

Signed-off-by: Azanul <azanulhaque@gmail.com>

* fix: aws config env vars support

Signed-off-by: Azanul <azanulhaque@gmail.com>

* feat: log schema setup err

Signed-off-by: Azanul <azanulhaque@gmail.com>

* feat: separate create config

Signed-off-by: Azanul <azanulhaque@gmail.com>

* feat: use util to create config

Signed-off-by: Azanul <azanulhaque@gmail.com>

---------

Signed-off-by: Azanul <azanulhaque@gmail.com>
  • Loading branch information
Azanul authored Apr 6, 2024
1 parent 5260f40 commit 38d4a02
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 35 deletions.
28 changes: 3 additions & 25 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,21 @@
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{
Use: "config",
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)

}
},
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions handlers/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"]
Expand Down
38 changes: 38 additions & 0 deletions internal/config/create.go
Original file line number Diff line number Diff line change
@@ -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()
}
9 changes: 4 additions & 5 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 38d4a02

Please sign in to comment.