-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
518 additions
and
1,039 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,94 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/ssm" | ||
"github.com/fatih/color" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ssm" | ||
"github.com/gjbae1212/gossm/internal" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var ( | ||
// cmdCommand is a AWS Systems Manager Run Command. | ||
// cmdCommand is AWS Systems Manager Run Command. | ||
cmdCommand = &cobra.Command{ | ||
Use: "cmd", | ||
Short: "Exec `run command` under AWS SSM with interactive CLI", | ||
Long: "Exec `run command` under AWS SSM with interactive CLI", | ||
PreRun: func(cmd *cobra.Command, args []string) { | ||
initCredential() | ||
executor.execCommand = viper.GetString("cmd-exec") | ||
Run: func(cmd *cobra.Command, args []string) { | ||
var ( | ||
targets []*internal.Target | ||
err error | ||
) | ||
ctx := context.Background() | ||
|
||
// set region | ||
if err := setRegion(credential); err != nil { | ||
panicRed(err) | ||
exec := strings.TrimSpace(viper.GetString("cmd-exec")) | ||
if exec == "" { | ||
panicRed(fmt.Errorf("[err] not found exec command")) | ||
} | ||
|
||
// set multi target | ||
if err := setMultiTarget(credential, executor); err != nil { | ||
panicRed(err) | ||
// get targets | ||
argTarget := strings.TrimSpace(viper.GetString("cmd-target")) | ||
if argTarget != "" { | ||
table, err := internal.FindInstances(ctx, *_credential.awsConfig) | ||
if err != nil { | ||
panicRed(err) | ||
} | ||
for _, t := range table { | ||
if t.Name == argTarget { | ||
targets = append(targets, t) | ||
break | ||
} | ||
} | ||
} | ||
}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
// send command | ||
cmdOutput, err := sendCommand(credential.awsSession, credential.awsRegion, executor.multiTarget, executor.execCommand) | ||
|
||
if len(targets) == 0 { | ||
targets, err = internal.AskMultiTarget(ctx, *_credential.awsConfig) | ||
if err != nil { | ||
panicRed(err) | ||
} | ||
} | ||
|
||
var targetName string | ||
for _, t := range targets { | ||
targetName += " " + t.Name + " " | ||
} | ||
|
||
internal.PrintReady(exec, _credential.awsConfig.Region, targetName) | ||
|
||
sendOutput, err := internal.SendCommand(ctx, *_credential.awsConfig, targets, exec) | ||
if err != nil { | ||
panicRed(err) | ||
} | ||
|
||
commandId := *cmdOutput.Command.CommandId | ||
instanceIds := aws.StringValueSlice(cmdOutput.Command.InstanceIds) | ||
fmt.Printf("command: %s, commandId: %s, targets: %s\n", | ||
color.GreenString(executor.execCommand), color.GreenString(commandId), | ||
color.GreenString(fmt.Sprintf("%v", instanceIds))) | ||
fmt.Printf("%s\n", color.YellowString("Waiting Response ...")) | ||
|
||
// wait 3 seconds | ||
time.Sleep(time.Second * 3) | ||
|
||
// show result | ||
var inputs []*ssm.GetCommandInvocationInput | ||
for _, inst := range cmdOutput.Command.InstanceIds { | ||
for _, inst := range sendOutput.Command.InstanceIds { | ||
inputs = append(inputs, &ssm.GetCommandInvocationInput{ | ||
CommandId: cmdOutput.Command.CommandId, | ||
InstanceId: inst, | ||
CommandId: sendOutput.Command.CommandId, | ||
InstanceId: aws.String(inst), | ||
}) | ||
} | ||
printCommandInvocation(credential.awsSession, credential.awsRegion, inputs) | ||
internal.PrintCommandInvocation(ctx, *_credential.awsConfig, inputs) | ||
}, | ||
} | ||
) | ||
|
||
func init() { | ||
cmdCommand.Flags().StringP("exec", "e", "", "[required] execute command") | ||
cmdCommand.Flags().StringP("target", "t", "", "[optional] it is ec2 instanceId.") | ||
|
||
viper.BindPFlag("cmd-exec", cmdCommand.Flags().Lookup("exec")) | ||
viper.BindPFlag("cmd-target", cmdCommand.Flags().Lookup("target")) | ||
|
||
rootCmd.AddCommand(cmdCommand) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.