forked from constabulary/gb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.go
92 lines (81 loc) · 2.16 KB
/
info.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
package main
import (
"fmt"
"path/filepath"
"runtime"
"strings"
"github.com/constabulary/gb"
"github.com/constabulary/gb/cmd"
)
func init() {
registerCommand(&cmd.Command{
Name: "info",
UsageLine: `info [var ...]`,
Short: "info returns information about this project",
Long: `
info prints gb environment information.
Values:
GB_PROJECT_DIR
The root of the gb project.
GB_SRC_PATH
The list of gb project source directories.
GB_PKG_DIR
The path of the gb project's package cache.
GB_BIN_SUFFIX
The suffix applied any binary written to $GB_PROJECT_DIR/bin
GB_GOROOT
The value of runtime.GOROOT for the Go version that built this copy of gb.
info returns 0 if the project is well formed, and non zero otherwise.
If one or more variable names is given as arguments, info prints the
value of each named variable on its own line.
`,
Run: info,
ParseArgs: func(ctx *gb.Context, cwd string, args []string) []string {
// env treats arguments as environment variables names,
// don't do any processing.
return args
},
AddFlags: addBuildFlags,
})
}
func info(ctx *gb.Context, args []string) error {
env := makeenv(ctx)
// print values for env variables when args are provided
if len(args) > 0 {
for _, arg := range args {
// print each var on its own line, blank line for each invalid variables
fmt.Println(findenv(env, arg))
}
return nil
}
// print all variable when no args are provided
for _, v := range env {
fmt.Printf("%s=\"%s\"\n", v.name, v.val)
}
return nil
}
// joinlist joins path elements using the os specific separator.
// TODO(dfc) it probably gets this wrong on windows in some circumstances.
func joinlist(paths ...string) string {
return strings.Join(paths, string(filepath.ListSeparator))
}
type envvar struct {
name, val string
}
func findenv(env []envvar, name string) string {
for _, e := range env {
if e.name == name {
return e.val
}
}
return ""
}
func makeenv(ctx *gb.Context) []envvar {
return []envvar{
{"GB_PROJECT_DIR", ctx.Projectdir()},
{"GB_SRC_PATH", joinlist(ctx.Srcdirs()...)},
{"GB_PKG_DIR", ctx.Pkgdir()},
{"GB_BIN_SUFFIX", ctx.Suffix()},
{"GB_GOROOT", runtime.GOROOT()},
}
}