Skip to content
This repository has been archived by the owner on May 18, 2024. It is now read-only.

Commit

Permalink
add context object
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Galbreath committed Oct 26, 2015
1 parent 15204b8 commit 6f63e57
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
65 changes: 65 additions & 0 deletions golist/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
12 changes: 11 additions & 1 deletion golist/packages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 6f63e57

Please sign in to comment.