aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/buildid
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2017-10-06 14:03:55 -0400
committerRuss Cox <rsc@golang.org>2017-10-11 18:16:02 +0000
commit9ad2319bbca45b0750366e99b79db8889f0dfc5b (patch)
tree2e8b606a92b962dce1d397cf9eb5600b24965eb0 /src/cmd/buildid
parent0bede7f34e20a77052b433a3c8ee402aa731183e (diff)
downloadgo-9ad2319bbca45b0750366e99b79db8889f0dfc5b.tar.gz
go-9ad2319bbca45b0750366e99b79db8889f0dfc5b.zip
cmd/buildid: add new tool factoring out code needed by go command
This CL does a few things. 1. It moves the existing "read a build ID" code out of the go command and into cmd/internal/buildid. 2. It adds new code there to "write a build ID". 3. It adds better tests. 4. It encapsulates cmd/internal/buildid into a new standalone program "go tool buildid". The go command is going to use the new "write a build ID" functionality in a future CL. Adding the separate "go tool buildid" gives "go build -x" a printable command to explain what it is doing in that new step. (This is similar to the go command printing "go tool pack" commands equivalent to the actions it is taking, even though it's not invoking pack directly.) Keeping go build -x honest means that other build systems can potentially keep up with the go command. Change-Id: I01c0a66e30a80fa7254e3f2879283d3cd7aa03b4 Reviewed-on: https://go-review.googlesource.com/69053 Reviewed-by: David Crawshaw <crawshaw@golang.org>
Diffstat (limited to 'src/cmd/buildid')
-rw-r--r--src/cmd/buildid/buildid.go73
-rw-r--r--src/cmd/buildid/doc.go18
2 files changed, 91 insertions, 0 deletions
diff --git a/src/cmd/buildid/buildid.go b/src/cmd/buildid/buildid.go
new file mode 100644
index 0000000000..8d810ffdd9
--- /dev/null
+++ b/src/cmd/buildid/buildid.go
@@ -0,0 +1,73 @@
+// Copyright 2017 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import (
+ "flag"
+ "fmt"
+ "log"
+ "os"
+ "strings"
+
+ "cmd/internal/buildid"
+)
+
+func usage() {
+ fmt.Fprintf(os.Stderr, "usage: go tool buildid [-w] file\n")
+ flag.PrintDefaults()
+ os.Exit(2)
+}
+
+var wflag = flag.Bool("w", false, "write build ID")
+
+func main() {
+ log.SetPrefix("buildid: ")
+ log.SetFlags(0)
+ flag.Usage = usage
+ flag.Parse()
+ if flag.NArg() != 1 {
+ usage()
+ }
+
+ file := flag.Arg(0)
+ id, err := buildid.ReadFile(file)
+ if err != nil {
+ log.Fatal(err)
+ }
+ if !*wflag {
+ fmt.Printf("%s\n", id)
+ return
+ }
+
+ f, err := os.Open(file)
+ if err != nil {
+ log.Fatal(err)
+ }
+ matches, hash, err := buildid.FindAndHash(f, id, 0)
+ if err != nil {
+ log.Fatal(err)
+ }
+ f.Close()
+
+ tail := id
+ if i := strings.LastIndex(id, "."); i >= 0 {
+ tail = tail[i+1:]
+ }
+ if len(tail) != len(hash)*2 {
+ log.Fatalf("%s: cannot find %d-byte hash in id %s", file, len(hash), id)
+ }
+ newID := id[:len(id)-len(tail)] + fmt.Sprintf("%x", hash)
+
+ f, err = os.OpenFile(file, os.O_WRONLY, 0)
+ if err != nil {
+ log.Fatal(err)
+ }
+ if err := buildid.Rewrite(f, matches, newID); err != nil {
+ log.Fatal(err)
+ }
+ if err := f.Close(); err != nil {
+ log.Fatal(err)
+ }
+}
diff --git a/src/cmd/buildid/doc.go b/src/cmd/buildid/doc.go
new file mode 100644
index 0000000000..d1ec155c97
--- /dev/null
+++ b/src/cmd/buildid/doc.go
@@ -0,0 +1,18 @@
+// Copyright 2017 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+/*
+Buildid displays or updates the build ID stored in a Go package or binary.
+
+Usage:
+ go tool buildid [-w] file
+
+By default, buildid prints the build ID found in the named file.
+If the -w option is given, buildid rewrites the build ID found in
+the file to accurately record a content hash of the file.
+
+This tool is only intended for use by the go command or
+other build systems.
+*/
+package main