From 12764237ff8e5163fe10dfef0ac99cd54d3e27e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Tue, 5 Jan 2021 14:20:24 +0000 Subject: [PATCH] add a -version flag to gofumpt This allows for versioned go get/install commands to bundle the module version in the binary, then the version flag can print it as-is. Prebuilt binaries will also include it via ldflags. Not for gofumports, since its days are counted. For #99. --- gen.go | 22 ++++++++++++++++------ gofmt.go | 10 ++++++++-- version.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 version.go diff --git a/gen.go b/gen.go index a59efa9..ea5244a 100644 --- a/gen.go +++ b/gen.go @@ -248,9 +248,16 @@ const extraSrcLangVersion = `` + }` func copyGofmt(pkg *Package) { - const extraSrc = ` - // This is the only gofumpt change on gofmt's codebase, besides changing - // the name in the usage text. + const extraVersion = ` + // Print the gofumpt version if the user asks for it. + if *showVersion { + printVersion() + return + } + ` + const extraFormat = ` + // Apply gofumpt's changes before we print the code in gofmt's + // format. ` + extraSrcLangVersion + ` gformat.File(fileSet, file, gformat.Options{ LangVersion: *langVersion, @@ -268,8 +275,11 @@ func copyGofmt(pkg *Package) { if i := strings.Index(body, "\t\"mvdan.cc/gofumpt"); i > 0 { body = body[:i] + "\n" + extraImport + "\n" + body[i:] } + if i := strings.Index(body, "if *cpuprofile !="); i > 0 { + body = body[:i] + "\n" + extraVersion + "\n" + body[i:] + } if i := strings.Index(body, "res, err := format("); i > 0 { - body = body[:i] + "\n" + extraSrc + "\n" + body[i:] + body = body[:i] + "\n" + extraFormat + "\n" + body[i:] } } body = strings.Replace(body, "gofmt", "gofumpt", -1) @@ -278,7 +288,7 @@ func copyGofmt(pkg *Package) { } func copyGoimports(pkg *Package) { - const extraSrc = ` + const extraFormat = ` // This is the only gofumpt change on goimports's codebase, besides changing // the name in the usage text. ` + extraSrcLangVersion + ` @@ -299,7 +309,7 @@ func copyGoimports(pkg *Package) { body = body[:i] + "\n" + extraImport + "\n" + body[i:] } if i := strings.Index(body, "if !bytes.Equal"); i > 0 { - body = body[:i] + "\n" + extraSrc + "\n" + body[i:] + body = body[:i] + "\n" + extraFormat + "\n" + body[i:] } } body = strings.Replace(body, "goimports", "gofumports", -1) diff --git a/gofmt.go b/gofmt.go index 8c64cf4..c040e77 100644 --- a/gofmt.go +++ b/gofmt.go @@ -122,8 +122,8 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error simplify(file) } - // This is the only gofumpt change on gofumpt's codebase, besides changing - // the name in the usage text. + // Apply gofumpt's changes before we print the code in gofumpt's + // format. if *langVersion == "" { out, err := exec.Command("go", "list", "-m", "-f", "{{.GoVersion}}").Output() out = bytes.TrimSpace(out) @@ -207,6 +207,12 @@ func gofumptMain() { flag.Usage = usage flag.Parse() + // Print the gofumpt version if the user asks for it. + if *showVersion { + printVersion() + return + } + if *cpuprofile != "" { f, err := os.Create(*cpuprofile) if err != nil { diff --git a/version.go b/version.go new file mode 100644 index 0000000..b70ee78 --- /dev/null +++ b/version.go @@ -0,0 +1,28 @@ +// Copyright (c) 2020, Daniel Martí +// See LICENSE for licensing information + +package main + +import ( + "flag" + "fmt" + "runtime/debug" +) + +var ( + showVersion = flag.Bool("version", false, "show version and exit") + + version = "(devel)" // to match the default from runtime/debug +) + +func printVersion() { + // don't overwrite the version if it was set by -ldflags=-X + if info, ok := debug.ReadBuildInfo(); ok && version == "(devel)" { + mod := &info.Main + if mod.Replace != nil { + mod = mod.Replace + } + version = mod.Version + } + fmt.Println(version) +}