refactor(cli): extract commands into own module
This commit is contained in:
parent
08f068d255
commit
4c3887857f
5 changed files with 96 additions and 38 deletions
28
cli/cli.go
Normal file
28
cli/cli.go
Normal file
|
@ -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
|
||||
}
|
21
cli/cli_test.go
Normal file
21
cli/cli_test.go
Normal file
|
@ -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.")
|
||||
}
|
||||
}
|
27
cli/start_service.go
Normal file
27
cli/start_service.go
Normal file
|
@ -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)
|
||||
},
|
||||
}
|
18
cli/stop_service.go
Normal file
18
cli/stop_service.go
Normal file
|
@ -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)
|
||||
},
|
||||
}
|
40
main.go
40
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()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue