-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
153 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |