Skip to content

Commit

Permalink
Add init command
Browse files Browse the repository at this point in the history
  • Loading branch information
swalkinshaw committed Sep 15, 2019
1 parent 5b75eea commit 4d90b00
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
85 changes: 85 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package cmd

import (
"fmt"
"strings"
"time"

"github.com/briandowns/spinner"
"github.com/mitchellh/cli"
"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
}

switch len(args) {
case 0:
default:
c.UI.Error(fmt.Sprintf("Error: too many arguments (expected 0, got %d)\n", len(args)))
c.UI.Output(c.Help())
return 1
}

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("✓ Virtualenv created\n")

pip := execCommand("pip", "install", "-r", "requirements.txt")

s := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
s.Suffix = " Installing pip dependencies (pip install -r requirements.txt) ..."
s.FinalMSG = "✓ Dependencies installed\n"
s.Start()

err = pip.Run()

if err != nil {
s.Stop()
c.UI.Error(fmt.Sprintf("Error installing pip requirements", err))
return 1
}

s.Stop()

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. creates a virtual environment via virtualenv to manage dependencies
2. installs dependencies via pip (specified by requirements.txt in your Trellis project)
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)
}
3 changes: 3 additions & 0 deletions cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ func (c *NewCommand) Run(args []string) int {
return 1
}

initCommand := &InitCommand{UI: c.UI, Trellis: c.trellis}
initCommand.Run([]string{})

// Update default configs
for env, config := range c.trellis.Environments {
c.trellis.UpdateDefaultConfig(config, c.name, c.host, env)
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func main() {
"info": func() (cli.Command, error) {
return &cmd.InfoCommand{UI: ui, Trellis: trellis}, nil
},
"init": func() (cli.Command, error) {
return &cmd.InitCommand{UI: ui, Trellis: trellis}, nil
},
"new": func() (cli.Command, error) {
return cmd.NewNewCommand(ui, trellis, c.Version), nil
},
Expand Down
4 changes: 4 additions & 0 deletions trellis/virtualenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func (v *Virtualenv) Create() (err error) {

cmd.Args = append(cmd.Args, v.Path)

if v.Initialized() {
return nil
}

err = cmd.Run()
if err != nil {
return err
Expand Down

0 comments on commit 4d90b00

Please sign in to comment.