Skip to content

Commit

Permalink
refactor: move getJsonForInputParams to parameters.go
Browse files Browse the repository at this point in the history
  • Loading branch information
pda committed Aug 28, 2018
1 parent aaf0c35 commit a22b976
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 43 deletions.
44 changes: 1 addition & 43 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
)

// see Makefile
Expand Down Expand Up @@ -72,45 +70,5 @@ func main() {
}

func getJsonForInput(input *Input) ([]byte, error) {
if err := parseParameters(input); err != nil {
return nil, err
}

specs, err := parseTemplate(input.TemplateBody)
if err != nil {
return nil, err
}

if err := validateParameters(input.Parameters, specs); err != nil {
return nil, err
}

items := []ParameterItem{}
missingNames := []string{}
for _, spec := range specs {
if value, ok := input.Parameters[spec.Name]; ok {
// specified in parameters
items = append(items, ParameterItem{
ParameterKey: spec.Name,
ParameterValue: value,
})
} else if input.AcceptDefaults && spec.HasDefault {
// has default; do not override
continue
} else if !input.NoPrevious {
// use previous value
items = append(items, ParameterItem{
ParameterKey: spec.Name,
UsePreviousValue: true,
})
} else {
missingNames = append(missingNames, spec.Name)
}
}

if len(missingNames) > 0 {
return nil, fmt.Errorf("missing parameters: %s", strings.Join(missingNames, ", "))
}

return json.MarshalIndent(items, "", " ")
return getJsonForInputParams(input) // parameters.go
}
44 changes: 44 additions & 0 deletions parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,50 @@ func (t *parameterStoreUnmarshaler) UnmarshalYAMLTag(tag string, fieldValue refl
return reflect.ValueOf(value)
}

func getJsonForInputParams(input *Input) ([]byte, error) {
if err := parseParameters(input); err != nil {
return nil, err
}

specs, err := parseTemplate(input.TemplateBody)
if err != nil {
return nil, err
}

if err := validateParameters(input.Parameters, specs); err != nil {
return nil, err
}

items := []ParameterItem{}
missingNames := []string{}
for _, spec := range specs {
if value, ok := input.Parameters[spec.Name]; ok {
// specified in parameters
items = append(items, ParameterItem{
ParameterKey: spec.Name,
ParameterValue: value,
})
} else if input.AcceptDefaults && spec.HasDefault {
// has default; do not override
continue
} else if !input.NoPrevious {
// use previous value
items = append(items, ParameterItem{
ParameterKey: spec.Name,
UsePreviousValue: true,
})
} else {
missingNames = append(missingNames, spec.Name)
}
}

if len(missingNames) > 0 {
return nil, fmt.Errorf("missing parameters: %s", strings.Join(missingNames, ", "))
}

return json.MarshalIndent(items, "", " ")
}

func parseParameters(input *Input) error {
input.Parameters = make(map[string]string)

Expand Down

0 comments on commit a22b976

Please sign in to comment.