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

feat: add option to filter instance by specific tag key in ask #58

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ var (

// get targets
argTarget := strings.TrimSpace(viper.GetString("cmd-target"))
argTag := strings.TrimSpace(viper.GetString("cmd-filter"))

if argTarget != "" {
table, err := internal.FindInstances(ctx, *_credential.awsConfig)
table, err := internal.FindInstances(ctx, *_credential.awsConfig, argTag)
if err != nil {
panicRed(err)
}
Expand All @@ -48,7 +50,7 @@ var (
}

if len(targets) == 0 {
targets, err = internal.AskMultiTarget(ctx, *_credential.awsConfig)
targets, err = internal.AskMultiTarget(ctx, *_credential.awsConfig, argTag)
if err != nil {
panicRed(err)
}
Expand Down Expand Up @@ -86,9 +88,11 @@ var (
func init() {
cmdCommand.Flags().StringP("exec", "e", "", "[required] execute command")
cmdCommand.Flags().StringP("target", "t", "", "[optional] it is ec2 instanceId.")
cmdCommand.Flags().StringP("filter", "f", "", "[optional] which tag key to add to in table filtering.")

viper.BindPFlag("cmd-exec", cmdCommand.Flags().Lookup("exec"))
viper.BindPFlag("cmd-target", cmdCommand.Flags().Lookup("target"))
viper.BindPFlag("cmd-filter", cmdCommand.Flags().Lookup("filter"))

rootCmd.AddCommand(cmdCommand)
}
7 changes: 5 additions & 2 deletions cmd/fwd.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ var (

// get target
argTarget := strings.TrimSpace(viper.GetString("fwd-target"))
argTag := strings.TrimSpace(viper.GetString("fwd-filter"))
if argTarget != "" {
table, err := internal.FindInstances(ctx, *_credential.awsConfig)
table, err := internal.FindInstances(ctx, *_credential.awsConfig, argTag)
if err != nil {
panicRed(err)
}
Expand All @@ -43,7 +44,7 @@ var (
}
}
if target == nil {
target, err = internal.AskTarget(ctx, *_credential.awsConfig)
target, err = internal.AskTarget(ctx, *_credential.awsConfig, argTag)
if err != nil {
panicRed(err)
}
Expand Down Expand Up @@ -115,11 +116,13 @@ func init() {
fwdCommand.Flags().StringP("remote", "z", "", "[optional] remote port to forward to, ex) 8080")
fwdCommand.Flags().StringP("local", "l", "", "[optional] local port to use, ex) 1234")
fwdCommand.Flags().StringP("target", "t", "", "[optional] it is ec2 instanceId.")
fwdCommand.Flags().StringP("filter", "f", "", "[optional] which tag key to add to in table filtering.")

// mapping viper
viper.BindPFlag("fwd-remote-port", fwdCommand.Flags().Lookup("remote"))
viper.BindPFlag("fwd-local-port", fwdCommand.Flags().Lookup("local"))
viper.BindPFlag("fwd-target", fwdCommand.Flags().Lookup("target"))
viper.BindPFlag("fwd-filter", fwdCommand.Flags().Lookup("filter"))

rootCmd.AddCommand(fwdCommand)
}
7 changes: 5 additions & 2 deletions cmd/fwdrem.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ var (

// get target
argTarget := strings.TrimSpace(viper.GetString("fwd-target"))
argTag := strings.TrimSpace(viper.GetString("fwd-filter"))
if argTarget != "" {
table, err := internal.FindInstances(ctx, *_credential.awsConfig)
table, err := internal.FindInstances(ctx, *_credential.awsConfig, argTag)
if err != nil {
panicRed(err)
}
Expand All @@ -44,7 +45,7 @@ var (
}
}
if target == nil {
target, err = internal.AskTarget(ctx, *_credential.awsConfig)
target, err = internal.AskTarget(ctx, *_credential.awsConfig, argTag)
if err != nil {
panicRed(err)
}
Expand Down Expand Up @@ -131,12 +132,14 @@ func init() {
fwdremCommand.Flags().StringP("local", "l", "", "[optional] local port to use, ex) 1234")
fwdremCommand.Flags().StringP("target", "t", "", "[optional] it is ec2 instanceId to proxy through.")
fwdremCommand.Flags().StringP("host", "a", "", "[optional] it is remote host address to proxy to.")
fwdremCommand.Flags().StringP("filter", "f", "", "[optional] which tag key to add to in table filtering.")

// mapping viper
viper.BindPFlag("fwd-remote-port", fwdremCommand.Flags().Lookup("remote"))
viper.BindPFlag("fwd-local-port", fwdremCommand.Flags().Lookup("local"))
viper.BindPFlag("fwd-target", fwdremCommand.Flags().Lookup("target"))
viper.BindPFlag("fwd-host", fwdremCommand.Flags().Lookup("host"))
viper.BindPFlag("fwd-filter", fwdremCommand.Flags().Lookup("filter"))

rootCmd.AddCommand(fwdremCommand)
}
9 changes: 7 additions & 2 deletions cmd/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ var (

// get target
argTarget := strings.TrimSpace(viper.GetString("start-session-target"))
argTag := strings.TrimSpace(viper.GetString("start-session-filter"))

if argTarget != "" {
table, err := internal.FindInstances(ctx, *_credential.awsConfig)
table, err := internal.FindInstances(ctx, *_credential.awsConfig, argTag)
if err != nil {
panicRed(err)
}
Expand All @@ -40,7 +42,7 @@ var (
}
}
if target == nil {
target, err = internal.AskTarget(ctx, *_credential.awsConfig)
target, err = internal.AskTarget(ctx, *_credential.awsConfig, argTag)
if err != nil {
panicRed(err)
}
Expand Down Expand Up @@ -80,7 +82,10 @@ var (

func init() {
startSessionCommand.Flags().StringP("target", "t", "", "[optional] it is ec2 instanceId.")
startSessionCommand.Flags().StringP("filter", "f", "", "[optional] which tag key to add to in table filtering.")

viper.BindPFlag("start-session-target", startSessionCommand.Flags().Lookup("target"))
viper.BindPFlag("start-session-filter", startSessionCommand.Flags().Lookup("filter"))

// add sub command
rootCmd.AddCommand(startSessionCommand)
Expand Down
2 changes: 1 addition & 1 deletion cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var (
var sshCommand string
var targetName string
if exec == "" {
target, err := internal.AskTarget(ctx, *_credential.awsConfig)
target, err := internal.AskTarget(ctx, *_credential.awsConfig, "")
if err != nil {
panicRed(err)
}
Expand Down
23 changes: 16 additions & 7 deletions internal/ssm.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ func AskRegion(ctx context.Context, cfg aws.Config) (*Region, error) {
}

// AskTarget asks you which selects an instance.
func AskTarget(ctx context.Context, cfg aws.Config) (*Target, error) {
table, err := FindInstances(ctx, cfg)
func AskTarget(ctx context.Context, cfg aws.Config, tag string) (*Target, error) {
table, err := FindInstances(ctx, cfg, tag)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -141,8 +141,8 @@ func AskTarget(ctx context.Context, cfg aws.Config) (*Target, error) {
}

// AskMultiTarget asks you which selects multi targets.
func AskMultiTarget(ctx context.Context, cfg aws.Config) ([]*Target, error) {
table, err := FindInstances(ctx, cfg)
func AskMultiTarget(ctx context.Context, cfg aws.Config, tag string) ([]*Target, error) {
table, err := FindInstances(ctx, cfg, tag)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -207,21 +207,30 @@ func AskPorts() (port *Port, retErr error) {
}

// FindInstances returns all of instances-map with running state.
func FindInstances(ctx context.Context, cfg aws.Config) (map[string]*Target, error) {
func FindInstances(ctx context.Context, cfg aws.Config, filterTag string) (map[string]*Target, error) {
var (
client = ec2.NewFromConfig(cfg)
table = make(map[string]*Target)
outputFunc = func(table map[string]*Target, output *ec2.DescribeInstancesOutput) {
for _, rv := range output.Reservations {
for _, inst := range rv.Instances {
name := ""
tags := ""
for _, tag := range inst.Tags {
if aws.ToString(tag.Key) == "Name" {
name = aws.ToString(tag.Value)
break
} else {
if len(filterTag) > 0 {
if aws.ToString(tag.Key) == filterTag {
tags += fmt.Sprintf("%s:%s, ", aws.ToString(tag.Key), aws.ToString(tag.Value))
}
}
}
}
table[fmt.Sprintf("%s\t(%s)", name, *inst.InstanceId)] = &Target{
if len(tags) > 0 {
tags = strings.TrimSuffix(tags, ", ")
}
table[fmt.Sprintf("%s\t(%s)\t(%s)", name, *inst.InstanceId, tags)] = &Target{
Name: aws.ToString(inst.InstanceId),
PublicDomain: aws.ToString(inst.PublicDnsName),
PrivateDomain: aws.ToString(inst.PrivateDnsName),
Expand Down
2 changes: 1 addition & 1 deletion internal/ssm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestFindInstances(t *testing.T) {
}

for _, t := range tests {
result, err := FindInstances(t.ctx, t.cfg)
result, err := FindInstances(t.ctx, t.cfg, "")
assert.Equal(t.isErr, err != nil)
fmt.Println(len(result))
}
Expand Down