-
Notifications
You must be signed in to change notification settings - Fork 2
/
genv.go
97 lines (90 loc) · 3.41 KB
/
genv.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
// genv creates a workspace with its own go binary and gopath.
// It's meant to keep track of your go code and its dependencies
// including the go binary used.
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"flag"
"path/filepath"
"github.com/nishakm/genv/pkg/script"
"github.com/nishakm/genv/pkg/workspace"
"github.com/nishakm/genv/pkg/versions"
"github.com/nishakm/genv/pkg/project"
)
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [options] folder\n", os.Args[0])
flag.PrintDefaults()
}
verPtr := flag.String("version", "", "Provide a Go version to use. Eg: go1.16.5")
projPtr := flag.String("project", "", "Provide a Git project to clone. Eg: [email protected]/nishakm/genv")
noTools := flag.Bool("no-tools", false, "Set the no-tools flag to avoid installing envtool in your environment")
flag.Parse()
folder := flag.Arg(0)
// create the workspace with the folder
folderpath := workspace.Create(folder)
// get the location of the new gopath
gopath := workspace.Gopath(folderpath)
// get the location of the new environment path
envpath := workspace.Envpath(folderpath)
// write the script
workspace.WriteScript(envpath, script.Generate(gopath, envpath, folder))
fmt.Printf("Workspace created at %s\n", folderpath)
// if a version is provided, make a symlink
if *verPtr != "" {
goroot := versions.GetGoRoot(*verPtr)
if goroot != "" {
workspace.SetGoSym(goroot, envpath)
}
}
// if a git project is provided, clone it in the required location
if *projPtr != "" {
srcpath := workspace.Srcpath(gopath)
projPath := filepath.Join(srcpath, project.GetProjPath(*projPtr))
project.CloneProject(*projPtr, projPath)
}
// if we haven't explicitly said to NOT install envtool, we attempt to install it
if *noTools == false {
// install the envtool binary
if *verPtr != "" {
err := exec.Command(*verPtr, "install", "github.com/nishakm/genv/envtool@latest").Run()
if err != nil {
fmt.Println("Error installing envtool: %s", err)
os.Exit(1)
}
} else {
err := exec.Command("go", "install", "github.com/nishakm/genv/envtool@latest").Run()
if err != nil {
fmt.Println("Error installing envtool: %s", err)
os.Exit(1)
}
}
// find the path the binary is located
result, err := exec.Command("which", "envtool").Output()
if err != nil {
fmt.Println("Error finding envtool: %s", err)
os.Exit(1)
}
envtoolPath := filepath.Join(workspace.Binpath(gopath), "envtool")
// copy binary over to the environment
copyFile(string(result), envtoolPath)
fmt.Println("envtool installed")
}
}
// Go doesn't have a builtin copy function to copy files
// from one place to another. So I'll make my own
func copyFile(srcPath string, destPath string) {
content, err := ioutil.ReadFile(srcPath)
if err != nil {
fmt.Println("Error reading envtool binary: %s", err)
os.Exit(1)
}
writeErr := ioutil.WriteFile(destPath, content, 0755)
if err != nil {
fmt.Println("Error writing envtool to env path: %s", writeErr)
os.Exit(1)
}
}