Skip to content

Commit

Permalink
cmd/gb: info and env returns values for env vars
Browse files Browse the repository at this point in the history
`gb info` optionally accepts a list of environment variable names and
returns their values. `gb env` is aliased to gb info.

Updates constabulary#476
  • Loading branch information
Greg Osuri committed Dec 8, 2015
1 parent b2176dd commit d1fa12d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 42 deletions.
39 changes: 7 additions & 32 deletions cmd/gb/env.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package main

import (
"fmt"

"github.com/constabulary/gb"
"github.com/constabulary/gb/cmd"
)
Expand All @@ -13,37 +11,14 @@ func init() {

var EnvCmd = &cmd.Command{
Name: "env",
UsageLine: `env`,
UsageLine: `env [var ...]`,
Short: "print project environment variables",
Long: `
Env prints project environment variables.
Env prints project environment variables. If one or more variable names is
given as arguments, info prints the value of each named variable on its own line.
`,
Run: env,
}

func env(ctx *gb.Context, args []string) error {
env := makeenv(ctx)
for _, e := range env {
fmt.Printf("%s=%q\n", e.name, e.val)
}
return nil
}

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()},
}
Run: info,
ParseArgs: func(ctx *gb.Context, cwd string, args []string) []string {
return args
},
}
11 changes: 11 additions & 0 deletions cmd/gb/gb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,17 @@ func TestInfoCmd(t *testing.T) {
gb.grepStdout(`^GB_GOROOT="`+regexp.QuoteMeta(runtime.GOROOT())+`"$`, "missing GB_GOROOT")
}

func TestInfoWithArgs(t *testing.T) {
gb := T{T: t}
defer gb.cleanup()

gb.tempDir("src")
gb.cd(gb.tempdir)
gb.run("info", "GB_PROJECT_DIR", "GB_GOROOT")
gb.grepStdout(`^`+regexp.QuoteMeta(gb.tempdir), "missing "+regexp.QuoteMeta(gb.tempdir))
gb.grepStdout(`^`+regexp.QuoteMeta(runtime.GOROOT()), "missing "+regexp.QuoteMeta(runtime.GOROOT()))
}

// Only succeeds if source order is preserved.
func TestSourceFileNameOrderPreserved(t *testing.T) {
gb := T{T: t}
Expand Down
48 changes: 38 additions & 10 deletions cmd/gb/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func init() {
registerCommand(&cmd.Command{
Name: "info",
UsageLine: `info`,
UsageLine: `info [var ...]`,
Short: "info returns information about this project",
Long: `
info prints gb environment information.
Expand All @@ -32,22 +32,27 @@ Values:
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,
Run: info,
ParseArgs: func(ctx *gb.Context, cwd string, args []string) []string {
return args
},
AddFlags: addBuildFlags,
})
}

func info(ctx *gb.Context, args []string) error {
vars := []struct{ name, value string }{
{"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()},
env := makeenv(ctx)
if len(args) > 0 {
for _, arg := range args {
fmt.Println(findenv(env, arg))
}
return nil
}
for _, v := range vars {
fmt.Printf("%s=\"%s\"\n", v.name, v.value)
for _, v := range env {
fmt.Printf("%s=\"%s\"\n", v.name, v.val)
}
return nil
}
Expand All @@ -57,3 +62,26 @@ func info(ctx *gb.Context, args []string) error {
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()},
}
}

0 comments on commit d1fa12d

Please sign in to comment.