From 321c1da23fb0a82b43d6ea63cae35c12e5cbaea7 Mon Sep 17 00:00:00 2001 From: ponkio-o <29038315+ponkio-o@users.noreply.github.com> Date: Fri, 4 Oct 2024 01:05:28 +0900 Subject: [PATCH 1/4] refactor --- connect.go | 73 +++++++++++++++++++----------------------------------- 1 file changed, 26 insertions(+), 47 deletions(-) diff --git a/connect.go b/connect.go index a2def24..555cd3f 100644 --- a/connect.go +++ b/connect.go @@ -8,7 +8,6 @@ import ( "os" "os/exec" "os/signal" - "strings" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/ec2" @@ -19,14 +18,14 @@ import ( ) type EC2Instance struct { - architecture string - instanceType string - instanceID string - instanceProfileArn string - keyName string - privateIP string - state string - nameTag string + architecture types.ArchitectureValues + instanceType types.InstanceType + instanceID string + instanceProfile string + keyName string + privateIP string + state types.InstanceStateName + nameTag string } type sessInfo struct { @@ -101,19 +100,9 @@ func selectInstance(c *cli.Context) (string, error) { "InstanceID", ins[i].instanceID, "InstanceProfile", - func(p string) string { - if p == "" { - return "" - } - return strings.Split(ins[i].instanceProfileArn, "/")[1] - }(ins[i].instanceProfileArn), + ins[i].instanceProfile, "KeyName", - func(k string) string { - if k == "" { - return "" - } - return k - }(ins[i].keyName), + ins[i].keyName, "PrivateIP", ins[i].privateIP, "State", @@ -129,36 +118,26 @@ func selectInstance(c *cli.Context) (string, error) { func getInstanceInfo(c *cli.Context) ([]EC2Instance, error) { app := c.Context.Value(appCLI).(*App) - result, err := app.ec2.DescribeInstances(context.TODO(), &ec2.DescribeInstancesInput{ + paginator := ec2.NewDescribeInstancesPaginator(app.ec2, &ec2.DescribeInstancesInput{ MaxResults: aws.Int32(150), }) - if err != nil { - return nil, err - } - for _, v := range result.Reservations { - if aws.ToString((*string)(&v.Instances[0].State.Name)) == "running" { + var instances []EC2Instance + for paginator.HasMorePages() { + page, err := paginator.NextPage(context.Background()) + if err != nil { + return nil, err + } + for _, rsv := range page.Reservations { instances = append(instances, EC2Instance{ - architecture: aws.ToString((*string)(&v.Instances[0].Architecture)), - instanceType: aws.ToString((*string)(&v.Instances[0].InstanceType)), - instanceID: aws.ToString(v.Instances[0].InstanceId), - instanceProfileArn: func(p types.Instance) string { - if p.IamInstanceProfile == nil { - return "" - } - return aws.ToString(p.IamInstanceProfile.Arn) - }(v.Instances[0]), - keyName: aws.ToString(v.Instances[0].KeyName), - privateIP: aws.ToString(v.Instances[0].PrivateIpAddress), - state: aws.ToString((*string)(&v.Instances[0].State.Name)), - nameTag: func(t []types.Tag) string { - for _, v := range t { - if aws.ToString(v.Key) == "Name" { - return aws.ToString(v.Value) - } - } - return "" - }(v.Instances[0].Tags), + architecture: rsv.Instances[0].Architecture, + instanceType: rsv.Instances[0].InstanceType, + instanceID: aws.ToString(rsv.Instances[0].InstanceId), + instanceProfile: extractInstanceProfile(rsv.Instances[0].IamInstanceProfile), + keyName: extractKeyName(rsv.Instances[0].KeyName), + privateIP: aws.ToString(rsv.Instances[0].PrivateIpAddress), + state: rsv.Instances[0].State.Name, + nameTag: extractNameTag(rsv.Instances[0].Tags), }) } } From eaf4a8f5d02828a371aeeada01979ccd1ac09516 Mon Sep 17 00:00:00 2001 From: ponkio-o <29038315+ponkio-o@users.noreply.github.com> Date: Fri, 4 Oct 2024 01:05:56 +0900 Subject: [PATCH 2/4] added helper func --- lib.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 lib.go diff --git a/lib.go b/lib.go new file mode 100644 index 0000000..d1f7142 --- /dev/null +++ b/lib.go @@ -0,0 +1,34 @@ +package app + +import ( + "strings" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/ec2/types" +) + +func extractInstanceProfile(p *types.IamInstanceProfile) string { + if p == nil { + return "" + } + + return strings.Split(aws.ToString(p.Arn), "/")[1] +} + +func extractKeyName(k *string) string { + if aws.ToString(k) == "" { + return "" + } + + return aws.ToString(k) +} + +func extractNameTag(tags []types.Tag) string { + for _, t := range tags { + if aws.ToString(t.Key) == "Name" { + return aws.ToString(t.Value) + } + } + + return "" +} From 27db7dea4cfe4247c2d6cb97171002e37230e9b3 Mon Sep 17 00:00:00 2001 From: ponkio-o <29038315+ponkio-o@users.noreply.github.com> Date: Fri, 4 Oct 2024 01:07:20 +0900 Subject: [PATCH 3/4] fix order --- lib.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib.go b/lib.go index d1f7142..dbed420 100644 --- a/lib.go +++ b/lib.go @@ -8,19 +8,19 @@ import ( ) func extractInstanceProfile(p *types.IamInstanceProfile) string { - if p == nil { - return "" + if p != nil { + return strings.Split(aws.ToString(p.Arn), "/")[1] } - return strings.Split(aws.ToString(p.Arn), "/")[1] + return "" } func extractKeyName(k *string) string { - if aws.ToString(k) == "" { - return "" + if aws.ToString(k) != "" { + return aws.ToString(k) } - return aws.ToString(k) + return "" } func extractNameTag(tags []types.Tag) string { From 84809b3d3678954466b24ac301feabba6a8ba883 Mon Sep 17 00:00:00 2001 From: ponkio-o <29038315+ponkio-o@users.noreply.github.com> Date: Fri, 4 Oct 2024 01:10:16 +0900 Subject: [PATCH 4/4] fix lint --- connect.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/connect.go b/connect.go index 555cd3f..cda66ce 100644 --- a/connect.go +++ b/connect.go @@ -34,8 +34,6 @@ type sessInfo struct { TokenValue string } -var instances = []EC2Instance{} - func ConnectToEC2Instance(c *cli.Context) error { id, err := selectInstance(c) if err != nil {