feat: add explicit flag / arg definitions
Some checks failed
/ build (push) Has been cancelled

This commit is contained in:
Marc 2024-09-28 21:47:48 -04:00
parent a11bbe2eec
commit 17221855cc
Signed by: marc
GPG key ID: 048E042F22B5DC79
8 changed files with 177 additions and 46 deletions

View file

@ -1,21 +1,27 @@
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 {
build := &cobra.Command{
Use: "build", Use: "build",
Short: "Build service images.", Short: "Build service images.",
Run: func(cmd *cobra.Command, args []string) { RunE: func(cmd *cobra.Command, args []string) error {
pathProvided := args[0] pathProvided, err := cmd.PersistentFlags().GetString("definition")
if err != nil {
return fmt.Errorf("%+v", err)
}
def, err := service_definition.GetServiceDefinitionFromFile(pathProvided) def, err := service_definition.GetServiceDefinitionFromFile(pathProvided)
if err != nil { if err != nil {
log.Fatal(err) return fmt.Errorf("Failed to read service definition from file: %+v", err)
} }
imagesToBuild := def.Build.Images imagesToBuild := def.Build.Images
@ -27,5 +33,11 @@ var build = &cobra.Command{
for _, imageDef := range imagesToBuild { for _, imageDef := range imagesToBuild {
podman.Build(imageDef) 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
View 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()
}

View file

@ -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)
} }

View file

@ -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 {
start := &cobra.Command{
Use: "start", Use: "start",
Short: "Creates or updates a service based on the provided definition.", Short: "Creates or updates a service based on the provided definition.",
Run: func(cmd *cobra.Command, args []string) { RunE: func(cmd *cobra.Command, args []string) error {
pathProvided := args[0] pathProvided, err := cmd.PersistentFlags().GetString("definition")
if err != nil {
return fmt.Errorf("%+v", err)
}
def, err := service_definition.GetServiceDefinitionFromFile(pathProvided) def, err := service_definition.GetServiceDefinitionFromFile(pathProvided)
if err != nil { if err != nil {
log.Fatal(err) return fmt.Errorf("Failed to read service definition from file: %+v", err)
} }
service.CreateService(def) 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
View 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()
}

View file

@ -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{
Use: "stop [service-name]",
Short: "Stops a running service and all of its containers.", 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] serviceName := args[0]
service.StopService(serviceName) service.StopService(serviceName)
return nil
}, },
}
return stop
} }

17
cli/stop_service_test.go Normal file
View 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.")
}
}

View file

@ -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)
}
} }