66 lines
1.7 KiB
Go
66 lines
1.7 KiB
Go
// Stopping services
|
|
//
|
|
// Commands related to stopping services.
|
|
package cli
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"github.com/spf13/cobra"
|
|
service "spud/service"
|
|
webclient "spud/webclient"
|
|
)
|
|
|
|
func getStopCommand() *cobra.Command {
|
|
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.",
|
|
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 {
|
|
return fmt.Errorf("Must provide a service name.")
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
return service.NewPodmanServiceManager().Stop(flags.serviceName)
|
|
},
|
|
}
|
|
|
|
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
|
|
}
|