Skip to content

Commit

Permalink
feat: add ability to export selected keys
Browse files Browse the repository at this point in the history
  • Loading branch information
adikari committed Sep 30, 2022
1 parent 3a1d3d3 commit 81ba81e
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 79 deletions.
25 changes: 0 additions & 25 deletions cmd/clean.go

This file was deleted.

40 changes: 37 additions & 3 deletions cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const doubleQuoteSpecialChars = "\\\n\r\"!$`"
var (
exportFormat string
outputFile string
keysToExport []string

exportCmd = &cobra.Command{
Use: "export",
Expand All @@ -30,8 +31,9 @@ var (
)

func init() {
exportCmd.Flags().StringVarP(&exportFormat, "format", "f", "json", "Output format (json, yaml, dotenv)")
exportCmd.Flags().StringVarP(&outputFile, "output-file", "o", "", "Output file (default is standard output)")
exportCmd.Flags().StringVarP(&exportFormat, "format", "f", "json", "output format (json, yaml, dotenv)")
exportCmd.Flags().StringVarP(&outputFile, "output-file", "o", "", "output file (default is standard output)")
exportCmd.Flags().StringSliceVarP(&keysToExport, "key", "k", []string{}, "only export specified config (default is export all)")
exportCmd.MarkFlagFilename("output-file")

rootCmd.AddCommand(exportCmd)
Expand All @@ -50,7 +52,13 @@ func export(cmd *cobra.Command, args []string) error {
return errors.Wrap(err, "failed to instantiate store")
}

configs, err := store.GetMany(config.Configs)
toExport, err := configsToExport(config.Configs)

if err != nil {
return err
}

configs, err := store.GetMany(toExport)

if err != nil {
return errors.Wrap(err, "failed to get params")
Expand Down Expand Up @@ -136,3 +144,29 @@ func doubleQuoteEscape(line string) string {
}
return line
}

func configsToExport(configs []store.ConfigInput) ([]store.ConfigInput, error) {
if len(keysToExport) == 0 {
return configs, nil
}

result := []store.ConfigInput{}

for _, key := range keysToExport {
var found bool

for _, config := range configs {
if config.Key() == key {
found = true
result = append(result, config)
break
}
}

if !found {
return nil, errors.Errorf("key '%s' is not found in safebox config file", key)
}
}

return result, nil
}
25 changes: 0 additions & 25 deletions cmd/get.go

This file was deleted.

33 changes: 33 additions & 0 deletions cmd/import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cmd

import (
"log"

"github.com/spf13/cobra"
)

var (
importFormat string
inputFile string

importCmd = &cobra.Command{
Use: "import",
Short: "Imports all configuration from a file",
RunE: importE,
Example: `TODO: import command example`,
}
)

func init() {
importCmd.Flags().StringVarP(&importFormat, "format", "f", "json", "input format (json, yaml, dotenv)")
importCmd.Flags().StringVarP(&inputFile, "input-file", "i", "", "input file")
importCmd.MarkFlagRequired("input-file")
importCmd.MarkFlagFilename("input-file")

rootCmd.AddCommand(importCmd)
}

func importE(cmd *cobra.Command, args []string) error {
log.Fatalf("not implemented")
return nil
}
4 changes: 2 additions & 2 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ var (
)

func init() {
listCmd.Flags().BoolVarP(&sortByModified, "modified", "m", false, "Sort by modified time")
listCmd.Flags().BoolVarP(&sortByVersion, "version", "v", false, "Sort by version")
listCmd.Flags().BoolVarP(&sortByModified, "modified", "m", false, "sort by modified time")
listCmd.Flags().BoolVarP(&sortByVersion, "version", "v", false, "sort by version")

rootCmd.AddCommand(listCmd)
}
Expand Down
34 changes: 25 additions & 9 deletions cmd/run.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
package cmd

import (
"log"

"github.com/adikari/safebox/v2/store"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

// runCmd represents the exec command
var runCmd = &cobra.Command{
Use: "run",
Short: "Deploys all configurations specified in config file",
RunE: run,
Example: `TODO: run command example`,
}
var (
removeOrphans bool
prompt string

deployCmd = &cobra.Command{
Use: "deploy",
Short: "Deploys all configurations specified in config file",
RunE: deploy,
Example: `TODO: deploy command example`,
}
)

func init() {
rootCmd.AddCommand(runCmd)
rootCmd.AddCommand(deployCmd)
deployCmd.Flags().BoolVarP(&removeOrphans, "remove-orphans", "r", true, "remove orphan configurations")
deployCmd.Flags().StringVarP(&prompt, "prompt", "p", "missing", "prompt for configurations (missing or all)")
}

func run(cmd *cobra.Command, args []string) error {
func deploy(cmd *cobra.Command, args []string) error {
config, err := loadConfig()

if prompt != "all" && prompt != "missing" {
return errors.New("value for prompt must be \"all\" or \"missing\"")
}

if removeOrphans {
log.Panic("remove orphans flag is not implemented")
}

if err != nil {
return errors.Wrap(err, "failed to load config")
}
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func parseConfig(rc rawConfig, c *Config, param LoadParam) {

for key, value := range temp {
c.Configs = append(c.Configs, store.ConfigInput{
Key: key,
Name: key,
Value: value,
Secret: false,
})
Expand Down
6 changes: 3 additions & 3 deletions store/ssmstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (s *SSMStore) Put(input ConfigInput) error {
}

putParameterInput := &ssm.PutParameterInput{
Name: aws.String(input.Key),
Name: aws.String(input.Name),
Type: aws.String(configType),
Value: aws.String(input.Value),
Overwrite: aws.Bool(true),
Expand All @@ -81,7 +81,7 @@ func (s *SSMStore) Delete(config ConfigInput) error {
}

deleteParameterInput := &ssm.DeleteParameterInput{
Name: aws.String(config.Key),
Name: aws.String(config.Name),
}

_, err = s.svc.DeleteParameter(deleteParameterInput)
Expand Down Expand Up @@ -147,7 +147,7 @@ func getNames(configs []ConfigInput) []*string {
var keys []string

for _, value := range configs {
keys = append(keys, value.Key)
keys = append(keys, value.Name)
}

var names []*string
Expand Down
27 changes: 16 additions & 11 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,12 @@ type Config struct {
DataType string
}

func (c *Config) Key() string {
parts := strings.Split(*c.Name, "/")
return parts[len(parts)-1]
}

func (c *Config) Path() string {
parts := strings.Split(*c.Name, "/")
return strings.Join(parts[0:len(parts)-1], "/")
}

const (
SsmProvider = "ssm"
)

type ConfigInput struct {
Key string
Name string
Value string
Secret bool
}
Expand All @@ -56,3 +46,18 @@ func GetStore(provider string) (Store, error) {
return nil, fmt.Errorf("invalid provider `%s`", provider)
}
}

func (c *Config) Key() string {
parts := strings.Split(*c.Name, "/")
return parts[len(parts)-1]
}

func (c *ConfigInput) Key() string {
parts := strings.Split(c.Name, "/")
return parts[len(parts)-1]
}

func (c *Config) Path() string {
parts := strings.Split(*c.Name, "/")
return strings.Join(parts[0:len(parts)-1], "/")
}

0 comments on commit 81ba81e

Please sign in to comment.