This commit is contained in:
parent
a11bbe2eec
commit
17221855cc
8 changed files with 177 additions and 46 deletions
46
cli/build.go
46
cli/build.go
|
@ -1,31 +1,43 @@
|
||||||
package cli
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"log"
|
"log"
|
||||||
podman "spud/podman"
|
podman "spud/podman"
|
||||||
service_definition "spud/service_definition"
|
service_definition "spud/service_definition"
|
||||||
)
|
)
|
||||||
|
|
||||||
var build = &cobra.Command{
|
func getBuildCommand() *cobra.Command {
|
||||||
Use: "build",
|
build := &cobra.Command{
|
||||||
Short: "Build service images.",
|
Use: "build",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Short: "Build service images.",
|
||||||
pathProvided := args[0]
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
def, err := service_definition.GetServiceDefinitionFromFile(pathProvided)
|
pathProvided, err := cmd.PersistentFlags().GetString("definition")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
return fmt.Errorf("%+v", err)
|
||||||
}
|
}
|
||||||
|
def, err := service_definition.GetServiceDefinitionFromFile(pathProvided)
|
||||||
|
|
||||||
imagesToBuild := def.Build.Images
|
if err != nil {
|
||||||
|
return fmt.Errorf("Failed to read service definition from file: %+v", err)
|
||||||
|
}
|
||||||
|
|
||||||
if len(imagesToBuild) == 0 {
|
imagesToBuild := def.Build.Images
|
||||||
log.Print("No images defined - nothing to build!")
|
|
||||||
}
|
if len(imagesToBuild) == 0 {
|
||||||
|
log.Print("No images defined - nothing to build!")
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
for _, imageDef := range imagesToBuild {
|
|
||||||
podman.Build(imageDef)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
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"
|
"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
|
// Creates the root of the CLI, with all available commands
|
||||||
// added to it.
|
// added to it.
|
||||||
func GetCli() *cobra.Command {
|
func GetCli() *cobra.Command {
|
||||||
|
@ -22,6 +13,15 @@ func GetCli() *cobra.Command {
|
||||||
Short: "A not-entirely-terrible-way to manage self-hosted services.",
|
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 {
|
for _, command := range allCommands {
|
||||||
cli.AddCommand(command)
|
cli.AddCommand(command)
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,23 +5,36 @@
|
||||||
package cli
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"log"
|
|
||||||
service "spud/service"
|
service "spud/service"
|
||||||
service_definition "spud/service_definition"
|
service_definition "spud/service_definition"
|
||||||
)
|
)
|
||||||
|
|
||||||
var start = &cobra.Command{
|
func getStartCommand() *cobra.Command {
|
||||||
Use: "start",
|
start := &cobra.Command{
|
||||||
Short: "Creates or updates a service based on the provided definition.",
|
Use: "start",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Short: "Creates or updates a service based on the provided definition.",
|
||||||
pathProvided := args[0]
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
def, err := service_definition.GetServiceDefinitionFromFile(pathProvided)
|
pathProvided, err := cmd.PersistentFlags().GetString("definition")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
return fmt.Errorf("%+v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
service.CreateService(def)
|
def, err := service_definition.GetServiceDefinitionFromFile(pathProvided)
|
||||||
},
|
|
||||||
|
if err != nil {
|
||||||
|
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
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
service "spud/service"
|
service "spud/service"
|
||||||
)
|
)
|
||||||
|
|
||||||
var stop = &cobra.Command{
|
func getStopCommand() *cobra.Command {
|
||||||
Use: "stop",
|
stop := &cobra.Command{
|
||||||
Short: "Stops a running service and all of its containers.",
|
Use: "stop [service-name]",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Short: "Stops a running service and all of its containers.",
|
||||||
serviceName := args[0]
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
service.StopService(serviceName)
|
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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
cli "spud/cli"
|
cli "spud/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cli.GetCli().Execute()
|
if err := cli.GetCli().Execute(); err != nil {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue