Skip to content

Commit

Permalink
add a -version flag to gofumpt
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mvdan committed Jan 5, 2021
1 parent 179728c commit 1276423
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
22 changes: 16 additions & 6 deletions gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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 + `
Expand All @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions gofmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
28 changes: 28 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2020, Daniel Martí <mvdan@mvdan.cc>
// 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)
}

0 comments on commit 1276423

Please sign in to comment.