This commit is contained in:
parent
a11bbe2eec
commit
17221855cc
8 changed files with 177 additions and 46 deletions
20
cli/build.go
20
cli/build.go
|
@ -1,21 +1,27 @@
|
|||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"log"
|
||||
podman "spud/podman"
|
||||
service_definition "spud/service_definition"
|
||||
)
|
||||
|
||||
var build = &cobra.Command{
|
||||
func getBuildCommand() *cobra.Command {
|
||||
build := &cobra.Command{
|
||||
Use: "build",
|
||||
Short: "Build service images.",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
pathProvided := args[0]
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
pathProvided, err := cmd.PersistentFlags().GetString("definition")
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("%+v", err)
|
||||
}
|
||||
def, err := service_definition.GetServiceDefinitionFromFile(pathProvided)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return fmt.Errorf("Failed to read service definition from file: %+v", err)
|
||||
}
|
||||
|
||||
imagesToBuild := def.Build.Images
|
||||
|
@ -27,5 +33,11 @@ var build = &cobra.Command{
|
|||
for _, imageDef := range imagesToBuild {
|
||||
podman.Build(imageDef)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
build.PersistentFlags().StringP("definition", "d", "./service.yml", "Path to the service definition to use.")
|
||||
|
||||
return build
|
||||
|
||||
}
|
||||
|
|
37
cli/build_test.go
Normal file
37
cli/build_test.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
package cli
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCliBuildServiceDefinitionPathMustExist(t *testing.T) {
|
||||
cli := GetCli()
|
||||
|
||||
cli.SetArgs([]string{"build", "-d", "./not-a-file.yml"})
|
||||
|
||||
outcome := cli.Execute()
|
||||
|
||||
if outcome == nil {
|
||||
t.Errorf("Expected error, got nil.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCliBuildDefaultsServiceDefinitionPath(t *testing.T) {
|
||||
cli := GetCli()
|
||||
|
||||
cli.SetArgs([]string{"build"})
|
||||
|
||||
startCommand, _, _ := cli.Find([]string{"build"})
|
||||
|
||||
startCommand.RunE = func(cmd *cobra.Command, args []string) error {
|
||||
actual, _ := cmd.PersistentFlags().GetString("definition")
|
||||
if actual != "./service.yml" {
|
||||
t.Errorf("Unexpected default value for 'definition' / 'd' arg: %s", actual)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
cli.Execute()
|
||||
}
|
18
cli/cli.go
18
cli/cli.go
|
@ -5,15 +5,6 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var allCommands = []*cobra.Command{
|
||||
// cli/start_service.go
|
||||
start,
|
||||
// cli/stop_service.go
|
||||
stop,
|
||||
// cli/build.go
|
||||
build,
|
||||
}
|
||||
|
||||
// Creates the root of the CLI, with all available commands
|
||||
// added to it.
|
||||
func GetCli() *cobra.Command {
|
||||
|
@ -22,6 +13,15 @@ func GetCli() *cobra.Command {
|
|||
Short: "A not-entirely-terrible-way to manage self-hosted services.",
|
||||
}
|
||||
|
||||
allCommands := []*cobra.Command{
|
||||
// cli/start_service.go
|
||||
getStartCommand(),
|
||||
// cli/stop_service.go
|
||||
getStopCommand(),
|
||||
// cli/build.go
|
||||
getBuildCommand(),
|
||||
}
|
||||
|
||||
for _, command := range allCommands {
|
||||
cli.AddCommand(command)
|
||||
}
|
||||
|
|
|
@ -5,23 +5,36 @@
|
|||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"log"
|
||||
service "spud/service"
|
||||
service_definition "spud/service_definition"
|
||||
)
|
||||
|
||||
var start = &cobra.Command{
|
||||
func getStartCommand() *cobra.Command {
|
||||
start := &cobra.Command{
|
||||
Use: "start",
|
||||
Short: "Creates or updates a service based on the provided definition.",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
pathProvided := args[0]
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
pathProvided, err := cmd.PersistentFlags().GetString("definition")
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("%+v", err)
|
||||
}
|
||||
|
||||
def, err := service_definition.GetServiceDefinitionFromFile(pathProvided)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return fmt.Errorf("Failed to read service definition from file: %+v", err)
|
||||
}
|
||||
|
||||
service.CreateService(def)
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
start.PersistentFlags().StringP("definition", "d", "./service.yml", "Path to the service definition to use.")
|
||||
|
||||
return start
|
||||
}
|
||||
|
|
37
cli/start_service_test.go
Normal file
37
cli/start_service_test.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
package cli
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCliStartServiceDefinitionPathMustExist(t *testing.T) {
|
||||
cli := GetCli()
|
||||
|
||||
cli.SetArgs([]string{"start", "-d", "./not-a-file.yml"})
|
||||
|
||||
outcome := cli.Execute()
|
||||
|
||||
if outcome == nil {
|
||||
t.Errorf("Expected error, got nil.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCliStartDefaultsServiceDefinitionPath(t *testing.T) {
|
||||
cli := GetCli()
|
||||
|
||||
cli.SetArgs([]string{"start"})
|
||||
|
||||
startCommand, _, _ := cli.Find([]string{"start"})
|
||||
|
||||
startCommand.RunE = func(cmd *cobra.Command, args []string) error {
|
||||
actual, _ := cmd.PersistentFlags().GetString("definition")
|
||||
if actual != "./service.yml" {
|
||||
t.Errorf("Unexpected default value for 'definition' / 'd' arg: %s", actual)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
cli.Execute()
|
||||
}
|
|
@ -4,15 +4,27 @@
|
|||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
service "spud/service"
|
||||
)
|
||||
|
||||
var stop = &cobra.Command{
|
||||
Use: "stop",
|
||||
func getStopCommand() *cobra.Command {
|
||||
stop := &cobra.Command{
|
||||
Use: "stop [service-name]",
|
||||
Short: "Stops a running service and all of its containers.",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) == 0 {
|
||||
return fmt.Errorf("Must specify a service using '--service'")
|
||||
}
|
||||
|
||||
serviceName := args[0]
|
||||
|
||||
service.StopService(serviceName)
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
return stop
|
||||
}
|
||||
|
|
17
cli/stop_service_test.go
Normal file
17
cli/stop_service_test.go
Normal file
|
@ -0,0 +1,17 @@
|
|||
package cli
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCliStopRequiresAServiceName(t *testing.T) {
|
||||
cli := GetCli()
|
||||
|
||||
cli.SetArgs([]string{"stop"})
|
||||
|
||||
outcome := cli.Execute()
|
||||
|
||||
if outcome == nil {
|
||||
t.Error("Expected error, got nil.")
|
||||
}
|
||||
}
|
5
main.go
5
main.go
|
@ -1,9 +1,12 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
cli "spud/cli"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cli.GetCli().Execute()
|
||||
if err := cli.GetCli().Execute(); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue