Skip to content

Commit

Permalink
Named Mode Args #84
Browse files Browse the repository at this point in the history
Added support for specifying mode arguments via flags
  • Loading branch information
Maksymilian Wojczuk committed Feb 28, 2018
1 parent aecc242 commit f0798b2
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 28 deletions.
91 changes: 71 additions & 20 deletions cliparser/cliparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,32 @@ func ParseCliArguments(args []string) (cliArguments CliArguments, err error) {
configurationPath = app.Flag("config", "A path to the configuration file").Short('c').String()
showProgress = app.Flag("progress", "Show progress of stack creation. Option available only after setting up a remote sink").Bool()

onlineValidate = app.Command(ValidateMode, "Online template Validation")
onlineValidateTemplate = onlineValidate.Arg("template", "A path to the template file.").Required().String()
onlineValidate = app.Command(ValidateMode, "Online template Validation")
onlineValidateTemplate = onlineValidate.Arg("template", "A path to the template file.").String()
onlineValidateImpTemplate = onlineValidate.Flag("template", "A path to the template file.").String()

offlineValidate = app.Command(OfflineValidateMode, "Offline Template Validation")
offlineValidateTemplate = offlineValidate.Arg("template", "A path to the template file.").Required().String()
offlineValidate = app.Command(OfflineValidateMode, "Offline Template Validation")
offlineValidateTemplate = offlineValidate.Arg("template", "A path to the template file.").String()
offlineValidateImpTemplate = offlineValidate.Flag("template", "A path to the template file.").String()

convert = app.Command(ConvertMode, "Convertion between JSON and YAML of template files")
convertTemplate = convert.Arg("template", "A path to the template file.").Required().String()
convertOutputFile = convert.Arg("output", "A path where converted file will be saved.").Required().String()
prettyPrint = convert.Flag("pretty-print", "Pretty printing JSON").Bool()
convert = app.Command(ConvertMode, "Convertion between JSON and YAML of template files")
convertTemplate = convert.Arg("template", "A path to the template file.").String()
convertOutputFile = convert.Arg("output", "A path where converted file will be saved.").String()
convertImpTemplate = convert.Flag("from", "A path to the template file.").String()
convertImpOutputFile = convert.Flag("to", "A path where converted file will be saved.").String()
prettyPrint = convert.Flag("pretty-print", "Pretty printing JSON").Bool()

configure = app.Command(ConfigureMode, "Create your own configuration mode")

createStack = app.Command(CreateStackMode, "Creates a stack on aws")
createStackName = createStack.Arg("stack", "An AWS stack name.").Required().String()
createStackTemplate = createStack.Arg("template", "A path to the template file.").Required().String()
createStack = app.Command(CreateStackMode, "Creates a stack on aws")
createStackName = createStack.Arg("stack", "An AWS stack name.").String()
createStackTemplate = createStack.Arg("template", "A path to the template file.").String()
createStackImpName = createStack.Flag("stack", "Sn AWS stack name.").String()
createStackImpTemplate = createStack.Flag("template", "A path to the template file.").String()

deleteStack = app.Command(DestroyStackMode, "Deletes a stack on aws")
deleteStackName = deleteStack.Arg("stack", "An AWS stack name.").Required().String()
deleteStack = app.Command(DestroyStackMode, "Deletes a stack on aws")
deleteStackName = deleteStack.Arg("stack", "An AWS stack name.").String()
deleteStackImpName = deleteStack.Flag("stack", "An AWS stack name.").String()

setupSink = app.Command(SetupSinkMode, "Sets up resources required for progress report on stack events (SNS Topic, SQS Queue and SQS Queue Policy)")

Expand All @@ -103,34 +110,78 @@ func ParseCliArguments(args []string) (cliArguments CliArguments, err error) {
//online validate
case onlineValidate.FullCommand():
cliArguments.Mode = &ValidateMode
cliArguments.TemplatePath = onlineValidateTemplate
if len(*onlineValidateTemplate) > 0 {
cliArguments.TemplatePath = onlineValidateTemplate
} else if len(*onlineValidateImpTemplate) > 0 {
cliArguments.TemplatePath = onlineValidateImpTemplate
} else {
err = errors.New("You have to specify the template, try --help")
return
}

// offline validation
case offlineValidate.FullCommand():
cliArguments.Mode = &OfflineValidateMode
cliArguments.TemplatePath = offlineValidateTemplate
if len(*offlineValidateTemplate) > 0 {
cliArguments.TemplatePath = offlineValidateTemplate
} else if len(*offlineValidateImpTemplate) > 0 {
cliArguments.TemplatePath = offlineValidateImpTemplate
} else {
err = errors.New("You have to specify the template, try --help")
return
}

// convert
case convert.FullCommand():
cliArguments.Mode = &ConvertMode
cliArguments.TemplatePath = convertTemplate
cliArguments.OutputFilePath = convertOutputFile
cliArguments.PrettyPrint = prettyPrint

if len(*convertImpOutputFile) > 0 && len(*convertImpTemplate) > 0 {
cliArguments.TemplatePath = convertImpTemplate
cliArguments.OutputFilePath = convertImpOutputFile
} else if len(*convertOutputFile) > 0 && len(*convertTemplate) > 0 {
cliArguments.TemplatePath = convertTemplate
cliArguments.OutputFilePath = convertOutputFile
} else if len(*convertTemplate) > 0 && len(*convertImpOutputFile) > 0 {
cliArguments.TemplatePath = convertTemplate
cliArguments.OutputFilePath = convertImpOutputFile
} else {
err = errors.New("You have to specify the template and the output file, try --help")
return
}

// configure
case configure.FullCommand():
cliArguments.Mode = &ConfigureMode

// create Stack
case createStack.FullCommand():
cliArguments.Mode = &CreateStackMode
cliArguments.Stack = createStackName
cliArguments.TemplatePath = createStackTemplate
if len(*createStackImpTemplate) > 0 && len(*createStackImpName) > 0 {
cliArguments.Stack = createStackImpName
cliArguments.TemplatePath = createStackImpTemplate
} else if len(*createStackName) > 0 && len(*createStackTemplate) > 0 {
cliArguments.Stack = createStackName
cliArguments.TemplatePath = createStackTemplate
} else if len(*createStackName) > 0 && len(*createStackImpTemplate) > 0 {
cliArguments.Stack = createStackName
cliArguments.TemplatePath = createStackImpTemplate
} else {
err = errors.New("You have to specify stack name and template file, try --help")
return
}

// delete Stack
case deleteStack.FullCommand():
cliArguments.Mode = &DestroyStackMode
cliArguments.Stack = deleteStackName
if len(*deleteStackName) > 0 {
cliArguments.Stack = deleteStackName
} else if len(*deleteStackImpName) > 0 {
cliArguments.Stack = deleteStackImpName
} else {
err = errors.New("You have to specify the stack name, try --help")
return
}

// set up remote sink
case setupSink.FullCommand():
Expand Down
2 changes: 1 addition & 1 deletion progress/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func ConfigureRemoteSink(context *context.Context) (err error) {

if err == nil {
context.Logger.Info("Remote sink configuration successful")
context.Logger.Info("It's configuration may take up to a minute, wait before calling 'create-stack' with flag --progress")
context.Logger.Warning("It's configuration may take up to a minute, wait before calling 'create-stack' with flag --progress")
}
return
}
Expand Down
20 changes: 13 additions & 7 deletions stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ func getTemplateFromFile(context *context.Context) (string, string) {
}

// This function uses CreateStackInput variable to create Stack.
func createStack(context *context.Context, templateStruct cloudformation.CreateStackInput, session *session.Session) {
func createStack(context *context.Context, templateStruct cloudformation.CreateStackInput, session *session.Session) (err error) {
api := cloudformation.New(session)
_, err := api.CreateStack(&templateStruct)
if err != nil {
context.Logger.Error("Error creating stack: " + err.Error())
}
_, err = api.CreateStack(&templateStruct)
return
}

// This function uses all functions above and session to create Stack.
Expand All @@ -63,10 +61,18 @@ func NewStack(context *context.Context) {
return
}
templateStruct.NotificationARNs = []*string{conn.TopicArn}
createStack(context, templateStruct, currentSession)
err = createStack(context, templateStruct, currentSession)
if err != nil {
context.Logger.Error("Error creating stack: " + err.Error())
return
}
conn.MonitorQueue()
} else {
createStack(context, templateStruct, currentSession)
err := createStack(context, templateStruct, currentSession)
if err != nil {
context.Logger.Error("Error creating stack: " + err.Error())
return
}
}

}
Expand Down

0 comments on commit f0798b2

Please sign in to comment.