cmd/age,cmd/age-keygen: add -version flag

Fixes #157
Fixes #101
Closes #97
This commit is contained in:
Filippo Valsorda
2021-01-02 15:20:59 +01:00
committed by Filippo Valsorda
parent f8507c1cac
commit 0522803919
3 changed files with 45 additions and 5 deletions

View File

@@ -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

View File

@@ -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 != "" {

View File

@@ -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.")