@@ -17,12 +17,6 @@ type Options struct {
1717 // Files to add to the zip package.
1818 Files []string
1919
20- // Root is the directory to use as the root of the zip file. This can
21- // optionally be set to specify additional files that you want within
22- // the zip. If this isn't set, we'll create a root with the files specified
23- // in Files.
24- Root string
25-
2620 // OutputPath is the path where the zip file will be written. The directory
2721 // containing this path must already exist. If a file already exist here
2822 // it will be overwritten.
@@ -36,23 +30,6 @@ type Options struct {
3630 BaseCmd * exec.Cmd
3731}
3832
39- func dittoCmd (ctx context.Context , cmd * exec.Cmd ) (* exec.Cmd , error ) {
40- path , err := exec .LookPath ("ditto" )
41- if err != nil {
42- return nil , err
43- }
44-
45- // We only set the path if it isn't set. This lets the options set the
46- // path to the codesigning binary that we use.
47- if cmd == nil {
48- cmd = exec .CommandContext (ctx , path )
49- } else if cmd .Path == "" {
50- cmd .Path = path
51- }
52-
53- return cmd , nil
54- }
55-
5633// Zip creates a zip archive for notarization using the options given.
5734//
5835// For now this works by subprocessing to "ditto" which is the recommended
@@ -65,65 +42,16 @@ func Zip(ctx context.Context, opts *Options) error {
6542 logger = hclog .NewNullLogger ()
6643 }
6744
68- // Set our root directory. If one wasn't specified, we create an empty
69- // temporary directory to act as our root and we copy over the source files
70- root := opts .Root
71- if root == "" {
72- var td string
73- var err error
74- td , err = ioutil .TempDir ("" , "gon-createzip" )
75- if err != nil {
76- return err
77- }
78- defer os .RemoveAll (td )
79- root = td
80-
81- // Build our copy command
82- var cmd * exec.Cmd
83- if opts .BaseCmd != nil {
84- cmdCopy := * opts .BaseCmd
85- cmd = & cmdCopy
86- }
87- if cmd , err = dittoCmd (ctx , cmd ); err != nil {
88- return err
89- }
90-
91- cmd .Args = []string {
92- filepath .Base (cmd .Path ),
93- }
94- cmd .Args = append (cmd .Args , opts .Files ... )
95- cmd .Args = append (cmd .Args , root )
96-
97- // We store all output in out for logging and in case there is an error
98- var out bytes.Buffer
99- cmd .Stdout = & out
100- cmd .Stderr = cmd .Stdout
101-
102- // Log what we're going to execute
103- logger .Info ("executing ditto to copy files for archiving" ,
104- "output_path" , opts .OutputPath ,
105- "command_path" , cmd .Path ,
106- "command_args" , cmd .Args ,
107- )
108-
109- // Execute copy
110- if err = cmd .Run (); err != nil {
111- logger .Error (
112- "error copying source files to create zip archive" ,
113- "err" , err ,
114- "output" , out .String (),
115- )
116- return err
117- }
45+ // Setup our root directory with the given files.
46+ root , err := createRoot (ctx , logger , opts )
47+ if err != nil {
48+ return err
11849 }
50+ defer os .RemoveAll (root )
11951
120- var cmd * exec.Cmd
121- var err error
122- if opts .BaseCmd != nil {
123- cmdCopy := * opts .BaseCmd
124- cmd = & cmdCopy
125- }
126- if cmd , err = dittoCmd (ctx , cmd ); err != nil {
52+ // Make our command for creating the archive
53+ cmd , err := dittoCmd (ctx , opts .BaseCmd )
54+ if err != nil {
12755 return err
12856 }
12957
@@ -156,3 +84,84 @@ func Zip(ctx context.Context, opts *Options) error {
15684 logger .Info ("zip archive creation complete" , "output" , out .String ())
15785 return nil
15886}
87+
88+ // dittoCmd returns an *exec.Cmd ready for executing `ditto` based on
89+ // the given base command.
90+ func dittoCmd (ctx context.Context , base * exec.Cmd ) (* exec.Cmd , error ) {
91+ path , err := exec .LookPath ("ditto" )
92+ if err != nil {
93+ return nil , err
94+ }
95+
96+ // Copy the base command so we don't modify it. If it isn't set then
97+ // we create a new command.
98+ var cmd * exec.Cmd
99+ if base == nil {
100+ cmd = exec .CommandContext (ctx , path )
101+ } else {
102+ cmdCopy := * base
103+ cmd = & cmdCopy
104+ }
105+
106+ // We only set the path if it isn't set. This lets the options set the
107+ // path to the codesigning binary that we use.
108+ if cmd .Path == "" {
109+ cmd .Path = path
110+ }
111+
112+ return cmd , nil
113+ }
114+
115+ // createRoot creates a root directory we can use `ditto` that contains all
116+ // the given Files as input. This lets us support multiple files.
117+ //
118+ // If the returned directory value is non-empty, you must defer to remove
119+ // the directory since it is meant to be a temporary directory.
120+ //
121+ // The directory is guaranteed to be empty if error is non-nil.
122+ func createRoot (ctx context.Context , logger hclog.Logger , opts * Options ) (string , error ) {
123+ // Build our copy command
124+ cmd , err := dittoCmd (ctx , opts .BaseCmd )
125+ if err != nil {
126+ return "" , err
127+ }
128+
129+ // Create our root directory
130+ root , err := ioutil .TempDir ("" , "gon-createzip" )
131+ if err != nil {
132+ return "" , err
133+ }
134+
135+ // Setup our args to copy our files into the root
136+ cmd .Args = []string {
137+ filepath .Base (cmd .Path ),
138+ }
139+ cmd .Args = append (cmd .Args , opts .Files ... )
140+ cmd .Args = append (cmd .Args , root )
141+
142+ // We store all output in out for logging and in case there is an error
143+ var out bytes.Buffer
144+ cmd .Stdout = & out
145+ cmd .Stderr = cmd .Stdout
146+
147+ // Log what we're going to execute
148+ logger .Info ("executing ditto to copy files for archiving" ,
149+ "output_path" , opts .OutputPath ,
150+ "command_path" , cmd .Path ,
151+ "command_args" , cmd .Args ,
152+ )
153+
154+ // Execute copy
155+ if err = cmd .Run (); err != nil {
156+ os .RemoveAll (root )
157+
158+ logger .Error (
159+ "error copying source files to create zip archive" ,
160+ "err" , err ,
161+ "output" , out .String (),
162+ )
163+ return "" , err
164+ }
165+
166+ return root , nil
167+ }
0 commit comments