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 def install
mkdir bin 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 prefix.install_metafiles
end end
end end

View File

@@ -11,20 +11,39 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"runtime/debug"
"time" "time"
"filippo.io/age" "filippo.io/age"
"golang.org/x/crypto/ssh/terminal" "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() { func main() {
log.SetFlags(0) log.SetFlags(0)
outFlag := flag.String("o", "", "output to `FILE` (default stdout)") outFlag := flag.String("o", "", "output to `FILE` (default stdout)")
versionFlag := flag.Bool("version", false, "print the version")
flag.Parse() flag.Parse()
if len(flag.Args()) != 0 { if len(flag.Args()) != 0 {
log.Fatalf("age-keygen takes no arguments") 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 out := os.Stdout
if name := *outFlag; name != "" { if name := *outFlag; name != "" {

View File

@@ -14,6 +14,7 @@ import (
"io" "io"
_log "log" _log "log"
"os" "os"
"runtime/debug"
"strings" "strings"
"filippo.io/age" "filippo.io/age"
@@ -61,17 +62,24 @@ Example:
$ tar cvz ~/data | age -r age1ql3z7hjy54pw3hyww5ayyfg7zqgvc7w3j2elw8zmrj2kg5sfn9aqmcac8p > data.tar.gz.age $ tar cvz ~/data | age -r age1ql3z7hjy54pw3hyww5ayyfg7zqgvc7w3j2elw8zmrj2kg5sfn9aqmcac8p > data.tar.gz.age
$ age -d -i key.txt -o data.tar.gz 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() { func main() {
_log.SetFlags(0) _log.SetFlags(0)
flag.Usage = func() { fmt.Fprintf(os.Stderr, "%s\n", usage) } flag.Usage = func() { fmt.Fprintf(os.Stderr, "%s\n", usage) }
var ( var (
outFlag string outFlag string
decryptFlag, armorFlag, passFlag bool decryptFlag, armorFlag bool
recipientFlags, identityFlags multiFlag passFlag, versionFlag bool
recipientsFileFlags multiFlag recipientFlags, identityFlags multiFlag
recipientsFileFlags multiFlag
) )
flag.BoolVar(&versionFlag, "version", false, "print the version")
flag.BoolVar(&decryptFlag, "d", false, "decrypt the input") flag.BoolVar(&decryptFlag, "d", false, "decrypt the input")
flag.BoolVar(&decryptFlag, "decrypt", false, "decrypt the input") flag.BoolVar(&decryptFlag, "decrypt", false, "decrypt the input")
flag.BoolVar(&passFlag, "p", false, "use a passphrase") flag.BoolVar(&passFlag, "p", false, "use a passphrase")
@@ -88,6 +96,19 @@ func main() {
flag.Var(&identityFlags, "identity", "identity (can be repeated)") flag.Var(&identityFlags, "identity", "identity (can be repeated)")
flag.Parse() 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 { if flag.NArg() > 1 {
logFatalf("Error: too many arguments.\n" + logFatalf("Error: too many arguments.\n" +
"age accepts a single optional argument for the input file.") "age accepts a single optional argument for the input file.")