Skip to content

Commit c9cfd9e

Browse files
committed
Refactored to tui package
1 parent a1fb741 commit c9cfd9e

File tree

7 files changed

+281
-301
lines changed

7 files changed

+281
-301
lines changed

internal/clean.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var (
1616

1717
var cleanCmd = &cobra.Command{
1818
Use: "clean [" + strings.Join(allowedCleanArgs, "] [") + "]",
19-
Short: "Delete expired role/sso credentials from cache, optionally delete all credentials",
19+
Short: "Clean sso and role credentials from cache",
2020
Args: cobra.RangeArgs(1, 2),
2121
ValidArgs: allowedCleanArgs,
2222
Example: " knox clean creds\n knox clean sso -a\n knox clean creds sso",

internal/connect.go

Lines changed: 37 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ package internal
22

33
import (
44
"fmt"
5+
"os"
56
"os/exec"
6-
"time"
77
"syscall"
8-
"os"
98

109
"github.com/null93/aws-knox/sdk/credentials"
11-
"github.com/null93/aws-knox/sdk/picker"
10+
"github.com/null93/aws-knox/sdk/tui"
1211
"github.com/spf13/cobra"
1312
)
1413

@@ -29,134 +28,66 @@ var connectCmd = &cobra.Command{
2928
Short: "Connect to a specific EC2 instance using AWS session-manager-plugin",
3029
Args: cobra.ExactArgs(0),
3130
Run: func(cmd *cobra.Command, args []string) {
32-
now := time.Now()
33-
sessions, err := credentials.GetSessions()
34-
if err != nil {
31+
var err error
32+
var sessions credentials.Sessions
33+
var session *credentials.Session
34+
var roles credentials.Roles
35+
var role *credentials.Role
36+
var binaryPath string
37+
if sessions, err = credentials.GetSessions(); err != nil {
3538
ExitWithError(1, "failed to get configured sessions", err)
3639
}
37-
if len(sessions) == 0 {
38-
ExitWithError(2, "no sso sessions found in config", err)
39-
}
4040
if connectSessionName == "" {
41-
p := picker.NewPicker()
42-
p.WithMaxHeight(10)
43-
p.WithEmptyMessage("No SSO Sessions Found")
44-
p.WithTitle("Pick SSO Session")
45-
p.WithHeaders("SSO Session", "Region", "SSO Start URL", "Expires In")
46-
for _, session := range sessions {
47-
expires := "-"
48-
if session.ClientToken != nil && !session.ClientToken.IsExpired() {
49-
expires = fmt.Sprintf("%.f mins", session.ClientToken.ExpiresAt.Sub(now).Minutes())
50-
}
51-
p.AddOption(session.Name, session.Name, session.Region, session.StartUrl, expires)
41+
if connectSessionName, err = tui.SelectSession(sessions); err != nil {
42+
ExitWithError(2, "failed to pick an sso session", err)
5243
}
53-
selection := p.Pick()
54-
if selection == nil {
55-
ExitWithError(3, "failed to pick an sso session", err)
56-
}
57-
connectSessionName = selection.Value.(string)
5844
}
59-
session := sessions.FindByName(connectSessionName)
60-
if session == nil {
61-
ExitWithError(4, "session with passed name not found", err)
45+
if session = sessions.FindByName(connectSessionName); session == nil {
46+
ExitWithError(3, "session with passed name not found", err)
6247
}
6348
if session.ClientToken == nil || session.ClientToken.IsExpired() {
64-
err := ClientLogin(session)
65-
if err != nil {
66-
ExitWithError(5, "failed to authorize device login", err)
49+
if err = tui.ClientLogin(session); err != nil {
50+
ExitWithError(4, "failed to authorize device login", err)
6751
}
6852
}
6953
if connectAccountId == "" {
70-
connectAccountIds, err := session.GetAccounts()
71-
if err != nil {
72-
ExitWithError(6, "failed to get account ids", err)
73-
}
74-
if len(connectAccountIds) == 0 {
75-
ExitWithError(7, "no accounts found", err)
76-
}
77-
p := picker.NewPicker()
78-
p.WithMaxHeight(5)
79-
p.WithEmptyMessage("No Accounts Found")
80-
p.WithTitle("Pick Account")
81-
p.WithHeaders("Account ID", "Name", "Email")
82-
for _, account := range connectAccountIds {
83-
p.AddOption(account.Id, account.Id, account.Name, account.Email)
84-
}
85-
selection := p.Pick()
86-
if selection == nil {
87-
ExitWithError(8, "failed to pick an account id", err)
54+
if connectAccountId, err = tui.SelectAccount(session); err != nil {
55+
ExitWithError(5, "failed to pick an account id", err)
8856
}
89-
connectAccountId = selection.Value.(string)
9057
}
91-
roles, err := session.GetRoles(connectAccountId)
58+
if roles, err = session.GetRoles(connectAccountId); err != nil {
59+
ExitWithError(6, "failed to get roles", err)
60+
}
9261
if connectRoleName == "" {
93-
if err != nil {
94-
ExitWithError(9, "failed to get roles", err)
95-
}
96-
p := picker.NewPicker()
97-
p.WithMaxHeight(5)
98-
p.WithEmptyMessage("No Roles Found")
99-
p.WithTitle("Pick Role")
100-
p.WithHeaders("Role Name", "Expires In")
101-
for _, role := range roles {
102-
expires := "-"
103-
if role.Credentials != nil && !role.Credentials.IsExpired() {
104-
expires = fmt.Sprintf("%.f mins", role.Credentials.Expiration.Sub(now).Minutes())
105-
}
106-
p.AddOption(role.Name, role.Name, expires)
62+
if connectRoleName, err = tui.SelectRole(roles); err != nil {
63+
ExitWithError(7, "failed to pick a role", err)
10764
}
108-
selection := p.Pick()
109-
if selection == nil {
110-
ExitWithError(10, "failed to pick a role name", err)
111-
}
112-
connectRoleName = selection.Value.(string)
11365
}
114-
role := roles.FindByName(connectRoleName)
115-
if role == nil {
116-
ExitWithError(11, "role with passed name not found", err)
66+
if role = roles.FindByName(connectRoleName); role == nil {
67+
ExitWithError(8, "role with passed name not found", err)
11768
}
11869
if role.Credentials == nil || role.Credentials.IsExpired() {
119-
err := session.RefreshRoleCredentials(role)
120-
if err != nil {
121-
ExitWithError(12, "failed to get credentials", err)
70+
if err = session.RefreshRoleCredentials(role); err != nil {
71+
ExitWithError(9, "failed to get credentials", err)
12272
}
123-
err = role.Credentials.Save(session.Name, role.CacheKey())
124-
if err != nil {
125-
ExitWithError(13, "failed to save credentials", err)
73+
if err = role.Credentials.Save(session.Name, role.CacheKey()); err != nil {
74+
ExitWithError(10, "failed to save credentials", err)
12675
}
12776
}
12877
if err := role.MarkLastUsed(); err != nil {
129-
ExitWithError(14, "failed to mark last used role", err)
78+
ExitWithError(11, "failed to mark last used role", err)
13079
}
13180
if connectInstanceId == "" {
132-
instances, err := role.GetManagedInstances()
133-
if err != nil {
134-
ExitWithError(15, "failed to get instances", err)
135-
}
136-
if len(instances) == 0 {
137-
ExitWithError(16, "no instances found", err)
81+
if connectInstanceId, err = tui.SelectInstance(role); err != nil {
82+
ExitWithError(12, "failed to pick an instance", err)
13883
}
139-
p := picker.NewPicker()
140-
p.WithMaxHeight(10)
141-
p.WithEmptyMessage("No Instances Found")
142-
p.WithTitle("Pick EC2 Instance")
143-
p.WithHeaders("Instance ID", "Instance Type", "Private IP", "Public IP", "Name")
144-
for _, instance := range instances {
145-
p.AddOption(instance.Id, instance.Id, instance.InstanceType, instance.PrivateIpAddress, instance.PublicIpAddress, instance.Name)
146-
}
147-
selection := p.Pick()
148-
if selection == nil {
149-
ExitWithError(17, "failed to pick an instance id", err)
150-
}
151-
connectInstanceId = selection.Value.(string)
15284
}
15385
details, err := role.StartSession(connectInstanceId, connectUid)
15486
if err != nil {
155-
ExitWithError(18, "failed to start ssm session", err)
87+
ExitWithError(13, "failed to start ssm session", err)
15688
}
157-
binaryPath, err := exec.LookPath("session-manager-plugin")
158-
if err != nil {
159-
ExitWithError(19, "failed to find session-manager-plugin, see "+SESSION_MANAGER_PLUGIN_URL, err)
89+
if binaryPath, err = exec.LookPath("session-manager-plugin"); err != nil {
90+
ExitWithError(14, "failed to find session-manager-plugin, see "+SESSION_MANAGER_PLUGIN_URL, err)
16091
}
16192
command := exec.Command(
16293
binaryPath,
@@ -170,12 +101,10 @@ var connectCmd = &cobra.Command{
170101
command.Stdin = os.Stdin
171102
command.Stdout = os.Stdout
172103
command.Stderr = os.Stderr
173-
command.SysProcAttr = &syscall.SysProcAttr{ Setpgid: true, Foreground: true }
174-
err = command.Run()
175-
if err != nil {
176-
ExitWithError(20, "failed to run session-manager-plugin", err)
104+
command.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Foreground: true}
105+
if err = command.Run(); err != nil {
106+
ExitWithError(15, "failed to run session-manager-plugin", err)
177107
}
178-
fmt.Println ("EXITED")
179108
},
180109
}
181110

internal/creds-select.go

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ package internal
22

33
import (
44
"fmt"
5-
"time"
65

76
"github.com/null93/aws-knox/sdk/credentials"
8-
"github.com/null93/aws-knox/sdk/picker"
7+
"github.com/null93/aws-knox/sdk/tui"
98
"github.com/spf13/cobra"
109
)
1110

@@ -14,60 +13,39 @@ var credsSelectCmd = &cobra.Command{
1413
Short: "Pick from cached role credentials",
1514
Args: cobra.ExactArgs(0),
1615
Run: func(cmd *cobra.Command, args []string) {
17-
now := time.Now()
18-
roles, err := credentials.GetSavedRolesWithCredentials()
19-
if err != nil {
20-
ExitWithError(1, "failed to get role credentials", err)
21-
}
22-
p := picker.NewPicker()
23-
p.WithMaxHeight(10)
24-
p.WithEmptyMessage("No Role Credentials Found")
25-
p.WithTitle("Pick Role Credentials")
26-
p.WithHeaders("SSO Session", "Region", "Account ID", "Role Name", "Expires In")
27-
for _, role := range roles {
28-
expires := "-"
29-
if role.Credentials != nil && !role.Credentials.IsExpired() {
30-
expires = fmt.Sprintf("%.f mins", role.Credentials.Expiration.Sub(now).Minutes())
16+
var err error
17+
var sessions credentials.Sessions
18+
var session *credentials.Session
19+
var role *credentials.Role
20+
var json string
21+
role, err = tui.SelectRolesCredentials()
22+
if role.Credentials == nil || role.Credentials.IsExpired() {
23+
if sessions, err = credentials.GetSessions(); err != nil {
24+
ExitWithError(1, "failed to parse sso sessions", err)
3125
}
32-
p.AddOption(role, role.SessionName, role.Region, role.AccountId, role.Name, expires)
33-
}
34-
selection := p.Pick()
35-
if selection == nil {
36-
ExitWithError(3, "failed to pick role credentials", err)
37-
}
38-
selectedRole := selection.Value.(credentials.Role)
39-
if selectedRole.Credentials.IsExpired() {
40-
sessions, err := credentials.GetSessions()
41-
if err != nil {
42-
ExitWithError(2, "failed to parse sso sessions", err)
43-
}
44-
session := sessions.FindByName(selectedRole.SessionName)
45-
if session == nil {
46-
ExitWithError(3, "failed to find sso session "+selectedRole.SessionName, err)
26+
if session = sessions.FindByName(role.SessionName); session == nil {
27+
ExitWithError(2, "failed to find sso session "+role.SessionName, err)
4728
}
4829
if session.ClientToken == nil || session.ClientToken.IsExpired() {
49-
err := ClientLogin(session)
50-
if err != nil {
51-
ExitWithError(4, "failed to authorize device login", err)
30+
if err = tui.ClientLogin(session); err != nil {
31+
ExitWithError(3, "failed to authorize device login", err)
5232
}
5333
}
54-
err = session.RefreshRoleCredentials(&selectedRole)
55-
if err != nil {
56-
ExitWithError(4, "failed to get credentials", err)
34+
35+
if err = session.RefreshRoleCredentials(role); err != nil {
36+
ExitWithError(9, "failed to get credentials", err)
5737
}
58-
err = selectedRole.Credentials.Save(session.Name, selectedRole.CacheKey())
59-
if err != nil {
60-
ExitWithError(5, "failed to save credentials", err)
38+
if err = role.Credentials.Save(session.Name, role.CacheKey()); err != nil {
39+
ExitWithError(10, "failed to save credentials", err)
6140
}
6241
}
63-
serialized, err := selectedRole.Credentials.ToJSON()
64-
if err != nil {
65-
ExitWithError(4, "failed to serialize role credentials", err)
42+
if err = role.MarkLastUsed(); err != nil {
43+
ExitWithError(11, "failed to mark last used role", err)
6644
}
67-
if err := selectedRole.MarkLastUsed(); err != nil {
68-
ExitWithError(5, "failed to mark last used role", err)
45+
if json, err = role.Credentials.ToJSON(); err != nil {
46+
ExitWithError(12, "failed to serialize role credentials", err)
6947
}
70-
fmt.Println(serialized)
48+
fmt.Println(json)
7149
},
7250
}
7351

internal/creds-last-used.go renamed to internal/last-used.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,50 @@ import (
44
"fmt"
55

66
"github.com/null93/aws-knox/sdk/credentials"
7+
"github.com/null93/aws-knox/sdk/tui"
78
"github.com/spf13/cobra"
89
)
910

10-
var credsLastUsedCmd = &cobra.Command{
11+
var lastUsedCmd = &cobra.Command{
1112
Use: "last-used",
1213
Short: "Use last used role credentials",
1314
Args: cobra.ExactArgs(0),
1415
Run: func(cmd *cobra.Command, args []string) {
15-
role, err := credentials.GetLastUsedRole()
16-
if err != nil {
16+
var err error
17+
var sessions credentials.Sessions
18+
var session *credentials.Session
19+
var role credentials.Role
20+
var json string
21+
if role, err = credentials.GetLastUsedRole(); err != nil {
1722
ExitWithError(1, "failed to get last used role", err)
1823
}
1924
if role.Credentials == nil || role.Credentials.IsExpired() {
20-
sessions, err := credentials.GetSessions()
21-
if err != nil {
25+
if sessions, err = credentials.GetSessions(); err != nil {
2226
ExitWithError(2, "failed to parse sso sessions", err)
2327
}
24-
session := sessions.FindByName(role.SessionName)
25-
if session == nil {
28+
if session = sessions.FindByName(role.SessionName); session == nil {
2629
ExitWithError(3, "failed to find sso session "+role.SessionName, err)
2730
}
2831
if session.ClientToken == nil || session.ClientToken.IsExpired() {
29-
err := ClientLogin(session)
30-
if err != nil {
32+
if err = tui.ClientLogin(session); err != nil {
3133
ExitWithError(4, "failed to authorize device login", err)
3234
}
3335
}
34-
err = session.RefreshRoleCredentials(&role)
35-
if err != nil {
36+
if err = session.RefreshRoleCredentials(&role); err != nil {
3637
ExitWithError(5, "failed to get credentials", err)
3738
}
38-
err = role.Credentials.Save(session.Name, role.CacheKey())
39-
if err != nil {
39+
if err = role.Credentials.Save(session.Name, role.CacheKey()); err != nil {
4040
ExitWithError(6, "failed to save credentials", err)
4141
}
4242
}
43-
serialized, err := role.Credentials.ToJSON()
44-
if err != nil {
43+
if json, err = role.Credentials.ToJSON(); err != nil {
4544
ExitWithError(7, "failed to serialize role credentials", err)
4645
}
47-
fmt.Println(serialized)
46+
fmt.Println(json)
4847
},
4948
}
5049

5150
func init() {
52-
credsCmd.AddCommand(credsLastUsedCmd)
53-
credsLastUsedCmd.Flags().SortFlags = true
51+
RootCmd.AddCommand(lastUsedCmd)
52+
lastUsedCmd.Flags().SortFlags = true
5453
}

0 commit comments

Comments
 (0)