Skip to content

Commit

Permalink
Added quiet (-q) flag
Browse files Browse the repository at this point in the history
  • Loading branch information
aidansteele committed Mar 16, 2018
1 parent 32e8480 commit 5628530
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
6 changes: 3 additions & 3 deletions root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

var cfgFile string
var quiet bool

var RootCmd = &cobra.Command{
Use: "gossm",
Expand Down Expand Up @@ -71,6 +72,7 @@ func init() {
RootCmd.PersistentFlags().String("s3-bucket", "", "")
RootCmd.PersistentFlags().String("s3-key-prefix", "", "")
RootCmd.PersistentFlags().BoolP("powershell", "p", false, "")
RootCmd.PersistentFlags().BoolVarP(&quiet, "quiet", "q", false, "")
RootCmd.PersistentFlags().StringSliceP("instance-id", "i", []string{}, "")
RootCmd.PersistentFlags().StringSliceP("tag", "t", []string{}, "")
RootCmd.PersistentFlags().Int64("timeout", 600, "")
Expand All @@ -88,7 +90,5 @@ func initConfig() {
viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
viper.ReadInConfig()
}
33 changes: 22 additions & 11 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ func getFromS3Url(sess *session.Session, urlString string) (*string, error) {
}

func printFormattedOutput(prefix, output string) {
if quiet {
fmt.Println(output)
return
}

windowWidth := 80

if err := termbox.Init(); err == nil {
Expand Down Expand Up @@ -230,7 +235,9 @@ func makeCommandInput(targets []*ssm.Target, bucket, keyPrefix, command, shellTy
}

func printInfo(prefix, info string) {
fmt.Printf("%s%s\n", color.BlueString("%s", prefix), color.New(color.Faint).Sprintf("%s", info))
if !quiet {
fmt.Printf("%s%s\n", color.BlueString("%s", prefix), color.New(color.Faint).Sprintf("%s", info))
}
}

func doit(sess *session.Session, commandInput *ssm.SendCommandInput) {
Expand All @@ -248,13 +255,15 @@ func doit(sess *session.Session, commandInput *ssm.SendCommandInput) {
instanceIds := commandInstanceIds(sess, *commandId)
printedInstanceIds := []string{}

printInfo("Command ID: ", *commandId)
printInfo(fmt.Sprintf("Running command on %d instances: ", len(instanceIds.InstanceIds)), fmt.Sprintf("%+v", instanceIds.InstanceIds))
if len(instanceIds.FaultyInstanceIds) > 0 {
color.Red("Command sent to %d terminated instances: %+v\n", len(instanceIds.FaultyInstanceIds), instanceIds.FaultyInstanceIds)
}
if len(instanceIds.WrongPlatformInstanceIds) > 0 {
color.Red("Command sent to %d wrong OS instances: %+v\n", len(instanceIds.WrongPlatformInstanceIds), instanceIds.WrongPlatformInstanceIds)
if !quiet {
printInfo("Command ID: ", *commandId)
printInfo(fmt.Sprintf("Running command on %d instances: ", len(instanceIds.InstanceIds)), fmt.Sprintf("%+v", instanceIds.InstanceIds))
if len(instanceIds.FaultyInstanceIds) > 0 {
color.Red("Command sent to %d terminated instances: %+v\n", len(instanceIds.FaultyInstanceIds), instanceIds.FaultyInstanceIds)
}
if len(instanceIds.WrongPlatformInstanceIds) > 0 {
color.Red("Command sent to %d wrong OS instances: %+v\n", len(instanceIds.WrongPlatformInstanceIds), instanceIds.WrongPlatformInstanceIds)
}
}

expectedResponseCount := len(instanceIds.InstanceIds) - len(instanceIds.FaultyInstanceIds) - len(instanceIds.WrongPlatformInstanceIds)
Expand Down Expand Up @@ -296,9 +305,11 @@ func doit(sess *session.Session, commandInput *ssm.SendCommandInput) {
printFormattedOutput(colour.Sprint(prefix), *stderr)
}

colour := color.New(color.FgBlue)
message := fmt.Sprintf("%s: %s", *invocation.Status, *invocation.StatusDetails)
printFormattedOutput(colour.Sprint(prefix), message)
if !quiet {
colour := color.New(color.FgBlue)
message := fmt.Sprintf("%s: %s", *invocation.Status, *invocation.StatusDetails)
printFormattedOutput(colour.Sprint(prefix), message)
}

printedInstanceIds = append(printedInstanceIds, instanceId)
}
Expand Down

0 comments on commit 5628530

Please sign in to comment.