diff --git a/cmd/age-inspect/inspect.go b/cmd/age-inspect/inspect.go index 976a78d..f599453 100644 --- a/cmd/age-inspect/inspect.go +++ b/cmd/age-inspect/inspect.go @@ -24,6 +24,10 @@ Options: INPUT defaults to standard input. "-" may be used as INPUT to explicitly read from standard input.` +// Version can be set at link time to override debug.BuildInfo.Main.Version when +// building manually without git history. It should look like "v1.2.3". +var Version string + func main() { flag.Usage = func() { fmt.Fprintf(os.Stderr, "%s\n", usage) } @@ -37,11 +41,10 @@ func main() { flag.Parse() if versionFlag { - if buildInfo, ok := debug.ReadBuildInfo(); ok { - fmt.Println(buildInfo.Main.Version) - return + if buildInfo, ok := debug.ReadBuildInfo(); ok && Version == "" { + Version = buildInfo.Main.Version } - fmt.Println("(unknown)") + fmt.Println(Version) return } diff --git a/cmd/age-keygen/keygen.go b/cmd/age-keygen/keygen.go index d2c0685..a192bcf 100644 --- a/cmd/age-keygen/keygen.go +++ b/cmd/age-keygen/keygen.go @@ -56,6 +56,10 @@ Examples: $ age-keygen -y key.txt age1ql3z7hjy54pw3hyww5ayyfg7zqgvc7w3j2elw8zmrj2kg5sfn9aqmcac8p` +// Version can be set at link time to override debug.BuildInfo.Main.Version when +// building manually without git history. It should look like "v1.2.3". +var Version string + func main() { log.SetFlags(0) flag.Usage = func() { fmt.Fprintf(os.Stderr, "%s\n", usage) } @@ -69,6 +73,15 @@ func main() { flag.StringVar(&outFlag, "o", "", "output to `FILE` (default stdout)") flag.StringVar(&outFlag, "output", "", "output to `FILE` (default stdout)") flag.Parse() + + if versionFlag { + if buildInfo, ok := debug.ReadBuildInfo(); ok && Version == "" { + Version = buildInfo.Main.Version + } + fmt.Println(Version) + return + } + if len(flag.Args()) != 0 && !convertFlag { errorf("too many arguments") } @@ -78,14 +91,6 @@ func main() { if pqFlag && convertFlag { errorf("-pq cannot be used with -y") } - if versionFlag { - if buildInfo, ok := debug.ReadBuildInfo(); ok { - fmt.Println(buildInfo.Main.Version) - return - } - fmt.Println("(unknown)") - return - } out := os.Stdout if outFlag != "" { diff --git a/cmd/age-plugin-batchpass/plugin-batchpass.go b/cmd/age-plugin-batchpass/plugin-batchpass.go index e325994..66e2675 100644 --- a/cmd/age-plugin-batchpass/plugin-batchpass.go +++ b/cmd/age-plugin-batchpass/plugin-batchpass.go @@ -7,6 +7,7 @@ import ( "io" "log" "os" + "runtime/debug" "strconv" "strings" @@ -73,6 +74,10 @@ When decrypting, you can set AGE_PASSPHRASE_MAX_WORK_FACTOR to limit the maximum scrypt work factor accepted (between 1 and 30, default 30). This can be used to avoid very slow decryptions.` +// Version can be set at link time to override debug.BuildInfo.Main.Version when +// building manually without git history. It should look like "v1.2.3". +var Version string + func main() { flag.Usage = func() { fmt.Fprintf(os.Stderr, "%s\n", usage) } @@ -80,6 +85,19 @@ func main() { if err != nil { log.Fatal(err) } + p.RegisterFlags(nil) + + versionFlag := flag.Bool("version", false, "print the version") + flag.Parse() + + if *versionFlag { + if buildInfo, ok := debug.ReadBuildInfo(); ok && Version == "" { + Version = buildInfo.Main.Version + } + fmt.Println(Version) + return + } + p.HandleIdentityAsRecipient(func(data []byte) (age.Recipient, error) { if len(data) != 0 { return nil, fmt.Errorf("batchpass identity does not take any payload") diff --git a/cmd/age/age.go b/cmd/age/age.go index 48d3a67..190fa45 100644 --- a/cmd/age/age.go +++ b/cmd/age/age.go @@ -98,6 +98,10 @@ func (f *identityFlags) addPluginFlag(value string) error { return nil } +// Version can be set at link time to override debug.BuildInfo.Main.Version when +// building manually without git history. It should look like "v1.2.3". +var Version string + func main() { flag.Usage = func() { fmt.Fprintf(os.Stderr, "%s\n", usage) } @@ -136,13 +140,10 @@ func main() { flag.Parse() if versionFlag { - if buildInfo, ok := debug.ReadBuildInfo(); ok { - // TODO: use buildInfo.Settings to prepare a pseudoversion such as - // v0.0.0-20210817164053-32db794688a5+dirty on Go 1.18+. - fmt.Println(buildInfo.Main.Version) - return + if buildInfo, ok := debug.ReadBuildInfo(); ok && Version == "" { + Version = buildInfo.Main.Version } - fmt.Println("(unknown)") + fmt.Println(Version) return } diff --git a/extra/age-plugin-pq/plugin-pq.go b/extra/age-plugin-pq/plugin-pq.go index 62d8837..19aab44 100644 --- a/extra/age-plugin-pq/plugin-pq.go +++ b/extra/age-plugin-pq/plugin-pq.go @@ -30,12 +30,16 @@ implementation of age that supports plugins. Recipients work out of the box, while identities need to be converted to plugin identities with -identity. If OUTPUT already exists, it is not overwritten.` +// Version can be set at link time to override debug.BuildInfo.Main.Version when +// building manually without git history. It should look like "v1.2.3". +var Version string + func main() { log.SetFlags(0) p, err := plugin.New("pq") if err != nil { - log.Fatal(err) + errorf("failed to create plugin: %v", err) } p.RegisterFlags(nil) @@ -50,11 +54,10 @@ func main() { flag.Parse() if versionFlag { - if buildInfo, ok := debug.ReadBuildInfo(); ok { - fmt.Println(buildInfo.Main.Version) - return + if buildInfo, ok := debug.ReadBuildInfo(); ok && Version == "" { + Version = buildInfo.Main.Version } - fmt.Println("(unknown)") + fmt.Println(Version) return } diff --git a/extra/age-plugin-tag/plugin-tag.go b/extra/age-plugin-tag/plugin-tag.go index a3c5269..bc1797f 100644 --- a/extra/age-plugin-tag/plugin-tag.go +++ b/extra/age-plugin-tag/plugin-tag.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "os" + "runtime/debug" "filippo.io/age" "filippo.io/age/plugin" @@ -18,6 +19,10 @@ support to any version and implementation of age that supports plugins. Usually, tagged recipients are the public side of private keys held in hardware, where the identity side is handled by a different plugin.` +// Version can be set at link time to override debug.BuildInfo.Main.Version when +// building manually without git history. It should look like "v1.2.3". +var Version string + func main() { flag.Usage = func() { fmt.Fprintf(os.Stderr, "%s\n", usage) } @@ -25,8 +30,22 @@ func main() { if err != nil { log.Fatal(err) } + p.RegisterFlags(nil) + + versionFlag := flag.Bool("version", false, "print the version") + flag.Parse() + + if *versionFlag { + if buildInfo, ok := debug.ReadBuildInfo(); ok && Version == "" { + Version = buildInfo.Main.Version + } + fmt.Println(Version) + return + } + p.HandleRecipient(func(b []byte) (age.Recipient, error) { return tag.NewClassicRecipient(b) }) + os.Exit(p.Main()) } diff --git a/extra/age-plugin-tagpq/plugin-tagpq.go b/extra/age-plugin-tagpq/plugin-tagpq.go index 8577f65..aa6fc79 100644 --- a/extra/age-plugin-tagpq/plugin-tagpq.go +++ b/extra/age-plugin-tagpq/plugin-tagpq.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "os" + "runtime/debug" "filippo.io/age" "filippo.io/age/plugin" @@ -19,6 +20,10 @@ implementation of age that supports plugins. Usually, tagged recipients are the public side of private keys held in hardware, where the identity side is handled by a different plugin.` +// Version can be set at link time to override debug.BuildInfo.Main.Version when +// building manually without git history. It should look like "v1.2.3". +var Version string + func main() { flag.Usage = func() { fmt.Fprintf(os.Stderr, "%s\n", usage) } @@ -26,8 +31,22 @@ func main() { if err != nil { log.Fatal(err) } + p.RegisterFlags(nil) + + versionFlag := flag.Bool("version", false, "print the version") + flag.Parse() + + if *versionFlag { + if buildInfo, ok := debug.ReadBuildInfo(); ok && Version == "" { + Version = buildInfo.Main.Version + } + fmt.Println(Version) + return + } + p.HandleRecipient(func(b []byte) (age.Recipient, error) { return tag.NewHybridRecipient(b) }) + os.Exit(p.Main()) }