forked from roots/trellis-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.go
73 lines (57 loc) · 1.27 KB
/
exec.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
package cmd
import (
"fmt"
"os"
"os/exec"
"strings"
"syscall"
"github.com/mitchellh/cli"
"github.com/roots/trellis-cli/trellis"
)
type ExecCommand struct {
UI cli.Ui
Trellis *trellis.Trellis
}
func (c *ExecCommand) Run(args []string) int {
if err := c.Trellis.LoadProject(); err != nil {
c.UI.Error(err.Error())
return 1
}
var command string
var cmdArgs []string
switch len(args) {
case 0:
c.UI.Error("Error: missing COMMAND argument\n")
c.UI.Output(c.Help())
return 1
default:
command = args[0]
cmdArgs = args
}
cmdPath, err := exec.LookPath(command)
if err != nil {
c.UI.Error(fmt.Sprintf("Error: %s not found", command))
return 1
}
env := os.Environ()
execErr := syscall.Exec(cmdPath, cmdArgs, env)
if execErr != nil {
c.UI.Error(fmt.Sprintf("Error running %s: %s", args[0], execErr))
return 1
}
return 0
}
func (c *ExecCommand) Synopsis() string {
return "Exec runs a command in the Trellis virtualenv"
}
func (c *ExecCommand) Help() string {
helpText := `
Usage: trellis exec [options]
Exec activates the Trellis virtual environment and executes the command specified.
Run a custom ansible-playbook command:
$ trellis exec ansible-playbook --version
Options:
-h, --help show this help
`
return strings.TrimSpace(helpText)
}