8
8
"strings"
9
9
"syscall"
10
10
11
+ "github.com/null93/aws-knox/pkg/color"
11
12
"github.com/null93/aws-knox/sdk/credentials"
13
+ . "github.com/null93/aws-knox/sdk/style"
12
14
"github.com/null93/aws-knox/sdk/tui"
13
15
"github.com/spf13/cobra"
14
16
)
@@ -48,11 +50,11 @@ func rsyncInit(role *credentials.Role, instanceId string) {
48
50
command := exec .Command (
49
51
binaryPath ,
50
52
fmt .Sprintf (`{"SessionId": "%s", "TokenValue": "%s", "StreamUrl": "%s"}` , * details .SessionId , * details .TokenValue , * details .StreamUrl ),
51
- role . Region ,
53
+ region ,
52
54
"StartSession" ,
53
55
"" , // No Profile
54
56
fmt .Sprintf (`{"Target": "%s"}` , instanceId ),
55
- fmt .Sprintf ("https://ssm.%s.amazonaws.com" , role . Region ),
57
+ fmt .Sprintf ("https://ssm.%s.amazonaws.com" , region ),
56
58
)
57
59
command .Stdin = os .Stdin
58
60
command .SysProcAttr = & syscall.SysProcAttr {Setpgid : true , Foreground : true }
@@ -85,11 +87,11 @@ func rsyncStart(role *credentials.Role, instanceId string) {
85
87
command := exec .Command (
86
88
binaryPath ,
87
89
fmt .Sprintf (`{"SessionId": "%s", "TokenValue": "%s", "StreamUrl": "%s"}` , * details .SessionId , * details .TokenValue , * details .StreamUrl ),
88
- role . Region ,
90
+ region ,
89
91
"StartSession" ,
90
92
"" , // No Profile
91
93
fmt .Sprintf (`{"Target": "%s"}` , instanceId ),
92
- fmt .Sprintf ("https://ssm.%s.amazonaws.com" , role . Region ),
94
+ fmt .Sprintf ("https://ssm.%s.amazonaws.com" , region ),
93
95
)
94
96
command .Stdin = os .Stdin
95
97
command .Stdout = nil
@@ -117,11 +119,11 @@ func rsyncClean(role *credentials.Role, instanceId string) {
117
119
command := exec .Command (
118
120
binaryPath ,
119
121
fmt .Sprintf (`{"SessionId": "%s", "TokenValue": "%s", "StreamUrl": "%s"}` , * details .SessionId , * details .TokenValue , * details .StreamUrl ),
120
- role . Region ,
122
+ region ,
121
123
"StartSession" ,
122
124
"" , // No Profile
123
125
fmt .Sprintf (`{"Target": "%s"}` , instanceId ),
124
- fmt .Sprintf ("https://ssm.%s.amazonaws.com" , role . Region ),
126
+ fmt .Sprintf ("https://ssm.%s.amazonaws.com" , region ),
125
127
)
126
128
command .Stdin = os .Stdin
127
129
command .SysProcAttr = & syscall.SysProcAttr {Setpgid : true , Foreground : true }
@@ -146,11 +148,11 @@ func rsyncPortForward(role *credentials.Role, instanceId string) {
146
148
command := exec .Command (
147
149
binaryPath ,
148
150
fmt .Sprintf (`{"SessionId": "%s", "TokenValue": "%s", "StreamUrl": "%s"}` , * details .SessionId , * details .TokenValue , * details .StreamUrl ),
149
- role . Region ,
151
+ region ,
150
152
"StartSession" ,
151
153
"" , // No Profile
152
154
fmt .Sprintf (`{"Target": "%s"}` , instanceId ),
153
- fmt .Sprintf ("https://ssm.%s.amazonaws.com" , role . Region ),
155
+ fmt .Sprintf ("https://ssm.%s.amazonaws.com" , region ),
154
156
)
155
157
command .Stdin = os .Stdin
156
158
command .Stdout = nil
@@ -224,17 +226,30 @@ var syncCmd = &cobra.Command{
224
226
continue
225
227
}
226
228
}
229
+ if region == "" {
230
+ region = role .Region
231
+ }
227
232
if instanceId == "" {
228
- if instanceId , action , err = tui .SelectInstance (role , searchTerm ); err != nil {
233
+ if instanceId , action , err = tui .SelectInstance (role , region , searchTerm ); err != nil {
229
234
ExitWithError (19 , "failed to pick an instance" , err )
230
235
} else if action == "back" {
231
236
goBack ()
232
237
continue
233
238
}
234
239
}
235
- fmt .Println ("Remote Destination: /root/knox-sync" )
236
- fmt .Printf ("Example Command: rsync -P ./dump.sql ./release.tar.gz rsync://127.0.0.1:%d/sync\n " , localPort )
237
- fmt .Println ()
240
+
241
+ yellow := color .ToForeground (YellowColor ).Decorator ()
242
+ gray := color .ToForeground (LightGrayColor ).Decorator ()
243
+ title := TitleStyle .Decorator ()
244
+ DefaultStyle .Printfln ("" )
245
+ DefaultStyle .Printfln ("%s %s" , title ("SSO Session: " ), gray (role .SessionName ))
246
+ DefaultStyle .Printfln ("%s %s" , title ("Region: " ), gray (region ))
247
+ DefaultStyle .Printfln ("%s %s" , title ("Account ID: " ), gray (role .AccountId ))
248
+ DefaultStyle .Printfln ("%s %s" , title ("Role Name: " ), gray (role .Name ))
249
+ DefaultStyle .Printfln ("%s %s" , title ("Instance ID: " ), gray (instanceId ))
250
+ DefaultStyle .Printfln ("%s %s" , title ("Remote Destination: " ), gray ("/root/knox-sync" ))
251
+ DefaultStyle .Printfln ("%s %s" , title ("Example Command: " ), yellow ("rsync -P ./dump.sql ./release.tar.gz rsync://127.0.0.1:%d/sync\n " , localPort ))
252
+
238
253
defer rsyncClean (role , instanceId )
239
254
defer func () {
240
255
fmt .Println ("\n Cleaning up..." )
@@ -257,6 +272,7 @@ func init() {
257
272
syncCmd .Flags ().StringVarP (& accountId , "account-id" , "a" , accountId , "AWS account ID" )
258
273
syncCmd .Flags ().StringVarP (& roleName , "role-name" , "r" , roleName , "AWS role name" )
259
274
syncCmd .Flags ().StringVarP (& instanceId , "instance-id" , "i" , instanceId , "EC2 instance ID" )
275
+ syncCmd .Flags ().StringVar (& region , "region" , region , "Region for quering instances" )
260
276
syncCmd .Flags ().Uint16VarP (& rsyncPort , "rsync-port" , "P" , rsyncPort , "rsync port" )
261
277
syncCmd .Flags ().Uint16VarP (& localPort , "local-port" , "p" , localPort , "local port" )
262
278
syncCmd .Flags ().BoolVarP (& lastUsed , "last-used" , "l" , lastUsed , "select last used credentials" )
0 commit comments