spud/cli/stop_service.go

67 lines
1.7 KiB
Go
Raw Permalink Normal View History

// Stopping services
//
// Commands related to stopping services.
package cli
import (
2024-10-05 00:28:09 +00:00
"context"
"fmt"
"github.com/spf13/cobra"
service "spud/service"
2024-10-05 00:28:09 +00:00
webclient "spud/webclient"
)
func getStopCommand() *cobra.Command {
2024-10-05 00:28:09 +00:00
type ParsedFlags struct {
serviceName string
daemonHost string
daemonPort int
}
stop := &cobra.Command{
Use: "stop [service-name]",
Short: "Stops a running service and all of its containers.",
2024-10-05 00:28:09 +00:00
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
var serviceName, host string
var port int
var err error
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.")
}
if len(args) == 0 {
2024-10-05 00:28:09 +00:00
return fmt.Errorf("Must provide a service name.")
}
2024-10-05 00:28:09 +00:00
serviceName = args[0]
cmd.SetContext(context.WithValue(cmd.Context(), "flags", ParsedFlags{serviceName: serviceName, daemonHost: host, daemonPort: port}))
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
flags := cmd.Context().Value("flags").(ParsedFlags)
if flags.daemonHost != "" && flags.daemonPort != 0 {
webclient.NewWebClient(flags.daemonHost, flags.daemonPort).StopService(flags.serviceName)
return nil
}
2024-10-05 00:28:09 +00:00
return service.NewPodmanServiceManager().Stop(flags.serviceName)
},
}
2024-10-05 00:28:09 +00:00
stop.PersistentFlags().StringP("host", "H", "", "If specified, host where the daemon lives.")
stop.PersistentFlags().IntP("port", "p", 0, "Port on the daemon host.")
return stop
}