Skip to content

Commit 7a6901e

Browse files
authored
Merge pull request #1136 from 99designs/feedback-on-shell
Provide feedback to user when prudent
2 parents 76d15f7 + 439d7a1 commit 7a6901e

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

cli/exec.go

+28-8
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ func ConfigureExecCommand(app *kingpin.Application, a *AwsVault) {
108108
StringVar(&input.ProfileName)
109109

110110
cmd.Arg("cmd", "Command to execute, defaults to $SHELL").
111-
Default(os.Getenv("SHELL")).
112111
StringVar(&input.Command)
113112

114113
cmd.Arg("args", "Command arguments").
@@ -209,14 +208,15 @@ func updateEnvForAwsVault(env environ, profileName string, region string) enviro
209208
}
210209

211210
func execEc2Server(input ExecCommandInput, config *vault.Config, credsProvider aws.CredentialsProvider) error {
211+
fmt.Fprintf(os.Stderr, "aws-vault: Starting an EC2 credential server.\n")
212212
if err := server.StartEc2CredentialsServer(context.TODO(), credsProvider, config.Region); err != nil {
213213
return fmt.Errorf("Failed to start credential server: %w", err)
214214
}
215215

216216
env := environ(os.Environ())
217217
env = updateEnvForAwsVault(env, input.ProfileName, config.Region)
218218

219-
return execCmd(input.Command, input.Args, env)
219+
return doRunCmd(input.Command, input.Args, env)
220220
}
221221

222222
func execEcsServer(input ExecCommandInput, config *vault.Config, credsProvider aws.CredentialsProvider) error {
@@ -237,7 +237,9 @@ func execEcsServer(input ExecCommandInput, config *vault.Config, credsProvider a
237237
env.Set("AWS_CONTAINER_CREDENTIALS_FULL_URI", ecsServer.BaseURL())
238238
env.Set("AWS_CONTAINER_AUTHORIZATION_TOKEN", ecsServer.AuthToken())
239239

240-
return execCmd(input.Command, input.Args, env)
240+
fmt.Fprintf(os.Stderr, "aws-vault: Starting an ECS credential server; your app's AWS sdk must support AWS_CONTAINER_CREDENTIALS_FULL_URI.\n")
241+
242+
return doRunCmd(input.Command, input.Args, env)
241243
}
242244

243245
func execEnvironment(input ExecCommandInput, config *vault.Config, credsProvider aws.CredentialsProvider) error {
@@ -265,10 +267,10 @@ func execEnvironment(input ExecCommandInput, config *vault.Config, credsProvider
265267
}
266268

267269
if !supportsExecSyscall() {
268-
return execCmd(input.Command, input.Args, env)
270+
return doRunCmd(input.Command, input.Args, env)
269271
}
270272

271-
return execSyscall(input.Command, input.Args, env)
273+
return doExecSyscall(input.Command, input.Args, env)
272274
}
273275

274276
// environ is a slice of strings representing the environment, in the form "key=value".
@@ -291,8 +293,21 @@ func (e *environ) Set(key, val string) {
291293
*e = append(*e, key+"="+val)
292294
}
293295

294-
func execCmd(command string, args []string, env []string) error {
295-
log.Printf("Starting child process: %s %s", command, strings.Join(args, " "))
296+
func getDefaultShell() string {
297+
command := os.Getenv("SHELL")
298+
if command == "" {
299+
command = "/bin/sh"
300+
}
301+
return command
302+
}
303+
304+
func doRunCmd(command string, args []string, env []string) error {
305+
if command == "" {
306+
command = getDefaultShell()
307+
fmt.Fprintf(os.Stderr, "aws-vault: Starting a subshell %s\n", command)
308+
}
309+
310+
log.Printf("Starting subprocess: %s %s", command, strings.Join(args, " "))
296311

297312
cmd := osexec.Command(command, args...)
298313
cmd.Stdin = os.Stdin
@@ -328,7 +343,12 @@ func supportsExecSyscall() bool {
328343
return runtime.GOOS == "linux" || runtime.GOOS == "darwin" || runtime.GOOS == "freebsd" || runtime.GOOS == "openbsd"
329344
}
330345

331-
func execSyscall(command string, args []string, env []string) error {
346+
func doExecSyscall(command string, args []string, env []string) error {
347+
if command == "" {
348+
command = getDefaultShell()
349+
fmt.Fprintf(os.Stderr, "aws-vault: Starting a subshell %s\n", command)
350+
}
351+
332352
log.Printf("Exec command %s %s", command, strings.Join(args, " "))
333353

334354
argv0, err := osexec.LookPath(command)

0 commit comments

Comments
 (0)