Skip to content

Commit

Permalink
vega down & destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
WSMathias committed Aug 10, 2020
1 parent 5035ff2 commit 1a9936b
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 2 deletions.
25 changes: 25 additions & 0 deletions cmd/destroy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cmd

import (
"fmt"
"io"

"github.com/spf13/cobra"
"github.com/srijanone/vega/pkg/compose"
)

func newDestroyCmd(out io.Writer) *cobra.Command {
const downDesc = "Delete application and persisted data"

downCmd := &cobra.Command{
Use: "down",
Short: downDesc,
Long: downDesc,
Run: func(cmd *cobra.Command, args []string) {
fmt.Fprintln(out, "Deleting the application")
compose.Destroy(out, args...)
},
}

return downCmd
}
2 changes: 2 additions & 0 deletions cmd/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"

"github.com/spf13/cobra"
docker "github.com/srijanone/vega/pkg/docker"
tilt "github.com/srijanone/vega/pkg/tilt"
)

Expand All @@ -18,6 +19,7 @@ func newDownCmd(out io.Writer) *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
fmt.Fprintln(out, "Stopping the application")
tilt.Down(out, args...)
docker.DeleteImagesByLabel(out, "builtby=tilt")
},
}

Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func newRootCmd(out io.Writer, in io.Reader) *cobra.Command {
cmd.AddCommand(newStarterKitCmd(out))
cmd.AddCommand(newUpCmd(out))
cmd.AddCommand(newDownCmd(out))
cmd.AddCommand(newDestroyCmd(out))
cmd.AddCommand(newRepoCmd(out))
cmd.AddCommand(newHooksCmd(out))

Expand Down
15 changes: 13 additions & 2 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ import (
"io"

"github.com/spf13/cobra"
common "github.com/srijanone/vega/pkg/common"
compose "github.com/srijanone/vega/pkg/compose"
tilt "github.com/srijanone/vega/pkg/tilt"
)

var watch, noBrowser bool
var port string

func isDrupal() bool {
signature := "composer.json"
result, _ := common.Exists(signature)
return result
}

func newUpCmd(out io.Writer) *cobra.Command {
const upDesc = "start the application"

Expand All @@ -19,18 +27,21 @@ func newUpCmd(out io.Writer) *cobra.Command {
Short: upDesc,
Long: upDesc,
Run: func(cmd *cobra.Command, args []string) {
fmt.Fprintln(out, "Running the application")
upArgs := []string{"--port", port}
if noBrowser {
upArgs = append(upArgs, "--no-browser")
}
if watch == false {
upArgs = append(upArgs, "--watch", "false")
}
if isDrupal() {
fmt.Fprintln(out, "Building the application")
compose.Run(out, "cli")
}
fmt.Fprintln(out, "Running the application")
tilt.Up(out, upArgs...)
},
}

flags := upCmd.Flags()
flags.BoolVar(&noBrowser, "no-browser", false, "If true, Web UI will not open on startup")
flags.BoolVar(&watch, "watch", true, "If true, services will be automatically rebuilt and redeployed when files change")
Expand Down
65 changes: 65 additions & 0 deletions pkg/compose/compose.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package compose

import (
"errors"
"fmt"
"io"
"os"
"os/exec"
)

const (
commandName = "docker-compose"
RequiredText = `
docker-compose is not installed, which is required to run the application.
`
InstallInstructions = `
Install using:
Linux : curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Mac : Install Docker Desktop
And more info. can be found at: https://docs.docker.com/get-docker/
`
)

func IsInstalled() bool {
_, err := exec.LookPath(commandName)
return err == nil
}

func Up(out io.Writer, arguments ...string) {
arguments = append([]string{"up"}, arguments...)
execute(out, arguments...)
}

func Down(out io.Writer, arguments ...string) {
arguments = append([]string{"down"}, arguments...)
execute(out, arguments...)
}

func Destroy(out io.Writer, arguments ...string) {
arguments = append([]string{"down", "--volumes"}, arguments...)
execute(out, arguments...)
}

func Run(out io.Writer, arguments ...string) {
arguments = append([]string{"run", "--rm"}, arguments...)
execute(out, arguments...)
}

func execute(out io.Writer, arguments ...string) error {
if !IsInstalled() {
fmt.Fprintf(out, RequiredText)
fmt.Fprintf(out, InstallInstructions)
return errors.New("docker-compose is not installed on system")
}

command := exec.Command(commandName, arguments...)
command.Stdout = out
command.Stderr = os.Stderr
err := command.Run()
if err != nil {
return err
}
return nil
}
47 changes: 47 additions & 0 deletions pkg/docker/docker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package docker

import (
"errors"
"fmt"
"io"
"os"
"os/exec"
)

const (
commandName = "docker"
RequiredText = `
docker is not installed, which is required to run the application.
`
InstallInstructions = `
Instructions to install can be found at: https://docs.docker.com/get-docker/
`
)

func IsInstalled() bool {
_, err := exec.LookPath(commandName)
return err == nil
}

func DeleteImagesByLabel(out io.Writer, label string) {
dockerCmd := `docker image rm $(docker image ls -q --filter "label=` + label + `") 2> /dev/null`
arguments := append([]string{"-c"}, dockerCmd)
execute(out, arguments...)
}

func execute(out io.Writer, arguments ...string) error {
if !IsInstalled() {
fmt.Fprintf(out, RequiredText)
fmt.Fprintf(out, InstallInstructions)
return errors.New("docker is not installed on system")
}
// TODO: Windows compatibility
command := exec.Command("bash", arguments...)
command.Stdout = out
command.Stderr = os.Stderr
err := command.Run()
if err != nil {
return err
}
return nil
}

0 comments on commit 1a9936b

Please sign in to comment.