/ cmd / _dbxroot / cmd / pup-stop.go
pup-stop.go
 1  package cmd
 2  
 3  import (
 4  	"fmt"
 5  	"os"
 6  	"os/exec"
 7  
 8  	"github.com/dogeorg/dogeboxd/cmd/_dbxroot/utils"
 9  	"github.com/spf13/cobra"
10  )
11  
12  var stopCmd = &cobra.Command{
13  	Use:   "stop",
14  	Short: "Stop a specific pup",
15  	Long: `Stop a specific pup by providing its ID.
16  This command requires a --pupId flag with an alphanumeric value.
17  
18  Example:
19    pup stop --pupId mypup123`,
20  	Run: func(cmd *cobra.Command, args []string) {
21  		pupId, _ := cmd.Flags().GetString("pupId")
22  		if !utils.IsAlphanumeric(pupId) {
23  			fmt.Println("Error: pupId must contain only alphanumeric characters")
24  			return
25  		}
26  
27  		fmt.Printf("Stopping container with ID: %s\n", pupId)
28  
29  		// We enforce the pup- prefix here to make sure that no bad-actor
30  		// can stop a non-pup container that is running on the system.
31  		machineId := fmt.Sprintf("pup-%s", pupId)
32  
33  		machineCtlCmd := exec.Command("sudo", "machinectl", "stop", machineId)
34  		machineCtlCmd.Stdout = os.Stdout
35  		machineCtlCmd.Stderr = os.Stderr
36  
37  		if err := machineCtlCmd.Run(); err != nil {
38  			fmt.Fprintln(os.Stderr, "Error executing machinectl stop:", err)
39  			os.Exit(1)
40  		}
41  	},
42  }
43  
44  func init() {
45  	pupCmd.AddCommand(stopCmd)
46  
47  	stopCmd.Flags().StringP("pupId", "p", "", "ID of the pup to stop (required, alphanumeric only)")
48  	stopCmd.MarkFlagRequired("pupId")
49  }