8000 refactor by ponkio-o · Pull Request #255 · ponkio-o/ec2x · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

refactor #255

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

Merged
merged 1 commit into from
Oct 12, 2024
Merged
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
12 changes: 6 additions & 6 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
)

type App struct {
ec2 *ec2.Client
ssm *ssm.Client
region string
EC2 *ec2.Client
SSM *ssm.Client
Region string
}

type appKey int
Expand All @@ -35,9 +35,9 @@ func New(c *cli.Context) error {
}

app := &App{
ec2: ec2.NewFromConfig(cfg),
ssm: ssm.NewFromConfig(cfg),
region: cfg.Region,
EC2: ec2.NewFromConfig(cfg),
SSM: ssm.NewFromConfig(cfg),
Region: cfg.Region,
}

c.Context = context.WithValue(c.Context, appCLI, app)
Expand Down
2 changes: 1 addition & 1 deletion cmd/ec2x/main.go
8000
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func main() {
{
Name: "connect",
Usage: "Connect to EC2 instance with Session Manager",
Action: app.ConnectToEC2Instance,
Action: app.ConnectCommand,
},
},
DefaultCommand: "connect",
Expand Down
51 changes: 21 additions & 30 deletions connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,43 +28,43 @@ type EC2Instance struct {
NameTag string
}

type sessInfo struct {
type SessInfo struct {
SessionID string
StreamUrl string
TokenValue string
}

func ConnectToEC2Instance(c *cli.Context) error {
id, err := selectInstance(c)
func ConnectCommand(c *cli.Context) error {
app := c.Context.Value(appCLI).(*App)
id, err := app.selectInstance()
if err != nil {
return err
}

err = startSession(c, id)
err = app.startSession(id, app.Region)
if err != nil {
return err
}

return nil
}

func startSession(c *cli.Context, id string) error {
app := c.Context.Value(appCLI).(*App)
result, err := app.ssm.StartSession(context.TODO(), &ssm.StartSessionInput{
func (app App) startSession(id, region string) error {
result, err := app.SSM.StartSession(context.TODO(), &ssm.StartSessionInput{
Target: aws.String(id),
})
if err != nil {
return err
}

sessi := sessInfo{
sessi := SessInfo{
SessionID: aws.ToString(result.SessionId),
StreamUrl: aws.ToString(result.StreamUrl),
TokenValue: aws.ToString(result.TokenValue),
}

sess, _ := json.Marshal(sessi)
cmd := exec.Command("session-manager-plugin", string(sess), app.region, "StartSession")
cmd := exec.Command("session-manager-plugin", string(sess), region, "StartSession")
signal.Ignore(os.Interrupt)
defer signal.Reset(os.Interrupt)
cmd.Stdout = os.Stdout
Expand All @@ -74,8 +74,8 @@ func startSession(c *cli.Context, id string) error {
return cmd.Run()
}

func selectInstance(c *cli.Context) (string, error) {
ins, err := getInstanceInfo(c)
func (app App) selectInstance() (string, error) {
ins, err := app.getInstanceInfo()
if err != nil {
return "", err
}
Expand All @@ -97,9 +97,8 @@ func selectInstance(c *cli.Context) (string, error) {
return ins[idx].InstanceID, nil
}

func getInstanceInfo(c *cli.Context) ([]EC2Instance, error) {
app := c.Context.Value(appCLI).(*App)
paginator := ec2.NewDescribeInstancesPaginator(app.ec2, &ec2.DescribeInstancesInput{
func (app App) getInstanceInfo() ([]EC2Instance, error) {
paginator := ec2.NewDescribeInstancesPaginator(app.EC2, &ec2.DescribeInstancesInput{
MaxResults: aws.Int32(150),
})

Expand Down Expand Up @@ -132,21 +131,13 @@ func getInstanceInfo(c *cli.Context) ([]EC2Instance, error) {

func genPreviewWindow(ins EC2Instance) string {
return fmt.Sprintf("%-16s: %s\n%-16s: %s\n%-16s: %s\n%-16s: %s\n%-16s: %s\n%-16s: %s\n%-16s: %s\n%-16s: %s\n",
"Name",
ins.NameTag,
"Architecture",
ins.Architecture,
"InstanceType",
ins.InstanceType,
"InstanceID",
ins.InstanceID,
"InstanceProfile",
ins.InstanceProfile,
"KeyName",
ins.KeyName,
"PrivateIP",
ins.PrivateIP,
"State",
ins.State,
"Name", ins.NameTag,
"Architecture", ins.Architecture,
"InstanceType", ins.InstanceType,
"InstanceID", ins.InstanceID,
"InstanceProfile", ins.InstanceProfile,
"KeyName", ins.KeyName,
"PrivateIP", ins.PrivateIP,
"State", ins.State,
)
}
0