2024-09-27 00:38:01 +00:00
|
|
|
// Stopping services
|
|
|
|
//
|
|
|
|
// Commands related to stopping services.
|
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2024-10-05 00:28:09 +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"
|
2024-10-05 00:28:09 +00:00
|
|
|
webclient "spud/webclient"
|
2024-09-27 00:38:01 +00:00
|
|
|
)
|
|
|
|
|
2024-09-29 01:47:48 +00:00
|
|
|
func getStopCommand() *cobra.Command {
|
2024-10-05 00:28:09 +00:00
|
|
|
type ParsedFlags struct {
|
|
|
|
serviceName string
|
|
|
|
daemonHost string
|
|
|
|
daemonPort int
|
|
|
|
}
|
2024-09-29 01:47:48 +00:00
|
|
|
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.")
|
|
|
|
}
|
|
|
|
|
2024-09-29 01:47:48 +00:00
|
|
|
if len(args) == 0 {
|
2024-10-05 00:28:09 +00:00
|
|
|
return fmt.Errorf("Must provide a service name.")
|
2024-09-29 01:47:48 +00:00
|
|
|
}
|
|
|
|
|
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-09-29 01:47:48 +00:00
|
|
|
|
2024-10-05 00:28:09 +00:00
|
|
|
return service.NewPodmanServiceManager().Stop(flags.serviceName)
|
2024-09-29 01:47:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
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.")
|
|
|
|
|
2024-09-29 01:47:48 +00:00
|
|
|
return stop
|
2024-09-27 00:38:01 +00:00
|
|
|
}
|