From 05228039190c94fff48eaeebe4c8effbfe5aa50d Mon Sep 17 00:00:00 2001 From: Filippo Valsorda Date: Sat, 2 Jan 2021 15:20:59 +0100 Subject: [PATCH] cmd/age,cmd/age-keygen: add -version flag Fixes #157 Fixes #101 Closes #97 --- HomebrewFormula/age.rb | 2 +- cmd/age-keygen/keygen.go | 19 +++++++++++++++++++ cmd/age/age.go | 29 +++++++++++++++++++++++++---- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/HomebrewFormula/age.rb b/HomebrewFormula/age.rb index 74a31e1c..224e32e9 100644 --- a/HomebrewFormula/age.rb +++ b/HomebrewFormula/age.rb @@ -14,7 +14,7 @@ class Age < Formula def install mkdir bin - system "go", "build", "-trimpath", "-o", bin, "filippo.io/age/cmd/..." + system "go", "build", "-trimpath", "-o", bin, "-ldflags", "-X main.Version=v#{version}", "filippo.io/age/cmd/..." prefix.install_metafiles end end diff --git a/cmd/age-keygen/keygen.go b/cmd/age-keygen/keygen.go index d358de60..3eef6afe 100644 --- a/cmd/age-keygen/keygen.go +++ b/cmd/age-keygen/keygen.go @@ -11,20 +11,39 @@ import ( "fmt" "log" "os" + "runtime/debug" "time" "filippo.io/age" "golang.org/x/crypto/ssh/terminal" ) +// Version can be set at link time to override debug.BuildInfo.Main.Version, +// which is "(devel)" when building from within the module. See +// golang.org/issue/29814 and golang.org/issue/29228. +var Version string + func main() { log.SetFlags(0) outFlag := flag.String("o", "", "output to `FILE` (default stdout)") + versionFlag := flag.Bool("version", false, "print the version") flag.Parse() if len(flag.Args()) != 0 { log.Fatalf("age-keygen takes no arguments") } + if *versionFlag { + if Version != "" { + fmt.Println(Version) + return + } + if buildInfo, ok := debug.ReadBuildInfo(); ok { + fmt.Println(buildInfo.Main.Version) + return + } + fmt.Println("(unknown)") + return + } out := os.Stdout if name := *outFlag; name != "" { diff --git a/cmd/age/age.go b/cmd/age/age.go index 8a1211fc..bde76771 100644 --- a/cmd/age/age.go +++ b/cmd/age/age.go @@ -14,6 +14,7 @@ import ( "io" _log "log" "os" + "runtime/debug" "strings" "filippo.io/age" @@ -61,17 +62,24 @@ Example: $ tar cvz ~/data | age -r age1ql3z7hjy54pw3hyww5ayyfg7zqgvc7w3j2elw8zmrj2kg5sfn9aqmcac8p > data.tar.gz.age $ age -d -i key.txt -o data.tar.gz data.tar.gz.age` +// Version can be set at link time to override debug.BuildInfo.Main.Version, +// which is "(devel)" when building from within the module. See +// golang.org/issue/29814 and golang.org/issue/29228. +var Version string + func main() { _log.SetFlags(0) flag.Usage = func() { fmt.Fprintf(os.Stderr, "%s\n", usage) } var ( - outFlag string - decryptFlag, armorFlag, passFlag bool - recipientFlags, identityFlags multiFlag - recipientsFileFlags multiFlag + outFlag string + decryptFlag, armorFlag bool + passFlag, versionFlag bool + recipientFlags, identityFlags multiFlag + recipientsFileFlags multiFlag ) + flag.BoolVar(&versionFlag, "version", false, "print the version") flag.BoolVar(&decryptFlag, "d", false, "decrypt the input") flag.BoolVar(&decryptFlag, "decrypt", false, "decrypt the input") flag.BoolVar(&passFlag, "p", false, "use a passphrase") @@ -88,6 +96,19 @@ func main() { flag.Var(&identityFlags, "identity", "identity (can be repeated)") flag.Parse() + if versionFlag { + if Version != "" { + fmt.Println(Version) + return + } + if buildInfo, ok := debug.ReadBuildInfo(); ok { + fmt.Println(buildInfo.Main.Version) + return + } + fmt.Println("(unknown)") + return + } + if flag.NArg() > 1 { logFatalf("Error: too many arguments.\n" + "age accepts a single optional argument for the input file.")