2024-09-27 00:38:01 +00:00
|
|
|
// Start services
|
|
|
|
//
|
|
|
|
// Commands related to starting services.
|
|
|
|
|
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2024-10-04 04:03:12 +00:00
|
|
|
"context"
|
2024-09-29 01:47:48 +00:00
|
|
|
"fmt"
|
2024-09-27 00:38:01 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
service "spud/service"
|
|
|
|
service_definition "spud/service_definition"
|
2024-10-04 04:03:12 +00:00
|
|
|
webclient "spud/webclient"
|
2024-09-27 00:38:01 +00:00
|
|
|
)
|
|
|
|
|
2024-09-29 01:47:48 +00:00
|
|
|
func getStartCommand() *cobra.Command {
|
2024-10-04 04:03:12 +00:00
|
|
|
type ParsedFlags struct {
|
|
|
|
definitionPath string
|
|
|
|
daemonHost string
|
|
|
|
daemonPort int
|
|
|
|
}
|
2024-09-29 01:47:48 +00:00
|
|
|
start := &cobra.Command{
|
|
|
|
Use: "start",
|
|
|
|
Short: "Creates or updates a service based on the provided definition.",
|
2024-10-04 04:03:12 +00:00
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
var pathProvided, host string
|
|
|
|
var port int
|
|
|
|
var err error
|
2024-09-27 00:38:01 +00:00
|
|
|
|
2024-10-04 04:03:12 +00:00
|
|
|
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
|
2024-09-29 01:47:48 +00:00
|
|
|
}
|
2024-09-27 00:38:01 +00:00
|
|
|
|
2024-10-04 04:03:12 +00:00
|
|
|
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 {
|
|
|
|
ctx := cmd.Context()
|
|
|
|
flags := ctx.Value("flags").(ParsedFlags)
|
|
|
|
def, err := service_definition.GetServiceDefinitionFromFile(flags.definitionPath)
|
2024-09-29 01:47:48 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to read service definition from file: %+v", err)
|
|
|
|
}
|
|
|
|
|
2024-10-04 04:03:12 +00:00
|
|
|
if flags.daemonHost != "" && flags.daemonPort != 0 {
|
|
|
|
webclient.NewWebClient(flags.daemonHost, flags.daemonPort).CreateService(def)
|
|
|
|
return nil
|
|
|
|
}
|
2024-09-29 01:47:48 +00:00
|
|
|
|
2024-10-05 00:05:33 +00:00
|
|
|
return service.NewPodmanServiceManager().Create(def)
|
2024-09-29 01:47:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
start.PersistentFlags().StringP("definition", "d", "./service.yml", "Path to the service definition to use.")
|
2024-10-04 04:03:12 +00:00
|
|
|
start.PersistentFlags().StringP("host", "H", "", "If specified, host where the daemon lives.")
|
|
|
|
start.PersistentFlags().IntP("port", "p", 0, "Port on the daemon host.")
|
2024-09-29 01:47:48 +00:00
|
|
|
|
|
|
|
return start
|
2024-09-27 00:38:01 +00:00
|
|
|
}
|