aboutsummaryrefslogtreecommitdiff
path: root/sign.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2018-02-13 01:17:35 +0100
committerDimitri Sokolyuk <demon@dim13.org>2018-02-13 01:17:35 +0100
commitc32d9bf24c3b670c3a56ea55aeefb6232f3d4ec8 (patch)
treef9891ff5b851d40c2f48c8e73e36e32197bfcc89 /sign.go
parent886201867ac7383535c08d81dbabc6c9c77fe855 (diff)
switch to subcommands
Diffstat (limited to 'sign.go')
-rw-r--r--sign.go72
1 files changed, 45 insertions, 27 deletions
diff --git a/sign.go b/sign.go
index 0c56b69..91b58ea 100644
--- a/sign.go
+++ b/sign.go
@@ -1,6 +1,7 @@
package main
import (
+ "context"
"flag"
"io/ioutil"
"log"
@@ -9,46 +10,63 @@ import (
"dim13.org/signify/b64file"
"dim13.org/signify/key"
"dim13.org/signify/zsig"
+ "github.com/google/subcommands"
)
// Usage: signify -S [-ez] [-x sigfile] -s seckey -m message
-func sign(args []string) error {
- opts := flag.NewFlagSet("sign", flag.ExitOnError)
- var (
- embedded = opts.Bool("e", false, "Embed the message")
- zip = opts.Bool("z", false, "Sign gzip archive") // TODO
- sigFile = opts.String("x", "", "Signature file")
- secFile = opts.String("s", "", "Secret file (required)")
- msgFile = opts.String("m", "", "Message file (required)")
- )
- opts.Parse(args)
- if *secFile == "" || *msgFile == "" {
- opts.Usage()
- return nil
- }
- if *sigFile == "" {
- *sigFile = SigName(*msgFile)
+type signCommand struct {
+ embedded bool
+ zip bool
+ sigFile string
+ secFile string
+ msgFile string
+}
+
+func (s *signCommand) Name() string { return "sign" }
+func (s *signCommand) Synopsis() string { return "sign file" }
+func (s *signCommand) Usage() string {
+ return "sign [-ez] [-x sigfile] -s seckey -m message\n"
+}
+
+func (s *signCommand) SetFlags(f *flag.FlagSet) {
+ f.BoolVar(&s.embedded, "e", false, "embed the message")
+ f.BoolVar(&s.zip, "z", false, "sign gzip archive") // TODO
+ f.StringVar(&s.sigFile, "x", "", "signature file")
+ f.StringVar(&s.secFile, "s", "", "secret file (required)")
+ f.StringVar(&s.msgFile, "m", "", "message file (required)")
+}
+
+func (s *signCommand) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
+ if s.secFile == "" || s.msgFile == "" {
+ f.Usage()
+ return subcommands.ExitUsageError
+ }
+ if s.sigFile == "" {
+ s.sigFile = SigName(s.msgFile)
}
switch {
- case *zip && *embedded:
- return ErrEZ
- case *zip:
- if err := signGzip(*secFile, *msgFile, *sigFile); err != nil {
- return err
+ case s.zip && s.embedded:
+ return subcommands.ExitUsageError
+ case s.zip:
+ if err := signGzip(s.secFile, s.msgFile, s.sigFile); err != nil {
+ log.Println(err)
+ return subcommands.ExitFailure
}
- case *embedded:
- if err := signEmbedded(*secFile, *msgFile, *sigFile); err != nil {
- return err
+ case s.embedded:
+ if err := signEmbedded(s.secFile, s.msgFile, s.sigFile); err != nil {
+ log.Println(err)
+ return subcommands.ExitFailure
}
default:
- if err := signPlain(*secFile, *msgFile, *sigFile); err != nil {
- return err
+ if err := signPlain(s.secFile, s.msgFile, s.sigFile); err != nil {
+ log.Println(err)
+ return subcommands.ExitFailure
}
}
- return nil
+ return subcommands.ExitSuccess
}
func signPlain(secFile, msgFile, sigFile string) error {