This repository has been archived by the owner on Apr 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
launch.go
74 lines (61 loc) · 1.55 KB
/
launch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import "os"
import "fmt"
import "errors"
import "os/exec"
import "github.com/tj/go-debug"
import "github.com/aws/aws-sdk-go/aws/credentials"
var debugLaunch = debug.Debug("oktad:launch")
// runs some program
func launch(cmd string, args []string, creds *credentials.Credentials) error {
debugLaunch("launching program %s with args %s", cmd, args)
e := exec.Command(
cmd,
args...,
)
cv, err := creds.Get()
if err != nil {
fmt.Println("wtf, ", err)
return err
}
e.Stderr = os.Stderr
e.Stdout = os.Stdout
e.Stdin = os.Stdin
e.Env = append(e.Env, os.Environ()...)
envVarFmt := "%s=%s"
e.Env = append(
e.Env,
fmt.Sprintf(envVarFmt, "AWS_SESSION_TOKEN", cv.SessionToken),
fmt.Sprintf(envVarFmt, "AWS_ACCESS_KEY_ID", cv.AccessKeyID),
fmt.Sprintf(envVarFmt, "AWS_SECRET_ACCESS_KEY", cv.SecretAccessKey),
)
return e.Run()
}
// prepares arguments for launching & then does it
// expected input includes arguments from the command line once the program name
// (in this case ./oktad) is removed
// and then AWS credentials to put in the environment of the launched program
func prepAndLaunch(args []string, creds *credentials.Credentials) error {
// WHOA!
var cmd string
var cArgs []string
if len(args) < 2 {
return errors.New("No program specified!")
}
for i, a := range args[1:] {
if a != "--" {
cmd = a
if len(args) > (i + 2) {
cArgs = args[i+2:]
} else {
cArgs = []string{}
}
break
}
}
err := launch(cmd, cArgs, creds)
if err != nil {
debugLaunch("caught error from launcher, %s", err)
}
return err
}