Skip to content

Commit

Permalink
Add --version and global --help
Browse files Browse the repository at this point in the history
  • Loading branch information
mchack-work committed Oct 31, 2023
1 parent 98b2fab commit d6188b0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ reload-rules:
podman:
podman run --rm --mount type=bind,source=$(CURDIR),target=/src --mount type=bind,source=$(CURDIR)/../tkey-libs,target=/tkey-libs -w /src -it ghcr.io/tillitis/tkey-builder:2 make -j

TKEY_SIGN_VERSION ?= $(shell git describe --dirty --always | sed -n "s/^v\(.*\)/\1/p")
# .PHONY to let go-build handle deps and rebuilds
.PHONY: tkey-sign
tkey-sign:
CGO_ENABLED=$(BUILD_CGO_ENABLED) go build -ldflags "-X main.signerAppNoTouch=$(TKEY_SIGNER_APP_NO_TOUCH)"
CGO_ENABLED=$(BUILD_CGO_ENABLED) go build -ldflags "-X main.version=$(TKEY_SIGN_VERSION) -X main.signerAppNoTouch=$(TKEY_SIGNER_APP_NO_TOUCH)" -trimpath -o tkey-sign

.PHONY: tkey-sign.exe
tkey-sign.exe:
Expand Down
44 changes: 43 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"log"
"os"
"os/signal"
"runtime/debug"
"strings"
"syscall"

Expand All @@ -33,6 +34,8 @@ var signerBinary []byte
// Use when printing err/diag msgs
var le = log.New(os.Stderr, "", 0)

var version string

// May be set to non-empty at build time to indicate that the signer
// app has been compiled with touch requirement removed.
var signerAppNoTouch string
Expand Down Expand Up @@ -242,16 +245,38 @@ func handleSignals(action func(), sig ...os.Signal) {
}()
}

func readBuildInfo() string {
var v string

if info, ok := debug.ReadBuildInfo(); ok {
sb := strings.Builder{}
sb.WriteString("devel")
for _, setting := range info.Settings {
if strings.HasPrefix(setting.Key, "vcs") {
sb.WriteString(fmt.Sprintf(" %s=%s", setting.Key, setting.Value))
}
}
v = sb.String()
}
return v
}

func main() {
var fileName, fileUSS, fileSignature, filePubkey, devPath string
var speed int
var enterUSS, showPubkeyOnly, verbose, helpOnlySign, helpOnlyVerify bool
var enterUSS, showPubkeyOnly, verbose, helpOnly, helpOnlySign, helpOnlyVerify, versionOnly bool

signString := "sign"
verifyString := "verify"

if version == "" {
version = readBuildInfo()
}

// Default text to show
root := pflag.NewFlagSet("root", pflag.ExitOnError)
root.BoolVar(&versionOnly, "version", false, "Output version information.")
root.BoolVar(&helpOnly, "help", false, "Give help text.")
root.Usage = func() {
desc := fmt.Sprintf(`%[1]s signs the data provided in FILE (the "message")
using the Tillitis TKey. The message will be hashed using
Expand Down Expand Up @@ -327,6 +352,23 @@ Use <command> --help for further help, i.e. %[1]s verify --help`, os.Args[0])
os.Exit(2)
}

// version? Print and exit
if len(os.Args) == 2 {
if err := root.Parse(os.Args); err != nil {
le.Printf("Error parsing input arguments: %v\n", err)
os.Exit(2)
}
if versionOnly {
fmt.Printf("tkey-sign %s\n", version)
os.Exit(0)
}

if helpOnly {
root.Usage()
os.Exit(0)
}
}

switch os.Args[1] {
case signString:
if err := cmdSign.Parse(os.Args[2:]); err != nil {
Expand Down

0 comments on commit d6188b0

Please sign in to comment.