Skip to content

Commit

Permalink
feat: add ability to get store
Browse files Browse the repository at this point in the history
  • Loading branch information
adikari committed Sep 30, 2022
1 parent bfbbde0 commit fcab318
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 39 deletions.
41 changes: 35 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
package cmd

import (
"fmt"
"os"
"strings"

c "github.com/adikari/safebox/v2/config"
"github.com/adikari/safebox/v2/store"
"github.com/spf13/cobra"
)

var (
stage string
config string
stage string
pathToConfig string
Config *c.Config
)

var rootCmd = &cobra.Command{
Use: "safebox",
Short: "SafeBox is a secret manager CLI program",
Long: `A Fast and Flexible secret manager built with love by adikari in Go.`,
Use: "safebox",
Short: "SafeBox is a secret manager CLI program",
Long: `A Fast and Flexible secret manager built with love by adikari in Go.`,
PersistentPreRun: prerun,
Run: func(cmd *cobra.Command, args []string) {
cmd.Usage()
},
}

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

Expand All @@ -34,6 +39,30 @@ func Execute(version string) {
if strings.Contains(err.Error(), "arg(s)") || strings.Contains(err.Error(), "usage") {
cmd.Usage()
}

os.Exit(1)
}
}

func prerun(cmd *cobra.Command, args []string) {
c, _ := loadConfig()
Config = c
}

func loadConfig() (*c.Config, error) {
params := c.LoadParam{
Path: pathToConfig,
Stage: stage,
}

return c.Load(params)
}

func getStore() (store.Store, error) {
switch Config.Provider {
case "ssm":
return store.NewSSMStore()
default:
return nil, fmt.Errorf("invalid provider `%s`", Config.Provider)
}
}
11 changes: 2 additions & 9 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"log"

c "github.com/adikari/safebox/v2/config"
"github.com/spf13/cobra"
)

Expand All @@ -20,14 +19,8 @@ func init() {
}

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

config, err := c.Load(params)

log.Printf("%v", config)
_, err := getStore()
log.Printf("%v", Config)
log.Printf("%v", err)
return nil
}
32 changes: 9 additions & 23 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type rawConfig struct {
Config map[string]map[string]string
}

type config struct {
type Config struct {
Provider string
Service string
Config map[string]string
Expand All @@ -26,49 +26,35 @@ type LoadParam struct {
Stage string
}

func Load(param LoadParam) (*config, error) {
func Load(param LoadParam) (*Config, error) {
yamlFile, err := ioutil.ReadFile(param.Path)

rc := rawConfig{}

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

rc := rawConfig{}

err = yaml.Unmarshal(yamlFile, &rc)

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

c := config{}
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) {
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
}
}
defaultConfig := rc.Config["defaults"]
sharedConfig := rc.Config["shared"]
envConfig := rc.Config[param.Stage]

for key, value := range defaultConfig {
c.Config[formatKey(param.Stage, c.Service, key)] = value
Expand Down
2 changes: 1 addition & 1 deletion store/ssmstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type SSMStore struct {
svc ssmiface.SSMAPI
}

func NewSSMStore(numRetries int) (*SSMStore, error) {
func NewSSMStore() (*SSMStore, error) {
ssmSession := session.Must(session.NewSession())

svc := ssm.New(ssmSession)
Expand Down

0 comments on commit fcab318

Please sign in to comment.