Skip to content

Commit c5c918a

Browse files
authored
Merge pull request #19 from roots/improve-new-prompts
Improve new project prompts
2 parents a52fecd + 734a79d commit c5c918a

File tree

3 files changed

+49
-24
lines changed

3 files changed

+49
-24
lines changed

cmd/new.go

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"path/filepath"
1313
"strings"
1414

15+
"github.com/fatih/color"
1516
"github.com/mholt/archiver"
1617
"github.com/mitchellh/cli"
1718
"trellis-cli/trellis"
@@ -42,7 +43,6 @@ func (c *NewCommand) init() {
4243
}
4344

4445
func (c *NewCommand) Run(args []string) int {
45-
var name string
4646
var path string
4747

4848
if err := c.flags.Parse(args); err != nil {
@@ -53,24 +53,21 @@ func (c *NewCommand) Run(args []string) int {
5353

5454
switch len(args) {
5555
case 0:
56-
c.UI.Error("Missing NAME argument\n")
56+
c.UI.Error("Missing PATH argument\n")
5757
c.UI.Output(c.Help())
5858
return 1
5959
case 1:
60-
name = args[0]
61-
case 2:
62-
name = args[0]
63-
path = args[1]
60+
path = args[0]
6461
default:
65-
c.UI.Error(fmt.Sprintf("Error: too many arguments (expected 2, got %d)\n", len(args)))
62+
c.UI.Error(fmt.Sprintf("Error: too many arguments (expected 1, got %d)\n", len(args)))
6663
c.UI.Output(c.Help())
6764
return 1
6865
}
6966

7067
path, _ = filepath.Abs(path)
7168
_, err := os.Stat(path)
7269

73-
fmt.Println("Creating new Trellis project in", path)
70+
c.UI.Info(fmt.Sprintf("Creating new Trellis project in %s\n", path))
7471

7572
if err != nil {
7673
if os.IsNotExist(err) {
@@ -91,16 +88,17 @@ func (c *NewCommand) Run(args []string) int {
9188
}
9289
}
9390

94-
var host string
95-
host, err = c.UI.Ask(fmt.Sprintf("Enter main site host [default: %s]", name))
91+
name, err := askProjectName(c.UI)
92+
if err != nil {
93+
return 1
94+
}
9695

97-
if err == nil {
98-
if len(host) == 0 {
99-
host = name
100-
}
96+
host, err := askDomain(c.UI, name)
97+
if err != nil {
98+
return 1
10199
}
102100

103-
fmt.Println("Fetching latest versions of Trellis and Bedrock...")
101+
fmt.Println("\nFetching latest versions of Trellis and Bedrock...")
104102

105103
trellisPath := filepath.Join(path, "trellis")
106104
trellisVersion := downloadLatestRelease("roots/trellis", path, trellisPath)
@@ -127,7 +125,7 @@ func (c *NewCommand) Run(args []string) int {
127125
c.trellis.WriteVaultYaml(vault, filepath.Join("group_vars", env, "vault.yml"))
128126
}
129127

130-
fmt.Printf("\n%s project created with versions:\n", name)
128+
fmt.Printf("\n%s project created with versions:\n", color.GreenString(name))
131129
fmt.Printf(" Trellis v%s\n", trellisVersion)
132130
fmt.Printf(" Bedrock v%s\n", bedrockVersion)
133131

@@ -140,30 +138,27 @@ func (c *NewCommand) Synopsis() string {
140138

141139
func (c *NewCommand) Help() string {
142140
helpText := `
143-
Usage: trellis new NAME [PATH]
141+
Usage: trellis new [PATH]
144142
145-
Creates a new Trellis project in the path specified (defaults to current directory)
146-
using the latest versions of Trellis and Bedrock.
143+
Creates a new Trellis project in the path specified using the latest versions of Trellis and Bedrock.
147144
148145
This uses our recommended project structure detailed at
149146
https://roots.io/trellis/docs/installing-trellis/#create-a-project
150147
151148
Create a new project in the current directory:
152149
153-
$ trellis new example.com
150+
$ trellis new .
154151
155152
Create a new project in the target path:
156153
157-
$ trellis new example.com ~/dev/example.com
154+
$ trellis new ~/dev/example.com
158155
159156
Force create a new project in a non-empty target path:
160157
161-
$ trellis new --force example.com ~/dev/example.com
158+
$ trellis new --force ~/dev/example.com
162159
163160
Arguments:
164-
NAME Name of new Trellis project (ie: example.com)
165161
PATH Path to create new project in
166-
(default: .)
167162
168163
Options:
169164
--force (default: false) Forces the creation of the project even if the target path is not empty
@@ -178,6 +173,34 @@ func addTrellisFile(path string) error {
178173
return ioutil.WriteFile(path, []byte{}, 0666)
179174
}
180175

176+
func askProjectName(ui cli.Ui) (name string, err error) {
177+
name, err = ui.Ask(fmt.Sprintf("%s:", color.MagentaString("Project name")))
178+
179+
if err != nil {
180+
return "", err
181+
}
182+
183+
if len(name) == 0 {
184+
return askProjectName(ui)
185+
}
186+
187+
return name, nil
188+
}
189+
190+
func askDomain(ui cli.Ui, name string) (host string, err error) {
191+
host, err = ui.Ask(fmt.Sprintf("%s [%s]:", color.MagentaString("Site domain"), color.GreenString(name)))
192+
193+
if err != nil {
194+
return "", err
195+
}
196+
197+
if len(host) == 0 {
198+
return name, nil
199+
}
200+
201+
return host, nil
202+
}
203+
181204
func downloadLatestRelease(repo string, path string, dest string) string {
182205
release := fetchLatestRelease(repo)
183206

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module trellis-cli
22

33
require (
44
github.com/dsnet/compress v0.0.0-20171208185109-cc9eb1d7ad76 // indirect
5+
github.com/fatih/color v1.7.0
56
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db // indirect
67
github.com/google/pprof v0.0.0-20190109223431-e84dfd68c163 // indirect
78
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6 // indirect

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ func main() {
1313
c.Args = os.Args[1:]
1414

1515
ui := &cli.ColoredUi{
16+
ErrorColor: cli.UiColorRed,
1617
Ui: &cli.BasicUi{
1718
Reader: os.Stdin,
1819
Writer: os.Stdout,

0 commit comments

Comments
 (0)