|
| 1 | +// Copyright 2018 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// The genv command generates version-specific go command source files. |
| 6 | +package main |
| 7 | + |
| 8 | +import ( |
| 9 | + "bytes" |
| 10 | + "fmt" |
| 11 | + "html/template" |
| 12 | + "io" |
| 13 | + "os" |
| 14 | + "path/filepath" |
| 15 | + "time" |
| 16 | +) |
| 17 | + |
| 18 | +func usage() { |
| 19 | + fmt.Fprintln(os.Stderr, "usage: genv <version>...") |
| 20 | + os.Exit(2) |
| 21 | +} |
| 22 | + |
| 23 | +func main() { |
| 24 | + if len(os.Args) == 1 { |
| 25 | + usage() |
| 26 | + } |
| 27 | + for _, version := range os.Args[1:] { |
| 28 | + var buf bytes.Buffer |
| 29 | + if err := mainTmpl.Execute(&buf, struct { |
| 30 | + Year int |
| 31 | + Version string |
| 32 | + }{time.Now().Year(), version}); err != nil { |
| 33 | + failf("mainTmpl.execute: %v", err) |
| 34 | + } |
| 35 | + path := filepath.Join(os.Getenv("GOPATH"), "src/golang.org/x/build/version", version, version+".go") |
| 36 | + if err := os.Mkdir(filepath.Dir(path), 0755); err != nil { |
| 37 | + failf("os.Mkdir: %v", err) |
| 38 | + } |
| 39 | + f, err := os.Create(path) |
| 40 | + if err != nil { |
| 41 | + failf("os.Create: %v", err) |
| 42 | + } |
| 43 | + defer f.Close() |
| 44 | + if _, err := io.Copy(f, &buf); err != nil { |
| 45 | + failf("could not write to file: %v", err) |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func failf(format string, args ...interface{}) { |
| 51 | + if len(format) == 0 || format[len(format)-1] != '\n' { |
| 52 | + format += "\n" |
| 53 | + } |
| 54 | + fmt.Fprintf(os.Stderr, format, args...) |
| 55 | + os.Exit(1) |
| 56 | +} |
| 57 | + |
| 58 | +var mainTmpl = template.Must(template.New("main").Parse(`// Copyright {{.Year}} The Go Authors. All rights reserved. |
| 59 | + // Use of this source code is governed by a BSD-style |
| 60 | + // license that can be found in the LICENSE file. |
| 61 | +
|
| 62 | + // The {{.Version}} command runs the go command from {{.Version}}. |
| 63 | + // |
| 64 | + // To install, run: |
| 65 | + // |
| 66 | + // $ go get golang.org/x/build/version/{{.Version}} |
| 67 | + // $ {{.Version}} download |
| 68 | + // |
| 69 | + // And then use the {{.Version}} command as if it were your normal go |
| 70 | + // command. |
| 71 | + // |
| 72 | + // See the release notes at https://golang.org/doc/{{.Version}} |
| 73 | + // |
| 74 | + // File bugs at https://golang.org/issues/new |
| 75 | + package main |
| 76 | +
|
| 77 | + import "golang.org/x/build/version" |
| 78 | +
|
| 79 | + func main() { |
| 80 | + version.Run("{{.Version}}") |
| 81 | + } |
| 82 | +`)) |
0 commit comments