-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetpath.go
More file actions
98 lines (82 loc) · 2.19 KB
/
Copy pathsetpath.go
File metadata and controls
98 lines (82 loc) · 2.19 KB
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
package main
import (
"flag"
"log"
"os"
"os/exec"
"strings"
"syscall"
)
var (
DIRNAMES = []string{"src", "pkg", "bin"}
noSrc = flag.Bool("no-src", false, "If src folder has to be skipped")
args []string
path string
executableName string
)
// Returns absolute path or relative path of a directory and err if not found
// Searches for an executable binary named file in the directories
// Named by the PATH environment variable
func lookPath(path string) (string, error) {
return exec.LookPath(path)
}
// Get the current working directory (directory where the command was invoked)
func getwd() (string, error) {
return os.Getwd()
}
func setGoPath(path string) {
log.Printf("Setting GOPATH: %s", path)
os.Setenv("GOPATH", path)
}
func createDirIfNotExists(path string) {
for _, dirName := range DIRNAMES[1:] {
if _, errDir := os.Stat(path + dirName); os.IsNotExist(errDir) {
log.Printf("Creating directory %v", dirName)
os.Mkdir(path+dirName, os.ModePerm)
}
}
}
func createGoPathLayout(dir string) {
setGoPath(dir)
createDirIfNotExists(dir + "/")
}
// This will find the path before the "first" occurance of src folder
// Then set the path as GOPATH
// eg. $HOME/workspace/client1/src/myproject/src then,
// GOPATH will be $HOME/workspace/client1/
func goPathLayout(dir string) {
index := -1
if index = strings.Index(dir, DIRNAMES[0]); index == -1 {
if _, errDir := os.Stat(dir + "/" + DIRNAMES[0]); os.IsNotExist(errDir) {
log.Fatal("Please make sure your directory has atleast three directories at it's root - %v", DIRNAMES)
} else {
createGoPathLayout(dir)
}
} else {
createGoPathLayout(dir[:index-1])
}
}
func parseCmdArgs(command []string) (string, []string) {
return command[0], command
}
func main() {
flag.Parse()
if *noSrc == false {
executableName, args = parseCmdArgs(os.Args[1:])
dir, getwdErr := getwd()
if getwdErr != nil {
log.Fatal(getwdErr)
}
goPathLayout(dir)
} else {
executableName, args = parseCmdArgs(flag.Args())
}
binary, lookErr := lookPath(executableName)
if lookErr != nil {
log.Fatal(lookErr)
}
execErr := syscall.Exec(binary, args, os.Environ())
if execErr != nil {
log.Fatal(execErr)
}
}