diff --git a/cli/cli.go b/cli/cli.go new file mode 100644 index 0000000..b92545a --- /dev/null +++ b/cli/cli.go @@ -0,0 +1,28 @@ +// Root of the CLI. +package cli + +import ( + "github.com/spf13/cobra" +) + +var allCommands = []*cobra.Command{ + // cli/start_service.go + start, + // cli/stop_service.go + stop, +} + +// Creates the root of the CLI, with all available commands +// added to it. +func GetCli() *cobra.Command { + var cli = &cobra.Command{ + Use: "spud", + Short: "A not-entirely-terrible-way to manage self-hosted services.", + } + + for _, command := range allCommands { + cli.AddCommand(command) + } + + return cli +} diff --git a/cli/cli_test.go b/cli/cli_test.go new file mode 100644 index 0000000..c0d2a82 --- /dev/null +++ b/cli/cli_test.go @@ -0,0 +1,21 @@ +package cli + +import ( + "testing" +) + +func TestGetCliReturnsNonNil(t *testing.T) { + root := GetCli() + + if root == nil { + t.Error("Expected to get command pointer, got nil.") + } +} + +func TestGetCliReturnsCliRoot(t *testing.T) { + root := GetCli() + + if len(root.Commands()) == 0 { + t.Error("Expected to find >= 0 commands mapped, found none.") + } +} diff --git a/cli/start_service.go b/cli/start_service.go new file mode 100644 index 0000000..42619cf --- /dev/null +++ b/cli/start_service.go @@ -0,0 +1,27 @@ +// Start services +// +// Commands related to starting services. + +package cli + +import ( + "github.com/spf13/cobra" + "log" + service "spud/service" + service_definition "spud/service_definition" +) + +var 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] + def, err := service_definition.GetServiceDefinitionFromFile(pathProvided) + + if err != nil { + log.Fatal(err) + } + + service.CreateService(def) + }, +} diff --git a/cli/stop_service.go b/cli/stop_service.go new file mode 100644 index 0000000..5686553 --- /dev/null +++ b/cli/stop_service.go @@ -0,0 +1,18 @@ +// Stopping services +// +// Commands related to stopping services. +package cli + +import ( + "github.com/spf13/cobra" + service "spud/service" +) + +var stop = &cobra.Command{ + Use: "stop", + Short: "Stops a running service and all of its containers.", + Run: func(cmd *cobra.Command, args []string) { + serviceName := args[0] + service.StopService(serviceName) + }, +} diff --git a/main.go b/main.go index bd63bd8..c0cf245 100644 --- a/main.go +++ b/main.go @@ -1,45 +1,9 @@ package main import ( - "github.com/spf13/cobra" - "log" - - service "spud/service" - service_definition "spud/service_definition" + cli "spud/cli" ) -var cli = &cobra.Command{ - Use: "spud", - Short: "A not-entirely-terrible-way to manage self-hosted services.", -} - -var 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] - def, err := service_definition.GetServiceDefinitionFromFile(pathProvided) - - if err != nil { - log.Fatal(err) - } - - service.CreateService(def) - }, -} - -var stop = &cobra.Command{ - Use: "stop", - Short: "Stops a running service and all of its containers.", - Run: func(cmd *cobra.Command, args []string) { - serviceName := args[0] - service.StopService(serviceName) - }, -} - func main() { - cli.AddCommand(start) - cli.AddCommand(stop) - - cli.Execute() + cli.GetCli().Execute() }