From af1c2178675c33e40dfae8c677517699adb973bd Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Tue, 18 Jun 2024 16:30:13 +0200 Subject: [PATCH] build(utils): improve util script 'runInPath' and 'outputInPath' --- magefiles/utils.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/magefiles/utils.go b/magefiles/utils.go index 355934e..f91f61a 100644 --- a/magefiles/utils.go +++ b/magefiles/utils.go @@ -5,35 +5,42 @@ import ( "os" "regexp" "strconv" - "strings" "github.com/magefile/mage/sh" ) // RunInPath runs a command in a specific path. -func runInPath(path string, cmd string, args ...string) error { - err := os.Chdir(path) +func runInPath(path string, cmd string, args ...string) (err error) { + oldPath, err := os.Getwd() + if err != nil { + return err + } + + err = os.Chdir(path) if err != nil { return err } defer func() { - dirs := strings.Split(path, "/") - _ = os.Chdir(strings.Repeat("../", len(dirs))) + err = os.Chdir(oldPath) }() return sh.Run(cmd, args...) } func outputInPath(path string, cmd string, args ...string) (_ string, err error) { + oldPath, err := os.Getwd() + if err != nil { + return "", err + } + err = os.Chdir(path) if err != nil { return "", err } defer func() { - dirs := strings.Split(path, "/") - _ = os.Chdir(strings.Repeat("../", len(dirs))) + err = os.Chdir(oldPath) }() return sh.Output(cmd, args...)