forked from roots/trellis-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.go
109 lines (82 loc) · 2.74 KB
/
init.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
package cmd
import (
"fmt"
"github.com/fatih/color"
"strings"
"time"
"github.com/briandowns/spinner"
"github.com/mitchellh/cli"
"github.com/roots/trellis-cli/trellis"
)
type InitCommand struct {
UI cli.Ui
Trellis *trellis.Trellis
}
func (c *InitCommand) Run(args []string) int {
if err := c.Trellis.LoadProject(); err != nil {
c.UI.Error(err.Error())
return 1
}
commandArgumentValidator := &CommandArgumentValidator{required: 0, optional: 0}
commandArgumentErr := commandArgumentValidator.validate(args)
if commandArgumentErr != nil {
c.UI.Error(commandArgumentErr.Error())
c.UI.Output(c.Help())
return 1
}
if err := c.Trellis.CreateConfigDir(); err != nil {
c.UI.Error(err.Error())
return 1
}
if ok, _ := c.Trellis.Virtualenv.Installed(); !ok {
c.UI.Info("virtualenv not found")
s := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
s.Suffix = " Installing virtualenv..."
s.Start()
c.Trellis.Virtualenv.Install()
s.Stop()
c.UI.Info(color.GreenString("✓ virtualenv installed"))
}
c.UI.Info(fmt.Sprintf("Creating virtualenv in %s", c.Trellis.Virtualenv.Path))
err := c.Trellis.Virtualenv.Create()
if err != nil {
c.UI.Error(fmt.Sprintf("Error creating virtualenv: %s", err))
return 1
}
c.UI.Info(color.GreenString("✓ Virtualenv created"))
pip := CommandExecWithStderrOnly("pip", []string{"install", "-r", "requirements.txt"}, c.UI)
s := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
s.Suffix = " Installing pip dependencies (pip install -r requirements.txt) ..."
s.Start()
err = pip.Run()
s.Stop()
if err != nil {
return 1
}
c.UI.Info(color.GreenString("✓ Dependencies installed"))
return 0
}
func (c *InitCommand) Synopsis() string {
return "Initializes an existing Trellis project"
}
func (c *InitCommand) Help() string {
helpText := `
Usage: trellis init [options]
Initializes an existing Trellis project to be managed by trellis-cli.
The initialization process does two things:
1. installs virtualenv if necessary (see below for details)
2. creates a virtual environment specific to the project to manage dependencies
3. installs dependencies via pip (specified by requirements.txt in your Trellis project)
trellis-cli will attempt to use an already installed method to manage virtualenvs
and only fallback to installing virtualenv if necessary:
1. if python3 is installed, use built-in virtualenv feature
2. use virtualenv command if available
3. finally install virtualenv at $HOME/.trellis/virtualenv
To learn more about virtual environments, see https://docs.python.org/3/tutorial/venv.html
This command is idempotent meaning it can be run multiple times without side-effects.
$ trellis init
Options:
-h, --help show this help
`
return strings.TrimSpace(helpText)
}