Skip to content

Commit

Permalink
chore: Add remove flag to cleanup command
Browse files Browse the repository at this point in the history
By specifying the remove flag (shorthand r) users can alter the behavior of the cleanup command to actually remove the OTel containers, instead of just stopping them

Signed-off-by: Thorsten Hans <[email protected]>
  • Loading branch information
ThorstenHans committed Oct 9, 2024
1 parent ec3d262 commit 5db90f0
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions cmd/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,22 @@ import (
"github.com/spf13/cobra"
)

var cleanUpCmd = &cobra.Command{
Use: "cleanup",
Short: "Clean up OTel dependencies",
RunE: func(cmd *cobra.Command, args []string) error {
if err := cleanUp(); err != nil {
return err
}
return nil
},
var (
removeContainers = false
cleanUpCmd = &cobra.Command{
Use: "cleanup",
Short: "Clean up OTel dependencies",
RunE: func(cmd *cobra.Command, args []string) error {
if err := cleanUp(); err != nil {
return err
}
return nil
},
}
)

func init() {
cleanUpCmd.Flags().BoolVarP(&removeContainers, "remove", "r", false, "If specified, OTel containers will be removed")
}

func getIDs(dockerOutput []byte) []string {
Expand Down Expand Up @@ -53,13 +60,20 @@ func cleanUp() error {
containerIDs := getIDs(dockerPsOutput)

// The `docker stop` command will throw an error if there are no containers to stop
if len(containerIDs) != 0 {
stopContainers := exec.Command("docker", append([]string{"stop"}, containerIDs...)...)
stopContainersOutput, err := stopContainers.CombinedOutput()
if err != nil {
fmt.Println(string(stopContainersOutput))
return err
}
if len(containerIDs) == 0 {
fmt.Println("No Spin OTel resources found. Nothing to clean up.")
return nil
}

cleanupArgs := []string{"stop"}
if removeContainers {
cleanupArgs = []string{"rm", "-f"}
}
stopContainers := exec.Command("docker", append(cleanupArgs, containerIDs...)...)
stopContainersOutput, err := stopContainers.CombinedOutput()
if err != nil {
fmt.Println(string(stopContainersOutput))
return err
}

fmt.Println("All Spin OTel resources have been cleaned up.")
Expand Down

0 comments on commit 5db90f0

Please sign in to comment.