/
run.go
155 lines (127 loc) · 3.42 KB
/
run.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"context"
"fmt"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"github.com/urfave/cli"
"github.com/manifoldco/go-manifold"
"github.com/manifoldco/manifold-cli/api"
"github.com/manifoldco/manifold-cli/clients"
"github.com/manifoldco/manifold-cli/errs"
"github.com/manifoldco/manifold-cli/generated/marketplace/models"
"github.com/manifoldco/manifold-cli/middleware"
"github.com/manifoldco/manifold-cli/session"
)
func init() {
runCmd := cli.Command{
Name: "run",
Usage: "Run a process and inject secrets into its environment",
Category: "CONFIGURATION",
Action: middleware.Chain(middleware.EnsureSession, middleware.LoadDirPrefs,
middleware.LoadTeamPrefs, run),
Flags: append(teamFlags, []cli.Flag{
projectFlag(),
}...),
}
cmds = append(cmds, runCmd)
}
func run(cliCtx *cli.Context) error {
ctx := context.Background()
args := cliCtx.Args()
if len(args) == 0 {
return errs.NewUsageExitError(cliCtx, fmt.Errorf("A command is required"))
} else if len(args) == 1 { //only one arg, maybe it was quoted
args = strings.Split(args[0], " ")
}
projectName, err := validateName(cliCtx, "project")
if err != nil {
return err
}
teamID, err := validateTeamID(cliCtx)
if err != nil {
return err
}
client, err := api.New(api.Analytics, api.Marketplace)
if err != nil {
return err
}
cMap := make(map[manifold.ID][]*models.Credential)
if projectName != "" {
p, err := clients.FetchProjectByLabel(ctx, client.Marketplace, teamID, projectName)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Could not retrieve project: %s", err), -1)
}
cMap, err = fetchProjectCredentials(ctx, client.Marketplace, p, true)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Could not retrieve credentials: %s", err), -1)
}
} else {
rs, err := clients.FetchResources(ctx, client.Marketplace, teamID, projectName)
if err != nil {
return cli.NewExitError("Could not retrieve resources: "+err.Error(), -1)
}
cMap, err = fetchResourceCredentials(ctx, client.Marketplace, rs, true)
if err != nil {
return cli.NewExitError("Could not retrieve credentials: "+err.Error(), -1)
}
}
credentials, err := flattenCMap(cMap)
if err != nil {
return cli.NewExitError("Could not flatten credential map: "+err.Error(), -1)
}
params := map[string]string{}
if projectName != "" {
params["project"] = projectName
}
client.Analytics.Track(ctx, "Project Run", ¶ms)
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = filterEnv()
for name, value := range credentials {
cmd.Env = append(cmd.Env, name+"="+value)
}
err = cmd.Start()
if err != nil {
return cli.NewExitError("Could not execute command: "+err.Error(), -1)
}
done := make(chan bool)
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c)
select {
case s := <-c:
cmd.Process.Signal(s)
case <-done:
signal.Stop(c)
return
}
}()
err = cmd.Wait()
close(done)
if err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
os.Exit(status.ExitStatus())
return nil
}
return err
}
}
return nil
}
func filterEnv() []string {
env := []string{}
for _, e := range os.Environ() {
if strings.HasPrefix(e, session.EnvManifoldEmail+"=") || strings.HasPrefix(e, session.EnvManifoldPass+"=") {
continue
}
env = append(env, e)
}
return env
}