aboutsummaryrefslogtreecommitdiff
path: root/cmd/podman-mac-helper/uninstall.go
blob: 0c21b27e9a6e711f0c7dd1b9d91d374885d55727 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//go:build darwin
// +build darwin

package main

import (
	"fmt"
	"os"
	"os/exec"
	"path/filepath"

	"github.com/spf13/cobra"
)

var uninstallCmd = &cobra.Command{
	Use:    "uninstall",
	Short:  "uninstalls the podman helper agent",
	Long:   "uninstalls the podman helper agent, which manages the /var/run/docker.sock link",
	PreRun: silentUsage,
	RunE:   uninstall,
}

func init() {
	addPrefixFlag(uninstallCmd)
	rootCmd.AddCommand(uninstallCmd)
}

func uninstall(cmd *cobra.Command, args []string) error {
	userName, _, _, err := getUser()
	if err != nil {
		return err
	}

	labelName := fmt.Sprintf("com.github.containers.podman.helper-%s", userName)
	fileName := filepath.Join("/Library", "LaunchDaemons", labelName+".plist")

	if err = runDetectErr("launchctl", "unload", fileName); err != nil {
		// Try removing the service by label in case the service is half uninstalled
		if rerr := runDetectErr("launchctl", "remove", labelName); rerr != nil {
			// Exit code 3 = no service to remove
			if exitErr, ok := rerr.(*exec.ExitError); !ok || exitErr.ExitCode() != 3 {
				fmt.Fprintf(os.Stderr, "Warning: service unloading failed: %s\n", err.Error())
				fmt.Fprintf(os.Stderr, "Warning: remove also failed: %s\n", rerr.Error())
			}
		}
	}

	if err := os.Remove(fileName); err != nil {
		if !os.IsNotExist(err) {
			return fmt.Errorf("could not remove plist file: %s", fileName)
		}
	}

	helperPath := filepath.Join(installPrefix, "podman", "helper", userName)
	if err := os.RemoveAll(helperPath); err != nil {
		return fmt.Errorf("could not remove helper binary path: %s", helperPath)
	}
	return nil
}