From 48c1aea2201c61355f9468e6c76684f32165ebbd Mon Sep 17 00:00:00 2001 From: wgroeneveld Date: Tue, 14 Mar 2023 20:12:37 +0100 Subject: [PATCH] use a wrapper for cmd "open" to allow for Linux/Win ports --- go.mod | 1 + go.sum | 2 ++ restic/wrapper.go | 22 +++++++++------------- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index 75ec6d9..c8c2b94 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.19 require ( fyne.io/systray v1.10.0 github.com/rs/zerolog v1.29.0 + github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 github.com/stretchr/testify v1.8.2 gopkg.in/natefinch/lumberjack.v2 v2.2.1 ) diff --git a/go.sum b/go.sum index acdac01..314eeda 100644 --- a/go.sum +++ b/go.sum @@ -16,6 +16,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.29.0 h1:Zes4hju04hjbvkVkOhdl2HpZa+0PmVwigmo8XoORE5w= github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0= +github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA= +github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= diff --git a/restic/wrapper.go b/restic/wrapper.go index 8d8ae77..7874c8f 100644 --- a/restic/wrapper.go +++ b/restic/wrapper.go @@ -6,6 +6,8 @@ import ( "errors" "fmt" "github.com/rs/zerolog/log" + "github.com/skratchdot/open-golang/open" + "os" "os/exec" "path/filepath" "time" @@ -58,10 +60,9 @@ func (w *Wrapper) Cleanup() { func resticCmdFn(args ...string) *exec.Cmd { // Running from GoLand: could be /private/var/folders/5s/csgpcjlx1wg9659_485vqz880000gn/T/GoLand/restictray // Installed: could be /Applications/restictray/restictray.app/Contents/MacOS/restictray - // This isn't ideal but I don't want to fiddle with build flags resticExec := filepath.Join(filepath.Dir(executable), "restic") - if IsDev() { - resticExec = "restic" // dev: assume in $PATH + if _, err := os.Stat(resticExec); os.IsNotExist(err) { + resticExec = "restic" // can't find embedded exec, assume in $PATH } cmd := exec.Command(resticExec, args...) @@ -90,23 +91,18 @@ func (w *Wrapper) MountBackups(c *Config) error { return nil } -// OpenConfigFile opens the restic config file using the NON-BLOCKING "open" command. +// OpenConfigFile opens the restic config file using the open command. func OpenConfigFile() error { - return exec.Command("open", ConfigFile()).Run() + return open.Run(ConfigFile()) } -// OpenLogs opens the restic logfile using the NON-BLOCKING "open" command. +// OpenLogs opens the restic logfile using the open command. func OpenLogs() error { - return exec.Command("open", LogFile()).Run() + return open.Run(LogFile()) } func openFolder(folder string) error { - cmd := exec.Command("open", folder) - out, err := cmd.CombinedOutput() - if err != nil { - return fmt.Errorf("open mount dir: %s: %w", string(out), err) - } - return nil + return open.Run(folder) } // UpdateLatestSnapshots updates LatestSnapshots or returns an error. If timed out, returns an error as well.