82 lines
2.3 KiB
Go
82 lines
2.3 KiB
Go
// Start services
|
|
//
|
|
// Commands related to starting services.
|
|
|
|
package cli
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"github.com/spf13/cobra"
|
|
service "spud/service"
|
|
service_definition "spud/service_definition"
|
|
webclient "spud/webclient"
|
|
)
|
|
|
|
func getStartCommand() *cobra.Command {
|
|
type ParsedFlags struct {
|
|
definitionPath string
|
|
daemonHost string
|
|
daemonPort int
|
|
}
|
|
start := &cobra.Command{
|
|
Use: "start",
|
|
Short: "Creates or updates a service based on the provided definition.",
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
|
var pathProvided, host string
|
|
var port int
|
|
var err error
|
|
|
|
if pathProvided, err = cmd.PersistentFlags().GetString("definition"); err != nil {
|
|
return err
|
|
}
|
|
|
|
if host, err = cmd.PersistentFlags().GetString("host"); err != nil {
|
|
return err
|
|
}
|
|
|
|
if port, err = cmd.PersistentFlags().GetInt("port"); err != nil {
|
|
return err
|
|
}
|
|
|
|
if host != "" && port == 0 || host == "" && port != 0 {
|
|
return fmt.Errorf("Invalid flags: host and port must be defined together or not at all.")
|
|
}
|
|
|
|
cmd.SetContext(context.WithValue(cmd.Context(), "flags", ParsedFlags{definitionPath: pathProvided, daemonHost: host, daemonPort: port}))
|
|
return nil
|
|
},
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
var fetcher service_definition.DefinitionFetcher
|
|
var def service_definition.ServiceDefinition
|
|
var err error
|
|
|
|
ctx := cmd.Context()
|
|
flags := ctx.Value("flags").(ParsedFlags)
|
|
|
|
defPath := flags.definitionPath
|
|
defPathType := service_definition.GetPathType(defPath)
|
|
|
|
if fetcher, err = service_definition.NewDefinitionFetcher(defPathType); err != nil {
|
|
return fmt.Errorf("Couldn't set up fetcher from the path provided.")
|
|
}
|
|
|
|
if def, err = fetcher.GetDefinition(defPath); err != nil {
|
|
return fmt.Errorf("Failed to read service definition: %+v", err)
|
|
}
|
|
|
|
if flags.daemonHost != "" && flags.daemonPort != 0 {
|
|
webclient.NewWebClient(flags.daemonHost, flags.daemonPort).CreateService(def)
|
|
return nil
|
|
}
|
|
|
|
return service.NewPodmanServiceManager().Create(def)
|
|
},
|
|
}
|
|
|
|
start.PersistentFlags().StringP("definition", "d", "./service.yml", "Path to the service definition to use.")
|
|
start.PersistentFlags().StringP("host", "H", "", "If specified, host where the daemon lives.")
|
|
start.PersistentFlags().IntP("port", "p", 0, "Port on the daemon host.")
|
|
|
|
return start
|
|
}
|