This repository has been archived by the owner on Mar 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
tooldir.go
104 lines (90 loc) · 2.63 KB
/
tooldir.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"flag"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"syscall"
"github.com/pkg/errors"
)
const (
toolDirName = "_tools"
)
var (
baseDir = flag.String("base-dir", "",
"Path of project root. If not specified and the working directory is within a git repository, the root of "+
"the repository is used. If the working directory is not within a git repository, the working directory "+
"is used.")
toolDir = flag.String("tool-dir", "",
"Path where tools are stored. The default value is the subdirectory of -base-dir named '_tools'.")
// These globals are set by ensureTooldir() after factoring in the flags above.
baseDirPath string
toolDirPath string
)
// If the working directory is within a git repository, return the path of the repository's root; otherwise, return the
// empty string. An error is returned iff invoking 'git' fails for some other reason.
func getRepoRoot() (string, error) {
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
stdout, err := cmd.Output()
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
exitStatus := exitErr.Sys().(syscall.WaitStatus).ExitStatus()
if exitStatus == 128 { // not in a repository
return "", nil
}
}
return "", errors.Wrap(err, "failed to invoke git")
}
repoRoot := strings.TrimSpace(string(stdout))
return repoRoot, nil
}
func ensureTooldir() error {
var err error
baseDirPath = *baseDir
if baseDirPath == "" {
var repoRootPath string
repoRootPath, err = getRepoRoot()
if err != nil {
return errors.Wrap(err, "failed to check for enclosing git repository")
}
if repoRootPath == "" {
baseDirPath, err = os.Getwd()
if err != nil {
return errors.Wrap(err, "failed to get working directory")
}
} else {
baseDirPath = repoRootPath
}
}
toolDirPath = *toolDir
if toolDirPath == "" {
toolDirPath = filepath.Join(baseDirPath, toolDirName)
}
verbosef("base dir: %v\n", baseDirPath)
verbosef("tool dir: %v\n", toolDirPath)
stat, err := os.Stat(toolDirPath)
switch {
case os.IsNotExist(err):
err = os.Mkdir(toolDirPath, 0777)
if err != nil {
return errors.Wrap(err, "unable to create tooldir")
}
case err != nil:
return errors.Wrap(err, "unable to stat tool directory")
case !stat.IsDir():
return errors.New("tool directory already exists, but it is not a directory; you can use -tool-dir to change where tools are saved")
}
err = ioutil.WriteFile(path.Join(toolDirPath, ".gitignore"), gitignore, 0664)
if err != nil {
return errors.Wrap(err, "unable to update .gitignore")
}
return nil
}
var gitignore = []byte(strings.TrimLeft(`
/bin/
/pkg/
/manifest.json
`, "\n"))