|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "os/exec" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/mitchellh/cli" |
| 10 | + "github.com/posener/complete" |
| 11 | + "trellis-cli/trellis" |
| 12 | +) |
| 13 | + |
| 14 | +type DeployCommand struct { |
| 15 | + UI cli.Ui |
| 16 | + Trellis *trellis.Trellis |
| 17 | +} |
| 18 | + |
| 19 | +func (c *DeployCommand) Run(args []string) int { |
| 20 | + c.Trellis.EnforceValid(c.UI) |
| 21 | + |
| 22 | + var environment string |
| 23 | + var siteName string |
| 24 | + |
| 25 | + switch len(args) { |
| 26 | + case 0: |
| 27 | + c.UI.Output(c.Help()) |
| 28 | + return 1 |
| 29 | + case 1: |
| 30 | + c.UI.Error("Missing SITE argument\n") |
| 31 | + c.UI.Output(c.Help()) |
| 32 | + return 1 |
| 33 | + case 2: |
| 34 | + environment = args[0] |
| 35 | + siteName = args[1] |
| 36 | + default: |
| 37 | + c.UI.Error(fmt.Sprintf("Error: too many arguments (expected 2, got %d)\n", len(args))) |
| 38 | + c.UI.Output(c.Help()) |
| 39 | + return 1 |
| 40 | + } |
| 41 | + |
| 42 | + deploy := exec.Command("./bin/deploy.sh", environment, siteName) |
| 43 | + logCmd(deploy, true) |
| 44 | + err := deploy.Run() |
| 45 | + |
| 46 | + if err != nil { |
| 47 | + log.Fatal(err) |
| 48 | + } |
| 49 | + |
| 50 | + return 0 |
| 51 | +} |
| 52 | + |
| 53 | +func (c *DeployCommand) Synopsis() string { |
| 54 | + return "Deploys a site to the specified environment." |
| 55 | +} |
| 56 | + |
| 57 | +func (c *DeployCommand) Help() string { |
| 58 | + helpText := ` |
| 59 | +Usage: trellis deploy [options] ENVIRONMENT SITE |
| 60 | +
|
| 61 | +Deploys a site to the specified environment. |
| 62 | +
|
| 63 | +See https://roots.io/trellis/docs/deploys/ for more information on deploys with Trellis. |
| 64 | +
|
| 65 | +Deploy a site to production: |
| 66 | +
|
| 67 | + $ trellis deploy production example.com |
| 68 | +
|
| 69 | +Arguments: |
| 70 | + ENVIRONMENT Name of environment (ie: production) |
| 71 | + SITE Name of the site (ie: example.com) |
| 72 | +
|
| 73 | +Options: |
| 74 | + -h, --help show this help |
| 75 | +` |
| 76 | + |
| 77 | + return strings.TrimSpace(helpText) |
| 78 | +} |
| 79 | + |
| 80 | +func (c *DeployCommand) AutocompleteArgs() complete.Predictor { |
| 81 | + return c.PredictSite() |
| 82 | +} |
| 83 | + |
| 84 | +func (c *DeployCommand) AutocompleteFlags() complete.Flags { |
| 85 | + return complete.Flags{} |
| 86 | +} |
| 87 | + |
| 88 | +func (c *DeployCommand) PredictSite() complete.PredictFunc { |
| 89 | + return func(args complete.Args) []string { |
| 90 | + switch len(args.Completed) { |
| 91 | + case 1: |
| 92 | + return c.Trellis.EnvironmentNames() |
| 93 | + case 2: |
| 94 | + return c.Trellis.SiteNamesFromEnvironment(args.LastCompleted) |
| 95 | + default: |
| 96 | + return []string{} |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments