|
| 1 | +// Command releasemanifest writes and signs terminal-release.json, the release |
| 2 | +// asset ZenNotes desktop reads to update the CLI it manages. The release |
| 3 | +// workflow runs it after GoReleaser; CONTRIBUTING.md describes the signing key. |
| 4 | +// |
| 5 | +// go run ./scripts/releasemanifest keygen -out <path> |
| 6 | +// go run ./scripts/releasemanifest build -dist dist -tag vX.Y.Z -commit <sha> -require-signature |
| 7 | +// go run ./scripts/releasemanifest verify -manifest <path> -sig <path> -public-key-file <path> |
| 8 | +package main |
| 9 | + |
| 10 | +import ( |
| 11 | + "errors" |
| 12 | + "flag" |
| 13 | + "fmt" |
| 14 | + "io" |
| 15 | + "os" |
| 16 | + "path/filepath" |
| 17 | + "runtime" |
| 18 | +) |
| 19 | + |
| 20 | +const usage = `usage: releasemanifest <command> [flags] |
| 21 | +
|
| 22 | + keygen -out <path> [-key-id zn-release-1] |
| 23 | + build -dist <dir> -tag vX.Y.Z -commit <40 hex> [-out <path>] [-sig <path>] |
| 24 | + [-key-env ZN_RELEASE_SIGNING_KEY] [-key-id zn-release-1] [-require-signature] |
| 25 | + verify -manifest <path> -sig <path> -public-key-file <path>` |
| 26 | + |
| 27 | +func main() { |
| 28 | + if err := run(os.Args[1:], os.Stdout, os.Stderr); err != nil { |
| 29 | + if !errors.Is(err, flag.ErrHelp) { |
| 30 | + fmt.Fprintln(os.Stderr, "releasemanifest: "+err.Error()) |
| 31 | + } |
| 32 | + os.Exit(1) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func run(args []string, stdout, stderr io.Writer) error { |
| 37 | + if len(args) == 0 { |
| 38 | + return errors.New(usage) |
| 39 | + } |
| 40 | + fs := flag.NewFlagSet(args[0], flag.ContinueOnError) |
| 41 | + fs.SetOutput(stderr) |
| 42 | + parse := func() error { |
| 43 | + if err := fs.Parse(args[1:]); err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + if fs.NArg() > 0 { |
| 47 | + return fmt.Errorf("%s: unexpected argument %q", args[0], fs.Arg(0)) |
| 48 | + } |
| 49 | + return nil |
| 50 | + } |
| 51 | + switch args[0] { |
| 52 | + case "keygen": |
| 53 | + out := fs.String("out", "", "file that receives the base64 seed (created with mode 0600)") |
| 54 | + keyID := fs.String("key-id", defaultKeyID, "key id printed with the public key") |
| 55 | + if err := parse(); err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + if *out == "" { |
| 59 | + return errors.New("keygen: -out is required") |
| 60 | + } |
| 61 | + return keygen(*out, *keyID, stdout) |
| 62 | + case "build": |
| 63 | + c := buildConfig{GOOS: runtime.GOOS, GOARCH: runtime.GOARCH} |
| 64 | + fs.StringVar(&c.Dist, "dist", "", "GoReleaser dist directory") |
| 65 | + fs.StringVar(&c.Tag, "tag", "", "release tag, vX.Y.Z") |
| 66 | + fs.StringVar(&c.Commit, "commit", "", "40-character source commit") |
| 67 | + fs.StringVar(&c.Out, "out", "", "manifest path (default <dist>/terminal-release.json)") |
| 68 | + fs.StringVar(&c.Sig, "sig", "", "signature path (default <dist>/terminal-release.json.sig)") |
| 69 | + fs.StringVar(&c.KeyEnv, "key-env", "ZN_RELEASE_SIGNING_KEY", "environment variable holding the base64 Ed25519 seed") |
| 70 | + fs.StringVar(&c.KeyID, "key-id", defaultKeyID, "key id recorded in the signature") |
| 71 | + fs.BoolVar(&c.RequireSignature, "require-signature", false, "fail when no signing key is set") |
| 72 | + if err := parse(); err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + if c.Dist == "" || c.Tag == "" || c.Commit == "" { |
| 76 | + return errors.New("build: -dist, -tag and -commit are required") |
| 77 | + } |
| 78 | + if c.Out == "" { |
| 79 | + c.Out = filepath.Join(c.Dist, "terminal-release.json") |
| 80 | + } |
| 81 | + if c.Sig == "" { |
| 82 | + c.Sig = filepath.Join(c.Dist, "terminal-release.json.sig") |
| 83 | + } |
| 84 | + return build(c, stdout, stderr) |
| 85 | + case "verify": |
| 86 | + manifest := fs.String("manifest", "", "terminal-release.json") |
| 87 | + sig := fs.String("sig", "", "terminal-release.json.sig") |
| 88 | + pub := fs.String("public-key-file", "", "<key id>.pub holding the base64 public key") |
| 89 | + if err := parse(); err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + if *manifest == "" || *sig == "" || *pub == "" { |
| 93 | + return errors.New("verify: -manifest, -sig and -public-key-file are required") |
| 94 | + } |
| 95 | + if err := verify(*manifest, *sig, *pub); err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + fmt.Fprintf(stdout, "verified %s with %s\n", *manifest, *pub) |
| 99 | + return nil |
| 100 | + default: |
| 101 | + return fmt.Errorf("unknown command %q\n%s", args[0], usage) |
| 102 | + } |
| 103 | +} |
0 commit comments