From b8d11be4b66d6c3f6abc6fd54fba404aa3de8476 Mon Sep 17 00:00:00 2001 From: Charlie Chiang Date: Wed, 5 Apr 2023 20:32:13 +0800 Subject: [PATCH] feat: fail if launch daemon already exists when installing --- install.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/install.go b/install.go index 7fd8485..185c095 100644 --- a/install.go +++ b/install.go @@ -44,6 +44,13 @@ func installDaemon() error { return fmt.Errorf("failed to create /Library/LaunchDaemons: %w", err) } + // warn if the file already exists + _, err = os.Stat("/Library/LaunchDaemons/cc.chlc.batt.plist") + if err == nil { + logrus.Errorf("/Library/LaunchDaemons/cc.chlc.batt.plist already exists") + return fmt.Errorf("/Library/LaunchDaemons/cc.chlc.batt.plist already exists. To avoid overwriting it, please remove it manually and try again") + } + err = os.WriteFile("/Library/LaunchDaemons/cc.chlc.batt.plist", []byte(tmpl), 0644) if err != nil { return fmt.Errorf("failed to write /Library/LaunchDaemons/cc.chlc.batt.plist: %w", err)