Skip to content

Commit

Permalink
Adding support for multiple paths with formatting in outputs.
Browse files Browse the repository at this point in the history
  • Loading branch information
kristofferahl committed Jan 11, 2024
1 parent cc05119 commit 783122c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 60 deletions.
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
- [x] Validate that ImplicitSources list is unique
- [x] Option to sort output keys (dotenv)
- [x] UI command with web interface that allows comparing results between different exports
- [x] Support multiple paths with formatting for outputs (using --path= should override all paths specified in manifest)

- [ ] (in-progress) Initial round of real world testing

Expand Down
120 changes: 62 additions & 58 deletions internal/command/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,80 +138,84 @@ func Export() *cli.Command {

outputMatched = true

path := o.Path
paths := o.Paths
if p != "" {
path = p
paths = []string{p}
}

if ot == "" && path == "-" {
ctx.Log.Infof("writing to stdout is only allowed when using the --output flag, skipping output %s (alias=%s)", o.Type, o.Alias)
continue
}

filtered := []string{}
filteredValues := make(map[string]string)
for _, s := range keys {
if len(o.Exclude) > 0 && utils.StringSliceContains(o.Exclude, s) {
continue
}
if len(o.Include) > 0 && !utils.StringSliceContains(o.Include, s) {
for _, path := range paths {
if ot == "" && path == "-" {
ctx.Log.Infof("writing to stdout is only allowed when using the --output flag, skipping output %s (alias=%s path=%s)", o.Type, o.Alias, path)
continue
}

v, ok := values[s]
if !ok {
continue
}
path = ctx.Parameters.Replace(path)

switch o.Export {
case config.ExportTypeClearText:
switch v.(type) {
case *api.SensitiveValue:
filtered := []string{}
filteredValues := make(map[string]string)
for _, s := range keys {
if len(o.Exclude) > 0 && utils.StringSliceContains(o.Exclude, s) {
continue
}
case config.ExportTypeSensitive:
switch v.(type) {
case *api.ClearTextValue:
if len(o.Include) > 0 && !utils.StringSliceContains(o.Include, s) {
continue
}
}

filtered = append(filtered, s)
filteredValues[s] = v.Raw()
}
v, ok := values[s]
if !ok {
continue
}

err := func() error {
out := os.Stdout
if path != "" && path != "-" {
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return fmt.Errorf("failed to open file for writing, %v", err)
switch o.Export {
case config.ExportTypeClearText:
switch v.(type) {
case *api.SensitiveValue:
continue
}
case config.ExportTypeSensitive:
switch v.(type) {
case *api.ClearTextValue:
continue
}
}
defer file.Close()
defer file.Sync()
out = file
}
w := bufio.NewWriter(out)
defer w.Flush()

switch out := config.AsOutput(o).(type) {
case output.Dotenv:
ctx.Log.Infof("exporting values as dotenv (alias=%s path=%s quote=%v)", o.Alias, path, out.Quote)
out.Write(w, filtered, o.Map, filteredValues)
case output.Tfvars:
ctx.Log.Infof("exporting values as tfvars (alias=%s path=%s)", o.Alias, path)
out.Write(w, filtered, o.Map, filteredValues)
case output.Json:
ctx.Log.Infof("exporting values as json (alias=%s path=%s)", o.Alias, path)
out.Write(w, filtered, o.Map, filteredValues)
default:
return fmt.Errorf("unsupported output type %s", o.Type)

filtered = append(filtered, s)
filteredValues[s] = v.Raw()
}

return nil
}()
if err != nil {
return err
err := func() error {
out := os.Stdout
if path != "" && path != "-" {
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return fmt.Errorf("failed to open file for writing, %v", err)
}
defer file.Close()
defer file.Sync()
out = file
}
w := bufio.NewWriter(out)
defer w.Flush()

switch out := config.AsOutput(o).(type) {
case output.Dotenv:
ctx.Log.Infof("exporting values as dotenv (alias=%s path=%s quote=%v)", o.Alias, path, out.Quote)
out.Write(w, filtered, o.Map, filteredValues)
case output.Tfvars:
ctx.Log.Infof("exporting values as tfvars (alias=%s path=%s)", o.Alias, path)
out.Write(w, filtered, o.Map, filteredValues)
case output.Json:
ctx.Log.Infof("exporting values as json (alias=%s path=%s)", o.Alias, path)
out.Write(w, filtered, o.Map, filteredValues)
default:
return fmt.Errorf("unsupported output type %s", o.Type)
}

return nil
}()
if err != nil {
return err
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/config/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ type ValueFromAwsParameterStore struct {
type OutputConfig struct {
Type OutputType `yaml:"type"`
Alias string `yaml:"alias"`
Path string `yaml:"path"`
Paths []string `yaml:"paths"`
Map map[string]string `yaml:"map"`
Include []string `yaml:"include"`
Exclude []string `yaml:"exclude"`
Expand Down
2 changes: 1 addition & 1 deletion testdata/racoon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,6 @@ layers:

outputs:
- type: dotenv
path: "-"
paths: ["-"]
config:
quote: false

0 comments on commit 783122c

Please sign in to comment.