podman
91 строка · 2.7 Кб
1//go:build darwin
2
3package main
4
5import (
6"errors"
7"fmt"
8"io/fs"
9"os"
10"os/exec"
11"path/filepath"
12
13"github.com/containers/storage/pkg/fileutils"
14"github.com/spf13/cobra"
15)
16
17var uninstallCmd = &cobra.Command{
18Use: "uninstall",
19Short: "Uninstall the podman helper agent",
20Long: "Uninstall the podman helper agent, which manages the /var/run/docker.sock link",
21PreRun: silentUsage,
22RunE: uninstall,
23}
24
25func init() {
26addPrefixFlag(uninstallCmd)
27rootCmd.AddCommand(uninstallCmd)
28}
29
30func uninstall(cmd *cobra.Command, args []string) error {
31userName, _, homeDir, err := getUser()
32if err != nil {
33return err
34}
35
36labelName := fmt.Sprintf("com.github.containers.podman.helper-%s", userName)
37fileName := filepath.Join("/Library", "LaunchDaemons", labelName+".plist")
38
39if err = runDetectErr("launchctl", "unload", fileName); err != nil {
40// Try removing the service by label in case the service is half uninstalled
41if rerr := runDetectErr("launchctl", "remove", labelName); rerr != nil {
42// Exit code 3 = no service to remove
43if exitErr, ok := rerr.(*exec.ExitError); !ok || exitErr.ExitCode() != 3 {
44fmt.Fprintf(os.Stderr, "Warning: service unloading failed: %s\n", err.Error())
45fmt.Fprintf(os.Stderr, "Warning: remove also failed: %s\n", rerr.Error())
46}
47}
48}
49
50if err := os.Remove(fileName); err != nil {
51if !os.IsNotExist(err) {
52return fmt.Errorf("could not remove plist file: %s", fileName)
53}
54}
55
56helperPath := filepath.Join(installPrefix, "podman", "helper", userName)
57if err := os.RemoveAll(helperPath); err != nil {
58return fmt.Errorf("could not remove helper binary path: %s", helperPath)
59}
60
61// Get the file information of dockerSock
62if err := fileutils.Lexists(dockerSock); err != nil {
63// If the error is due to the file not existing, return nil
64if errors.Is(err, fs.ErrNotExist) {
65return nil
66}
67// Return an error if unable to get the file information
68return fmt.Errorf("could not stat dockerSock: %v", err)
69}
70if target, err := os.Readlink(dockerSock); err != nil {
71// Return an error if unable to read the symlink
72return fmt.Errorf("could not read dockerSock symlink: %v", err)
73} else {
74// Check if the target of the symlink matches the expected target
75expectedTarget := filepath.Join(homeDir, ".local", "share", "containers", "podman", "machine", "podman.sock")
76if target != expectedTarget {
77// If the targets do not match, print the information and return with nothing left to do
78fmt.Printf("dockerSock does not point to the expected target: %v\n", target)
79return nil
80}
81
82// Attempt to remove dockerSock
83if err := os.Remove(dockerSock); err != nil {
84if !errors.Is(err, fs.ErrNotExist) {
85return fmt.Errorf("could not remove dockerSock file: %s", err)
86}
87}
88}
89
90return nil
91}
92