Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding escape chars #6

Merged
merged 4 commits into from
Sep 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion cfn-clone/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ func main() {

fmt.Println("Going to clone")

createStack(options.NewName, parameters, newTemplate)
output, err := createStack(options.NewName, parameters, newTemplate)
if err != nil {
fmt.Printf("Received error '%s' with output '%s'.\n", err.Error(), output)
os.Exit(1)
}

fmt.Printf("Success with output '%s'.\n", output)
os.Exit(0)
}
27 changes: 23 additions & 4 deletions cfn-clone/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type describeStackResponse struct {
}
}

func createStackCmd(name string, params map[string]string, template string) []string {
func createStackCmd(name string, params map[string]string, template string) ([]string, error) {
cmd := []string{
"aws",
"cloudformation",
Expand All @@ -28,14 +28,22 @@ func createStackCmd(name string, params map[string]string, template string) []st
name,
"--template-body",
"file:///" + template,
"--capabilities", "CAPABILITY_IAM",
"--parameters",
}

return append(cmd, cliParamsForCreate(params)...)
if err := noEchoParamsOverriden(params); err != nil {
return []string{}, err
}

return append(cmd, cliParamsForCreate(params)...), nil
}

func createStack(name string, params map[string]string, template string) (string, error) {
createCmd := createStackCmd(name, params, template)
createCmd, err := createStackCmd(name, params, template)
if err != nil {
return "", err
}

fmt.Println("Going to run with command:")
fmt.Printf("%s\n", strings.Join(createCmd, " "))
Expand Down Expand Up @@ -78,10 +86,21 @@ func newStackTemplateFile(sourceStack string, path string) (string, error) {
return f.Name(), nil
}

func noEchoParamsOverriden(params map[string]string) error {
for k, v := range params {
if v == "****" {
return fmt.Errorf("NoEcho Paramater '%s' must have overrid value specified.", k)
}
}
return nil
}

func cliParamsForCreate(params map[string]string) []string {
p := []string{}
for k, v := range params {
p = append(p, "ParameterKey="+k+",ParameterValue="+v)
escapedK := strings.Replace(k, ",", "\\,", -1)
escapedV := strings.Replace(v, ",", "\\,", -1)
p = append(p, "ParameterKey="+escapedK+",ParameterValue=\""+escapedV+"\"")
}

return p
Expand Down
16 changes: 9 additions & 7 deletions cfn-clone/stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (

func TestCliParamsForCreate(t *testing.T) {
expected := []string{
"ParameterKey=foo,ParameterValue=bar",
"ParameterKey=bar,ParameterValue=baz",
"ParameterKey=foo,ParameterValue=\"bar\\,1\"",
"ParameterKey=bar,ParameterValue=\"2\\,baz\"",
}
params := map[string]string{"foo": "bar", "bar": "baz"}
params := map[string]string{"foo": "bar,1", "bar": "2,baz"}
result := cliParamsForCreate(params)

if !reflect.DeepEqual(result, expected) {
Expand All @@ -22,7 +22,7 @@ func TestCliParamsForCreate(t *testing.T) {

func TestCreateStackCmd(t *testing.T) {
name := "foo"
params := map[string]string{"param1": "val1", "param2": "val2"}
params := map[string]string{"param1": "val1,valx", "param2": "val2,valy"}
template := "/var/tmp/new_template.json"

expected := []string{
Expand All @@ -33,12 +33,14 @@ func TestCreateStackCmd(t *testing.T) {
name,
"--template-body",
"file:///" + template,
"--capabilities",
"CAPABILITY_IAM",
"--parameters",
"ParameterKey=param1,ParameterValue=val1",
"ParameterKey=param2,ParameterValue=val2",
"ParameterKey=param1,ParameterValue=\"val1\\,valx\"",
"ParameterKey=param2,ParameterValue=\"val2\\,valy\"",
}

cmd := createStackCmd(name, params, template)
cmd, _ := createStackCmd(name, params, template)

// because order of maps are not guaranteed
if !reflect.DeepEqual(cmd[:8], expected[:8]) {
Expand Down