-
Notifications
You must be signed in to change notification settings - Fork 27
/
run.go
63 lines (54 loc) · 1.31 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
package cli
import (
"fmt"
"os"
"github.com/bitrise-io/envman/env"
"github.com/bitrise-io/envman/models"
"github.com/bitrise-io/go-utils/command"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
// CommandModel ...
type CommandModel struct {
Command string
Arguments []string
Environments []models.EnvironmentItemModel
}
func run(c *cli.Context) error {
if len(c.Args()) == 0 {
log.Fatal("[ENVMAN] - No command specified")
}
cmd, err := createCommand(CurrentEnvStoreFilePath, c.Args())
if err != nil {
log.Errorf("command failed: %s", err)
}
cmd.SetStdin(os.Stdin)
cmd.SetStdout(os.Stdout)
cmd.SetStderr(os.Stderr)
exitCode, err := cmd.RunAndReturnExitCode()
if err != nil {
log.Errorf("command failed: %s", err)
}
if err != nil && exitCode == 0 {
exitCode = 1
}
os.Exit(exitCode)
return nil
}
func createCommand(envStorePth string, args []string) (*command.Model, error) {
if len(args) == 0 {
return nil, fmt.Errorf("no command specified")
}
cmdEnvs, err := ReadAndEvaluateEnvs(envStorePth, &env.DefaultEnvironmentSource{})
if err != nil {
return nil, fmt.Errorf("failed to load EnvStore: %s", err)
}
cmdName := args[0]
var cmdArgs []string
if len(args) > 1 {
cmdArgs = args[1:]
}
cmd := command.New(cmdName, cmdArgs...)
cmd.SetEnvs(cmdEnvs...)
return cmd, nil
}