From 6f63e574a9016281a12c1318c6726af43e45d3df Mon Sep 17 00:00:00 2001 From: Nick Galbreath Date: Mon, 26 Oct 2015 15:01:06 -0700 Subject: [PATCH] add context object --- golist/packages.go | 65 +++++++++++++++++++++++++++++++++++++++++ golist/packages_test.go | 12 +++++++- 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/golist/packages.go b/golist/packages.go index 10a0aca..7890603 100644 --- a/golist/packages.go +++ b/golist/packages.go @@ -3,14 +3,31 @@ package golist import ( "bufio" "encoding/json" + "fmt" "io" // "fmt" "log" "os" "os/exec" "sort" + "strings" ) +// Context is similar to +// // https://github.com/golang/go/blob/master/src/cmd/go/context.go +type Context struct { + GOARCH string // target architecture + GOOS string // target operating system + GOROOT string // Go root + GOPATH string // Go path + CgoEnabled bool // whether cgo can be used + UseAllFiles bool // use files regardless of +build lines, file names + Compiler string // compiler to assume when computing target paths + BuildTags []string // build constraints to match in +build lines + ReleaseTags []string // releases the current release is compatible with + InstallSuffix string // suffix to use in the name of the install dir +} + // A PackageError describes an error loading information about a package. type PackageError struct { ImportStack []string // shortest path from package named on command line to this one @@ -177,3 +194,51 @@ func Deps(name ...string) ([]string, error) { sort.Strings(paths) return paths, nil } + +/* +fm := template.FuncMap{ + "join": strings.Join, + "context": context, +} +*/ + +const contextTemplate = `{{ with context }}{{ .GOARCH }} +{{ .GOOS }} +{{ .GOROOT }} +{{ .GOPATH }} +{{ .CgoEnabled }} +{{ .UseAllFiles }} +{{ .Compiler }} +{{ join .BuildTags "," }} +{{ join .ReleaseTags "," }} +{{ .InstallSuffix }}{{ end }}` + +// NewContext generates a context object +func NewContext() (*Context, error) { + c := Context{} + cmd := exec.Command("go", "list", "-f", contextTemplate) + outbytes, err := cmd.Output() + if err != nil { + return nil, err + } + lines := strings.Split(string(outbytes), "\n") + if len(lines) != 10 { + return nil, fmt.Errorf("expected 10 outlines from golist, got %d with %q", len(lines), string(outbytes)) + } + c.GOARCH = lines[0] + c.GOOS = lines[1] + c.GOROOT = lines[2] + c.GOPATH = lines[3] + if lines[4] == "true" { + c.CgoEnabled = true + } + if lines[5] == "true" { + c.UseAllFiles = true + } + c.Compiler = lines[6] + c.BuildTags = strings.Split(lines[7], ",") + c.ReleaseTags = strings.Split(lines[8], ",") + c.InstallSuffix = lines[9] + + return &c, nil +} diff --git a/golist/packages_test.go b/golist/packages_test.go index 55d902b..b0fc594 100644 --- a/golist/packages_test.go +++ b/golist/packages_test.go @@ -8,7 +8,7 @@ import ( // TestGoListStd tests GoListStd. This is really over kill // for the current implimentation, but previous one was much weirder func TestGoListStd(t *testing.T) { - pkgs, err := GoListStd() + pkgs, err := Std() if err != nil { t.Fatalf("Unable to get standard packages: %s", err) } @@ -43,3 +43,13 @@ func TestGoListStd(t *testing.T) { } } } + +func TestNewContext(t *testing.T) { + c, err := NewContext() + if err != nil { + t.Fatalf("Unable to get context: %s", err) + } + if c.GOARCH != "amd64" { + t.Errorf("GOARCH not set correctly: %q", c.GOARCH) + } +}