validate.go

 1package commands
 2
 3import (
 4	"fmt"
 5
 6	"github.com/MichaelMure/git-bug/cache"
 7	"github.com/MichaelMure/git-bug/identity"
 8	"github.com/MichaelMure/git-bug/util/interrupt"
 9	"github.com/MichaelMure/git-bug/validate"
10	"github.com/pkg/errors"
11	"github.com/spf13/cobra"
12)
13
14func runValidate(cmd *cobra.Command, args []string) error {
15	backend, err := cache.NewRepoCache(repo)
16	if err != nil {
17		return err
18	}
19	defer backend.Close()
20	interrupt.RegisterCleaner(backend.Close)
21
22	validator, err := validate.NewValidator(backend)
23	if err != nil {
24		return err
25	}
26
27	fmt.Printf("first commit signed with key: %s\n", identity.EncodeKeyFingerprint(validator.FirstKey.PublicKey.Fingerprint))
28
29	var refErr error
30	for _, ref := range args {
31		hash, err := backend.ResolveRef(ref)
32		if err != nil {
33			return err
34		}
35
36		_, err = validator.ValidateRef(hash)
37		if err != nil {
38			refErr = errors.Wrapf(refErr, "ref %s check fail", ref)
39			fmt.Printf("ref %s\tFAIL\n", ref)
40		} else {
41			fmt.Printf("ref %s\tOK\n", ref)
42		}
43	}
44	if refErr != nil {
45		return refErr
46	}
47
48	return nil
49}
50
51var validateCmd = &cobra.Command{
52	Use:     "validate",
53	Short:   "Validate identities and commits signatures.",
54	PreRunE: loadRepo,
55	RunE:    runValidate,
56}
57
58func init() {
59	RootCmd.AddCommand(validateCmd)
60	validateCmd.Flags().SortFlags = false
61}