Skip to content

Commit

Permalink
build(utils): improve util script 'runInPath' and 'outputInPath'
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Jun 18, 2024
1 parent c48e110 commit af1c217
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions magefiles/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down

0 comments on commit af1c217

Please sign in to comment.