-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add go-match-context cmd for debugging issues with MatchContext
- Loading branch information
1 parent
269c26b
commit 96ce2d5
Showing
2 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/go-match-context | ||
*.exe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"go/build" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/charlievieth/buildutil" | ||
) | ||
|
||
func init() { | ||
log.SetFlags(log.Lshortfile) | ||
} | ||
|
||
type Context struct { | ||
GOARCH string | ||
GOOS string | ||
GOROOT string | ||
GOPATH string | ||
Dir string | ||
CgoEnabled bool | ||
UseAllFiles bool | ||
Compiler string | ||
BuildTags []string | ||
ToolTags []string | ||
ReleaseTags []string | ||
InstallSuffix string | ||
} | ||
|
||
func main() { | ||
flag.Usage = func() { | ||
const usage = "Usage: %s [OPTION] FILE\n" + | ||
"MatchContext for FILE and print the new build.Context\n" | ||
fmt.Fprintf(os.Stdout, usage, filepath.Base(os.Args[0])) | ||
flag.PrintDefaults() | ||
} | ||
printJSON := flag.Bool("json", false, "Print output as JSON") | ||
flag.Parse() | ||
if flag.NArg() != 1 { | ||
log.Panicln("error: expect one FILE argument") | ||
flag.Usage() | ||
os.Exit(1) | ||
} | ||
filename := flag.Arg(0) | ||
|
||
ctxt, err := buildutil.MatchContext(&build.Default, filename, nil) | ||
if err != nil { | ||
log.Fatal("error:", err) | ||
} | ||
|
||
if *printJSON { | ||
c := Context{ | ||
GOARCH: ctxt.GOARCH, | ||
GOOS: ctxt.GOOS, | ||
GOROOT: ctxt.GOROOT, | ||
GOPATH: ctxt.GOPATH, | ||
Dir: ctxt.Dir, | ||
CgoEnabled: ctxt.CgoEnabled, | ||
UseAllFiles: ctxt.UseAllFiles, | ||
Compiler: ctxt.Compiler, | ||
BuildTags: ctxt.BuildTags, | ||
ToolTags: ctxt.ToolTags, | ||
ReleaseTags: ctxt.ReleaseTags, | ||
InstallSuffix: ctxt.InstallSuffix, | ||
} | ||
data, err := json.MarshalIndent(&c, "", " ") | ||
if err != nil { | ||
log.Fatal("error:", err) | ||
} | ||
if _, err := os.Stdout.Write(data); err != nil { | ||
log.Fatal("error:", err) | ||
} | ||
} else { | ||
fmt.Printf("GOARCH=%q\n", ctxt.GOARCH) | ||
fmt.Printf("GOOS=%q\n", ctxt.GOOS) | ||
fmt.Printf("GOROOT=%q\n", ctxt.GOROOT) | ||
fmt.Printf("GOPATH=%q\n", ctxt.GOPATH) | ||
fmt.Printf("Dir=%q\n", ctxt.Dir) | ||
fmt.Printf("CgoEnabled=%t\n", ctxt.CgoEnabled) | ||
fmt.Printf("UseAllFiles=%t\n", ctxt.UseAllFiles) | ||
fmt.Printf("Compiler=%q\n", ctxt.Compiler) | ||
fmt.Printf("BuildTags=%q\n", ctxt.BuildTags) | ||
fmt.Printf("ToolTags=%q\n", ctxt.ToolTags) | ||
fmt.Printf("ReleaseTags=%q\n", ctxt.ReleaseTags) | ||
fmt.Printf("InstallSuffix=%q\n", ctxt.InstallSuffix) | ||
} | ||
} |