|
| 1 | +package container |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "strings" |
| 7 | + |
| 8 | + containertypes "github.com/docker/docker/api/types/container" |
| 9 | + "github.com/manifoldco/promptui" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + stopall bool |
| 15 | +) |
| 16 | + |
| 17 | +// deleteallcontainerCmd represents the deleteallcontainer command |
| 18 | +var stopAllContainersCmd = &cobra.Command{ |
| 19 | + Use: "stop", |
| 20 | + Short: "Stop a running container", |
| 21 | + Run: func(cmd *cobra.Command, args []string) { |
| 22 | + stopContainers() |
| 23 | + }, |
| 24 | +} |
| 25 | + |
| 26 | +func stopContainers() { |
| 27 | + ctx, cli := dockerClient() |
| 28 | + containerList := runnnigContainerList(cli, ctx) |
| 29 | + |
| 30 | + if len(containerList) == 0 { |
| 31 | + fmt.Println("No container running") |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + // If the flag --all is set, delete all containers |
| 36 | + if stopall { |
| 37 | + |
| 38 | + prompt := promptui.Select{ |
| 39 | + Label: "Are you sure you want to stop all containers?", |
| 40 | + Items: []string{"Yes", "No"}, |
| 41 | + } |
| 42 | + |
| 43 | + _, option, err := prompt.Run() |
| 44 | + |
| 45 | + if err != nil { |
| 46 | + log.Fatal(err) |
| 47 | + } |
| 48 | + |
| 49 | + if option == "Yes" { |
| 50 | + |
| 51 | + for _, container := range containerList { |
| 52 | + fmt.Printf("Stopping container %s (%s)...\n", container.Names[0][1:], container.ID[:6]) |
| 53 | + noWaitTimeout := 5 // to not wait for the container to exit gracefully |
| 54 | + err := cli.ContainerStop(ctx, container.ID, containertypes.StopOptions{Timeout: &noWaitTimeout}) |
| 55 | + if err != nil { |
| 56 | + log.Fatal(err) |
| 57 | + } |
| 58 | + |
| 59 | + } |
| 60 | + fmt.Println("Stopped all containers") |
| 61 | + return |
| 62 | + |
| 63 | + } else { |
| 64 | + fmt.Println("Aborted") |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + // If the flag --all is not set, delete a specific container |
| 71 | + for _, container := range containerList { |
| 72 | + runnnigContainer = append(runnnigContainer, container.Names[0][1:]+" - "+container.ID[:6]) |
| 73 | + } |
| 74 | + |
| 75 | + // Prompt the user to select a container |
| 76 | + fmt.Println("CONTAINER NAME - CONTAINER ID") |
| 77 | + prompt := promptui.Select{ |
| 78 | + Label: "Select a container to stop", |
| 79 | + Items: runnnigContainer, |
| 80 | + } |
| 81 | + |
| 82 | + _, result, err := prompt.Run() |
| 83 | + if err != nil { |
| 84 | + log.Fatal(err) |
| 85 | + } |
| 86 | + |
| 87 | + prompt = promptui.Select{ |
| 88 | + Label: "Are you sure you want to stop " + result + "?", |
| 89 | + Items: []string{"Yes", "No"}, |
| 90 | + } |
| 91 | + |
| 92 | + _, option, err := prompt.Run() |
| 93 | + if err != nil { |
| 94 | + log.Fatal(err) |
| 95 | + } |
| 96 | + |
| 97 | + if option == "Yes" { |
| 98 | + |
| 99 | + split := strings.Split(result, " - ") |
| 100 | + |
| 101 | + noWaitTimeout := 5 // to not wait for the container to exit gracefully |
| 102 | + err := cli.ContainerStop(ctx, split[1], containertypes.StopOptions{Timeout: &noWaitTimeout}) |
| 103 | + if err != nil { |
| 104 | + log.Fatal(err) |
| 105 | + } |
| 106 | + |
| 107 | + fmt.Println("Container + " + split[0] + " stopped successfully") |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | +} |
| 112 | + |
| 113 | +func init() { |
| 114 | + |
| 115 | + ContainerCmd.AddCommand(stopAllContainersCmd) |
| 116 | + |
| 117 | + stopAllContainersCmd.Flags().BoolVarP(&stopall, "all", "a", false, "Delete all containers") |
| 118 | + |
| 119 | +} |
0 commit comments