From c32d9bf24c3b670c3a56ea55aeefb6232f3d4ec8 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Tue, 13 Feb 2018 01:17:35 +0100 Subject: switch to subcommands --- verify.go | 85 +++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 53 insertions(+), 32 deletions(-) (limited to 'verify.go') diff --git a/verify.go b/verify.go index 0934762..c38955d 100644 --- a/verify.go +++ b/verify.go @@ -1,59 +1,80 @@ package main import ( + "context" "flag" "fmt" "io/ioutil" + "log" "strings" "dim13.org/signify/b64file" "dim13.org/signify/key" "dim13.org/signify/zsig" + "github.com/google/subcommands" ) // Usage: signify -V [-eqz] [-p pubkey] [-t keytype] [-x sigfile] -m message -func verify(args []string) error { - opts := flag.NewFlagSet("verify", flag.ExitOnError) - var ( - embedded = opts.Bool("e", false, "Embed message") - quiet = opts.Bool("q", false, "Quiet mode") - zip = opts.Bool("z", false, "Verify gzip archive") // TODO - pubFile = opts.String("p", "", "Public key file") - keyType = opts.String("t", "", "Key type") // TODO - sigFile = opts.String("x", "", "Signature file") - msgFile = opts.String("m", "", "Message file (required)") - ) - opts.Parse(args) - if *msgFile == "" { - opts.Usage() - return nil - } - if *sigFile == "" { - *sigFile = SigName(*msgFile) - } - _ = keyType // TODO +type verifyCommand struct { + embedded bool + quiet bool + zip bool + pubFile string + keyFile string + sigFile string + msgFile string +} + +func (v *verifyCommand) Name() string { return "verify" } +func (v *verifyCommand) Synopsis() string { return "verify signature" } +func (v *verifyCommand) Usage() string { + return "verify [-eqz] [-p pubkey] [-t keytype] [-x sigfile] -m message\n" +} + +func (v *verifyCommand) SetFlags(f *flag.FlagSet) { + f.BoolVar(&v.embedded, "e", false, "embed message") + f.BoolVar(&v.quiet, "q", false, "quiet mode") + f.BoolVar(&v.zip, "z", false, "verify gzip archive") // TODO + f.StringVar(&v.pubFile, "p", "", "public key file") + f.StringVar(&v.keyFile, "t", "", "key type") // TODO + f.StringVar(&v.sigFile, "x", "", "signature file") + f.StringVar(&v.msgFile, "m", "", "message file (required)") +} + +func (v *verifyCommand) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus { + if v.msgFile == "" { + f.Usage() + return subcommands.ExitUsageError + } + if v.sigFile == "" { + v.sigFile = SigName(v.msgFile) + } + // TODO keyType switch { - case *zip && *embedded: - return ErrEZ - case *zip: - if err := verifyGzip(*pubFile, *sigFile); err != nil { - return err + case v.zip && v.embedded: + return subcommands.ExitUsageError + case v.zip: + if err := verifyGzip(v.pubFile, v.sigFile); err != nil { + log.Println(err) + return subcommands.ExitFailure } - case *embedded: - if err := verifyEmbedded(*pubFile, *sigFile); err != nil { - return err + case v.embedded: + if err := verifyEmbedded(v.pubFile, v.sigFile); err != nil { + log.Println(err) + return subcommands.ExitFailure } default: - if err := verifyPlain(*pubFile, *sigFile, *msgFile); err != nil { - return err + if err := verifyPlain(v.pubFile, v.sigFile, v.msgFile); err != nil { + log.Println(err) + return subcommands.ExitFailure } } - if !*quiet { + if !v.quiet { fmt.Println("Signature Verified") } - return nil + return subcommands.ExitSuccess } func verifyPlain(pubFile, sigFile, msgFile string) error { -- cgit v1.2.3