Skip to content

Commit

Permalink
cmd/age,cmd/age-keygen: add -version flag
Browse files Browse the repository at this point in the history
Fixes #157
Fixes #101
Closes #97
  • Loading branch information
FiloSottile committed Jan 3, 2021
1 parent f8507c1 commit 0522803
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
2 changes: 1 addition & 1 deletion HomebrewFormula/age.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 19 additions & 0 deletions cmd/age-keygen/keygen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down
29 changes: 25 additions & 4 deletions cmd/age/age.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"io"
_log "log"
"os"
"runtime/debug"
"strings"

"filippo.io/age"
Expand Down Expand Up @@ -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")
Expand All @@ -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.")
Expand Down

0 comments on commit 0522803

Please sign in to comment.