Skip to content

Commit ef47527

Browse files
committed
staple: add stapler
1 parent f64e1df commit ef47527

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

staple/staple.go

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Package staple staples a notarization ticket to a file, allowing it
2+
// to be validated offline. This only works for files of type "app", "dmg",
3+
// or "pkg".
4+
package staple
5+
6+
import (
7+
"bytes"
8+
"context"
9+
"fmt"
10+
"os/exec"
11+
"path/filepath"
12+
13+
"github.com/hashicorp/go-hclog"
14+
)
15+
16+
// Options are the options for creating the zip archive.
17+
type Options struct {
18+
// File to staple. It is stapled in-place.
19+
File string
20+
21+
// Logger is the logger to use. If this is nil then no logging will be done.
22+
Logger hclog.Logger
23+
24+
// BaseCmd is the base command for executing the codesign binary. This is
25+
// used for tests to overwrite where the codesign binary is.
26+
BaseCmd *exec.Cmd
27+
}
28+
29+
// Staple staples the notarization ticket to a file.
30+
func Staple(ctx context.Context, opts *Options) error {
31+
logger := opts.Logger
32+
if logger == nil {
33+
logger = hclog.NewNullLogger()
34+
}
35+
36+
// Build our command
37+
var cmd exec.Cmd
38+
if opts.BaseCmd != nil {
39+
cmd = *opts.BaseCmd
40+
}
41+
42+
// We only set the path if it isn't set. This lets the options set the
43+
// path to the codesigning binary that we use.
44+
if cmd.Path == "" {
45+
path, err := exec.LookPath("xcrun")
46+
if err != nil {
47+
return err
48+
}
49+
cmd.Path = path
50+
}
51+
52+
cmd.Args = []string{
53+
filepath.Base(cmd.Path),
54+
"stapler",
55+
"staple",
56+
opts.File,
57+
}
58+
59+
// We store all output in out for logging and in case there is an error
60+
var out bytes.Buffer
61+
cmd.Stdout = &out
62+
cmd.Stderr = cmd.Stdout
63+
64+
// Log what we're going to execute
65+
logger.Info("executing stapler",
66+
"file", opts.File,
67+
"command_path", cmd.Path,
68+
"command_args", cmd.Args,
69+
)
70+
71+
// Execute
72+
if err := cmd.Run(); err != nil {
73+
logger.Error("error stapling", "err", err, "output", out.String())
74+
return fmt.Errorf("error stapling:\n\n%s", out.String())
75+
}
76+
77+
logger.Info("stapling complete", "file", opts.File)
78+
return nil
79+
}

0 commit comments

Comments
 (0)