From 48be4d4c734be372871ee16782292f724ab1c7dc Mon Sep 17 00:00:00 2001 From: praveenkumar Date: Mon, 14 Mar 2022 16:41:36 +0530 Subject: [PATCH] Mac Preflight: Check if plist file exist before removing it This patch will first check if the plist file exist before error out for check function and also remove it in fix function. This will fix following error for preflight on mac. ``` INFO Checking if old launchd config for tray autostart exists DEBU force trigger cleanup to remove old launchd config for tray force trigger cleanup to remove old launchd config for tray ``` --- pkg/crc/preflight/preflight_darwin.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/pkg/crc/preflight/preflight_darwin.go b/pkg/crc/preflight/preflight_darwin.go index 97166cb58b..f776bc9aeb 100644 --- a/pkg/crc/preflight/preflight_darwin.go +++ b/pkg/crc/preflight/preflight_darwin.go @@ -9,6 +9,7 @@ import ( "github.com/code-ready/crc/pkg/crc/network" "github.com/code-ready/crc/pkg/crc/preset" crcpreset "github.com/code-ready/crc/pkg/crc/preset" + crcos "github.com/code-ready/crc/pkg/os" ) // SetupHost performs the prerequisite checks and setups the host to run the cluster @@ -70,13 +71,23 @@ var trayLaunchdCleanupChecks = []Check{ { configKeySuffix: "check-old-autostart", checkDescription: "Checking if old launchd config for tray autostart exists", - check: func() error { return fmt.Errorf("force trigger cleanup to remove old launchd config for tray") }, - fixDescription: "Removing old launchd config for tray autostart", - fix: func() error { - _ = os.Remove(filepath.Join(constants.GetHomeDir(), "Library", "LaunchAgents", "crc.tray.plist")) - _ = os.Remove(filepath.Join(constants.GetHomeDir(), "Library", "LaunchAgents", "crc.daemon.plist")) + check: func() error { + if crcos.FileExists(filepath.Join(constants.GetHomeDir(), "Library", "LaunchAgents", "crc.tray.plist")) { + return fmt.Errorf("force trigger cleanup to remove old launchd config for tray") + } return nil }, + fixDescription: "Removing old launchd config for tray autostart", + fix: func() error { + var err error + if crcos.FileExists(filepath.Join(constants.GetHomeDir(), "Library", "LaunchAgents", "crc.tray.plist")) { + err = os.Remove(filepath.Join(constants.GetHomeDir(), "Library", "LaunchAgents", "crc.tray.plist")) + } + if crcos.FileExists(filepath.Join(constants.GetHomeDir(), "Library", "LaunchAgents", "crc.daemon.plist")) { + err = os.Remove(filepath.Join(constants.GetHomeDir(), "Library", "LaunchAgents", "crc.daemon.plist")) + } + return err + }, labels: labels{Os: Darwin}, },