-
Notifications
You must be signed in to change notification settings - Fork 1
/
magelib.go
45 lines (37 loc) · 898 Bytes
/
magelib.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package magelib
import (
"os"
"path/filepath"
"github.com/magefile/mage/sh"
"github.com/pkg/errors"
)
var (
MkTempDirOut = sh.OutCmd("mktemp", "-d")
GoInstall = sh.RunCmd("go", "install")
GoUpdate = sh.RunCmd("go", "get", "-u")
GoGet = sh.RunCmd("go", "get")
GoEnvOut = sh.OutCmd("go", "env")
GoMod = sh.RunCmd("go", "mod")
GoModVendor = sh.RunCmd("go", "mod", "vendor")
GoModTidy = sh.RunCmd("go", "mod", "tidy")
)
func InDirectory(path string, cmd Cmd) (err error) {
path = os.ExpandEnv(path)
if !filepath.IsAbs(path) {
path, err = filepath.Abs(path)
if err != nil {
return errors.Wrap(err, "Abs")
}
}
oldPath, err := os.Getwd()
if err != nil {
return errors.Wrap(err, "Getwd")
}
if err := os.Chdir(path); err != nil {
return errors.Wrap(err, "Chdir")
}
defer func(p string) {
err = os.Chdir(p)
}(oldPath)
return cmd()
}