Skip to content

Commit 98e9e66

Browse files
authored
Merge pull request #245 from roots/new-command-improve-venv-failure
Improve init and virtual error messages
2 parents 5c97f83 + 70d7beb commit 98e9e66

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

cmd/init.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"flag"
66
"fmt"
7-
"os"
87
"os/exec"
98
"strings"
109

@@ -58,8 +57,9 @@ func (c *InitCommand) Run(args []string) int {
5857
}
5958

6059
c.UI.Info("Initializing project\n")
60+
ok, virtualenvCmd := c.Trellis.Virtualenv.Installed()
6161

62-
if ok, _ := c.Trellis.Virtualenv.Installed(); !ok {
62+
if !ok {
6363
c.UI.Info("virtualenv not found")
6464
spinner := NewSpinner(
6565
SpinnerCfg{
@@ -68,8 +68,20 @@ func (c *InitCommand) Run(args []string) int {
6868
},
6969
)
7070
spinner.Start()
71-
c.Trellis.Virtualenv.Install()
71+
_, err := c.Trellis.Virtualenv.Install()
7272
spinner.Stop()
73+
74+
c.UI.Error(err.Error())
75+
c.UI.Error("")
76+
c.UI.Error("Project initialization failed due to the error above.")
77+
c.UI.Error("")
78+
c.UI.Error("trellis-cli attempted to install virtualenv as a fallback but failed.")
79+
c.UI.Error("Without virtualenv, a Python virtual environment cannot be created and the required dependencies (eg: Ansible) can't be installed either.")
80+
c.UI.Error("")
81+
c.UI.Error("There are two options:")
82+
c.UI.Error(" 1. Ensure Python 3 is installed and the `python3` command works. trellis-cli will use python3's built-in venv feature.")
83+
c.UI.Error(fmt.Sprintf(" 2. Disable trellis-cli's virtual env feature by setting this env variable: export %s=false", trellis.TrellisVenvEnvName))
84+
return 1
7385
}
7486

7587
if c.force {
@@ -80,7 +92,7 @@ func (c *InitCommand) Run(args []string) int {
8092
},
8193
)
8294
spinner.Start()
83-
err := os.RemoveAll(c.Trellis.Virtualenv.Path)
95+
err := c.Trellis.Virtualenv.Delete()
8496

8597
if err != nil {
8698
spinner.StopFail()
@@ -105,6 +117,19 @@ func (c *InitCommand) Run(args []string) int {
105117
if err != nil {
106118
spinner.StopFail()
107119
c.UI.Error(err.Error())
120+
c.UI.Error("")
121+
c.UI.Error("Project initialization failed due to the error above.")
122+
c.UI.Error("")
123+
c.UI.Error("trellis-cli tried to create a virtual environment but failed.")
124+
c.UI.Error(fmt.Sprintf(" => %s", virtualenvCmd.String()))
125+
c.UI.Error("")
126+
c.UI.Error("Without a Python virtual environment the required dependencies (eg: Ansible) can't be installed.")
127+
c.UI.Error("")
128+
c.UI.Error("There are two options:")
129+
c.UI.Error(" 1. Ensure Python 3 is installed and the `python3` command works. trellis-cli will use python3's built-in venv feature.")
130+
c.UI.Error(fmt.Sprintf(" 2. Disable trellis-cli's virtual env feature by setting this env variable: export %s=false", trellis.TrellisVenvEnvName))
131+
c.UI.Error("")
132+
c.UI.Error("This could also be a bug in trellis-cli. Please create an issue at https://github.com/roots/trellis-cli and include the output above.")
108133
return 1
109134
}
110135

cmd/new.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,11 @@ func (c *NewCommand) Run(args []string) int {
122122

123123
if !c.skipVirtualenv {
124124
initCommand := NewInitCommand(c.UI, c.trellis)
125-
initCommand.Run([]string{})
125+
code := initCommand.Run([]string{})
126+
127+
if code != 0 {
128+
return 1
129+
}
126130
}
127131

128132
// Update default configs

trellis/virtualenv.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ func (v *Virtualenv) Deactivate() {
6969
os.Setenv(PathEnvName, v.OldPath)
7070
}
7171

72+
func (v *Virtualenv) Delete() error {
73+
return os.RemoveAll(v.Path)
74+
}
75+
7276
func (v *Virtualenv) LocalPath() string {
7377
configHome := os.Getenv("XDG_CONFIG_HOME")
7478

@@ -96,17 +100,17 @@ func (v *Virtualenv) Initialized() bool {
96100
return true
97101
}
98102

99-
func (v *Virtualenv) Install() string {
103+
func (v *Virtualenv) Install() (installPath string, err error) {
100104
localPath := v.LocalPath()
101105
configDir := filepath.Dir(localPath)
102106

103-
if _, err := os.Stat(configDir); os.IsNotExist(err) {
107+
if _, err = os.Stat(configDir); os.IsNotExist(err) {
104108
if err = os.MkdirAll(configDir, 0755); err != nil {
105-
log.Fatal(err)
109+
return "", err
106110
}
107111
}
108112

109-
return github.DownloadRelease("pypa/virtualenv", "latest", os.TempDir(), localPath)
113+
return github.DownloadRelease("pypa/virtualenv", "latest", os.TempDir(), localPath), nil
110114
}
111115

112116
func (v *Virtualenv) Installed() (ok bool, cmd *exec.Cmd) {

0 commit comments

Comments
 (0)