github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/cmd/podman-mac-helper/uninstall.go (about)

     1  //go:build darwin
     2  // +build darwin
     3  
     4  package main
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  	"os/exec"
    10  	"path/filepath"
    11  
    12  	"github.com/pkg/errors"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  var uninstallCmd = &cobra.Command{
    17  	Use:    "uninstall",
    18  	Short:  "uninstalls the podman helper agent",
    19  	Long:   "uninstalls the podman helper agent, which manages the /var/run/docker.sock link",
    20  	PreRun: silentUsage,
    21  	RunE:   uninstall,
    22  }
    23  
    24  func init() {
    25  	addPrefixFlag(uninstallCmd)
    26  	rootCmd.AddCommand(uninstallCmd)
    27  }
    28  
    29  func uninstall(cmd *cobra.Command, args []string) error {
    30  	userName, _, _, err := getUser()
    31  	if err != nil {
    32  		return err
    33  	}
    34  
    35  	labelName := fmt.Sprintf("com.github.containers.podman.helper-%s", userName)
    36  	fileName := filepath.Join("/Library", "LaunchDaemons", labelName+".plist")
    37  
    38  	if err = runDetectErr("launchctl", "unload", fileName); err != nil {
    39  		// Try removing the service by label in case the service is half uninstalled
    40  		if rerr := runDetectErr("launchctl", "remove", labelName); rerr != nil {
    41  			// Exit code 3 = no service to remove
    42  			if exitErr, ok := rerr.(*exec.ExitError); !ok || exitErr.ExitCode() != 3 {
    43  				fmt.Fprintf(os.Stderr, "Warning: service unloading failed: %s\n", err.Error())
    44  				fmt.Fprintf(os.Stderr, "Warning: remove also failed: %s\n", rerr.Error())
    45  			}
    46  		}
    47  	}
    48  
    49  	if err := os.Remove(fileName); err != nil {
    50  		if !os.IsNotExist(err) {
    51  			return errors.Errorf("could not remove plist file: %s", fileName)
    52  		}
    53  	}
    54  
    55  	helperPath := filepath.Join(installPrefix, "podman", "helper", userName)
    56  	if err := os.RemoveAll(helperPath); err != nil {
    57  		return errors.Errorf("could not remove helper binary path: %s", helperPath)
    58  	}
    59  	return nil
    60  }