aboutsummaryrefslogtreecommitdiff
path: root/cmd/sigsum-debug/key/key.go
blob: 0d9e580e14f2325e4adf35a821c6d48b51f03208 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package key

import (
	"flag"
	"fmt"
	"log"

	"git.sigsum.org/sigsum-go/cmd/sigsum-debug/key/hash"
	"git.sigsum.org/sigsum-go/cmd/sigsum-debug/key/private"
	"git.sigsum.org/sigsum-go/cmd/sigsum-debug/key/public"
	"git.sigsum.org/sigsum-go/internal/options"
)

const usage = `
sigsum-debug key generates private keys, public keys, and key hashes.

Usage:

  sigsum-debug key help     Outputs a usage message
  sigsum-debug key private  Outputs a new private key
  sigsum-debug key public   Outputs a public key for a private key on stdin
  sigsum-debug key hash     Outputs a key hash for a public key on stdin

`

func Main(args []string) error {
	var err error

	opt := options.New(args, func() { log.Printf(usage[1:]) }, func(_ *flag.FlagSet) {})
	switch opt.Name() {
	case "help", "":
		opt.Usage()
	case "private":
		err = private.Main(opt.Args())
	case "public":
		err = public.Main(opt.Args())
	case "hash":
		err = hash.Main(opt.Args())
	default:
		err = fmt.Errorf("invalid command %q, try \"help\"", opt.Name())
	}
	if err != nil {
		format := " %s: %w"
		if len(opt.Name()) == 0 {
			format = "%s: %w"
		}
		err = fmt.Errorf(format, opt.Name(), err)
	}

	return err
}