diff --git a/root.go b/root.go index fcb00e8..bb5915b 100644 --- a/root.go +++ b/root.go @@ -11,6 +11,7 @@ import ( ) var cfgFile string +var quiet bool var RootCmd = &cobra.Command{ Use: "gossm", @@ -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, "") @@ -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() } diff --git a/run.go b/run.go index 614f95a..de2798d 100644 --- a/run.go +++ b/run.go @@ -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 { @@ -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) { @@ -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) @@ -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) }