Skip to content

Commit

Permalink
feat: load config from file
Browse files Browse the repository at this point in the history
  • Loading branch information
adikari committed Sep 29, 2022
1 parent b057b93 commit bfbbde0
Show file tree
Hide file tree
Showing 8 changed files with 310 additions and 44 deletions.
6 changes: 3 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ var rootCmd = &cobra.Command{
}

func init() {
rootCmd.Flags().StringVarP(&stage, "stage", "s", "", "stage to deploy to")
rootCmd.Flags().StringVarP(&config, "config", "c", "safebox.yml", "path to safebox configuration file")
rootCmd.MarkFlagRequired("stage")
rootCmd.PersistentFlags().StringVarP(&stage, "stage", "s", "", "stage to deploy to")
rootCmd.PersistentFlags().StringVarP(&config, "config", "c", "safebox.yml", "path to safebox configuration file")
rootCmd.MarkPersistentFlagRequired("stage")
}

func Execute(version string) {
Expand Down
14 changes: 12 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"log"

c "github.com/adikari/safebox/v2/config"
"github.com/spf13/cobra"
)
Expand All @@ -10,14 +12,22 @@ var runCmd = &cobra.Command{
Use: "run",
Short: "Deploys all configurations specified in config file",
RunE: execute,
Example: `TODO`,
Example: `TODO: run command example`,
}

func init() {
rootCmd.AddCommand(runCmd)
}

func execute(cmd *cobra.Command, args []string) error {
c.LoadConfig()
params := c.LoadParam{
Path: config,
Stage: stage,
}

config, err := c.Load(params)

log.Printf("%v", config)
log.Printf("%v", err)
return nil
}
86 changes: 64 additions & 22 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,91 @@ package config

import (
"errors"
"fmt"
"io/ioutil"

"gopkg.in/yaml.v2"
)

type ConfigParam struct {
Name string
Description string
Value string
Override map[string]string
Secret bool
Required bool
type rawConfig struct {
Provider string
Service string

Config map[string]map[string]string
}

type Config struct {
type config struct {
Provider string
Service string
Config map[string]string
}

Defaults struct {
Path string
}

Params []ConfigParam
type LoadParam struct {
Path string
Stage string
}

func loadConfig() (*Config, error) {
path := "example/safebox.yml"
yamlFile, err := ioutil.ReadFile(path)
func Load(param LoadParam) (*config, error) {
yamlFile, err := ioutil.ReadFile(param.Path)

config := Config{}
rc := rawConfig{}

if err != nil {
return nil, errors.New("Could not find config file: " + path)
return nil, errors.New("Could not find config file: " + param.Path)
}

err = yaml.Unmarshal(yamlFile, &config)
err = yaml.Unmarshal(yamlFile, &rc)

if err != nil {
return nil, errors.New("Could not parse config file")
}

return &config, nil
c := config{}
c.Service = rc.Service
c.Provider = rc.Provider
parseConfig(rc, &c, param)

return &c, nil
}

func parseConfig(rc rawConfig, c *config, param LoadParam) {
c.Config = map[string]string{}

var defaultConfig map[string]string
var sharedConfig map[string]string
var envConfig map[string]string

for key, value := range rc.Config {
if key == "defaults" {
defaultConfig = value
}

if key == "shared" {
sharedConfig = value
}

if key == param.Stage {
envConfig = value
}
}

for key, value := range defaultConfig {
c.Config[formatKey(param.Stage, c.Service, key)] = value
}

for key, value := range sharedConfig {
c.Config[formatSharedKey(param.Stage, key)] = value
}

for key, value := range envConfig {
c.Config[formatKey(param.Stage, c.Service, key)] = value
}
}

func formatSharedKey(stage string, key string) string {
return fmt.Sprintf("/%s/shared/%s", stage, key)
}

func GetConfig() []ConfigParam {
return nil
func formatKey(stage string, service string, key string) string {
return fmt.Sprintf("/%s/%s/%s", stage, service, key)
}
29 changes: 12 additions & 17 deletions example/safebox.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
service: safebox
provider: ssm

defaults:
path: /${STAGE}/gateway/
config:
defaults:
DB_NAME: "database name"
API_ENDPOINT: "endpoint"

params:
- name: DB_NAME
description: 'database name'
value: database-name
override:
production: production-database-name
staging: staging-database-name
prod:
DB_NAME: "production db name"

- name: DB_PASSWORD
description: 'database password'
secret: true
dev:
DB_NAME: "dev db name"

shared:
SHARED_KEY: "shared key"

- name: DB_PORT
required: false

- name: DATADOG_API_KEY
path: /shared/
secret: true
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ module github.com/adikari/safebox/v2
go 1.19

require (
github.com/aws/aws-sdk-go v1.44.107
github.com/spf13/cobra v1.5.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
19 changes: 19 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
github.com/aws/aws-sdk-go v1.44.107 h1:VP7Rq3wzsOV7wrfHqjAAKRksD4We58PaoVSDPKhm8nw=
github.com/aws/aws-sdk-go v1.44.107/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU=
github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
Loading

0 comments on commit bfbbde0

Please sign in to comment.