Skip to content

Commit d0c605f

Browse files
committed
modified HandleExtras so collection is optional
fixed bug in signature collection
1 parent 316a595 commit d0c605f

File tree

8 files changed

+57
-48
lines changed

8 files changed

+57
-48
lines changed

cmd/build.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package cmd
1616

1717
import (
18-
"fmt"
1918
"github.com/nikogura/gomason/pkg/gomason"
2019
"github.com/spf13/cobra"
2120
"io/ioutil"
@@ -53,8 +52,6 @@ Binaries are dropped into the current working directory.
5352
log.Fatalf("Failed to create temp dir: %s", err)
5453
}
5554

56-
log.Printf("[DEBUG] Created temp dir %s", rootWorkDir)
57-
5855
defer os.RemoveAll(rootWorkDir)
5956

6057
meta, err := gomason.ReadMetadata(gomason.METADATA_FILENAME)
@@ -87,23 +84,19 @@ Binaries are dropped into the current working directory.
8784
if err != nil {
8885
log.Fatalf("error running go test: %s", err)
8986
}
90-
91-
fmt.Print("Tests Succeeded!\n\n")
9287
}
9388

9489
err = lang.Build(workDir, meta, buildSkipTargets)
9590
if err != nil {
9691
log.Fatalf("build failed: %s", err)
9792
}
9893

99-
fmt.Print("Build Succeeded!\n\n")
100-
10194
err = gm.HandleArtifacts(meta, workDir, cwd, false, false, true, buildSkipTargets)
10295
if err != nil {
10396
log.Fatalf("signing failed: %s", err)
10497
}
10598

106-
err = gm.HandleExtras(meta, workDir, cwd, false, false)
99+
err = gm.HandleExtras(meta, workDir, cwd, false, false, true)
107100
if err != nil {
108101
log.Fatalf("Extra artifact processing failed: %s", err)
109102
}

cmd/publish.go

+3-12
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
package cmd
1616

1717
import (
18-
"fmt"
19-
"io/ioutil"
2018
"log"
2119
"os"
2220

@@ -46,14 +44,12 @@ Publish will upload your binaries to wherever it is you've configured them to go
4644
if err != nil {
4745
log.Fatalf("Failed to get current working directory: %s", err)
4846
}
49-
rootWorkDir, err := ioutil.TempDir("", "gomason")
47+
rootWorkDir, err := os.MkdirTemp("", "gomason")
5048
if err != nil {
5149
log.Fatalf("Failed to create temp dir: %s", err)
5250
}
5351
defer os.RemoveAll(rootWorkDir)
5452

55-
log.Printf("[DEBUG] Created temp dir %s", rootWorkDir)
56-
5753
meta, err := gomason.ReadMetadata(gomason.METADATA_FILENAME)
5854
if err != nil {
5955
log.Fatalf("failed to read metadata: %s", err)
@@ -76,8 +72,6 @@ Publish will upload your binaries to wherever it is you've configured them to go
7672
log.Fatalf("Failed getting current working directory.")
7773
}
7874

79-
fmt.Printf("Workdir is %s\n", workDir)
80-
8175
for _, t := range meta.PublishInfo.Targets {
8276
if meta.PublishInfo.SkipSigning {
8377
err = gm.PublishFile(meta, t.Source)
@@ -114,24 +108,21 @@ Publish will upload your binaries to wherever it is you've configured them to go
114108
log.Fatalf("error running go test: %s", err)
115109
}
116110

117-
fmt.Print("Tests Succeeded!\n\n")
118111
}
119112

120113
err = lang.Build(workDir, meta, buildSkipTargets)
121114
if err != nil {
122115
log.Fatalf("build failed: %s", err)
123116
}
124117

125-
fmt.Print("Build Succeeded!\n\n")
126-
127118
if meta.PublishInfo.SkipSigning {
128119
log.Printf("[DEBUG] Skipping signing due to 'skip-signing': true in metadata file")
129120
err = gm.HandleArtifacts(meta, workDir, cwd, false, true, false, buildSkipTargets)
130121
if err != nil {
131122
log.Fatalf("post-build processing failed: %s", err)
132123
}
133124

134-
err = gm.HandleExtras(meta, workDir, cwd, false, true)
125+
err = gm.HandleExtras(meta, workDir, cwd, false, true, false)
135126
if err != nil {
136127
log.Fatalf("Extra artifact processing failed: %s", err)
137128
}
@@ -142,7 +133,7 @@ Publish will upload your binaries to wherever it is you've configured them to go
142133
log.Fatalf("post-build processing failed: %s", err)
143134
}
144135

145-
err = gm.HandleExtras(meta, workDir, cwd, true, true)
136+
err = gm.HandleExtras(meta, workDir, cwd, true, true, false)
146137
if err != nil {
147138
log.Fatalf("Extra artifact processing failed: %s", err)
148139
}

cmd/sign.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ Signing sorta implies something to sign, which in turn, implies that it built, w
4949
log.Fatalf("Failed to create temp dir: %s", err)
5050
}
5151

52-
log.Printf("[DEBUG] Created temp dir %s", rootWorkDir)
53-
5452
defer os.RemoveAll(rootWorkDir)
5553

5654
meta, err := gomason.ReadMetadata(gomason.METADATA_FILENAME)
@@ -90,14 +88,12 @@ Signing sorta implies something to sign, which in turn, implies that it built, w
9088
log.Fatalf("build failed: %s", err)
9189
}
9290

93-
log.Printf("Build Succeeded!\n\n")
94-
9591
err = gm.HandleArtifacts(meta, workDir, cwd, true, false, true, buildSkipTargets)
9692
if err != nil {
9793
log.Fatalf("signing failed: %s", err)
9894
}
9995

100-
err = gm.HandleExtras(meta, workDir, cwd, true, false)
96+
err = gm.HandleExtras(meta, workDir, cwd, true, false, true)
10197
if err != nil {
10298
log.Fatalf("Extra artifact processing failed: %s", err)
10399
}

cmd/test.go

-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package cmd
1616

1717
import (
18-
"fmt"
1918
"github.com/nikogura/gomason/pkg/gomason"
2019
"github.com/spf13/cobra"
2120
"io/ioutil"
@@ -51,8 +50,6 @@ Sometimes you need the benefits of a full system here. Now. Right at your fing
5150
log.Fatalf("Failed to create temp dir: %s", err)
5251
}
5352

54-
log.Printf("[DEBUG] Created temp dir %s", rootWorkDir)
55-
5653
defer os.RemoveAll(rootWorkDir)
5754

5855
meta, err := gomason.ReadMetadata(gomason.METADATA_FILENAME)
@@ -85,7 +82,6 @@ Sometimes you need the benefits of a full system here. Now. Right at your fing
8582
log.Fatalf("error running go test: %s", err)
8683
}
8784

88-
fmt.Print("Success!\n\n")
8985
},
9086
}
9187

pkg/gomason/golang.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func (g Golang) Build(gopath string, meta Metadata, skipTargets string) (err err
248248
continue
249249
}
250250

251-
logrus.Debugf("Building target: %q", target.Name)
251+
logrus.Debugf("Building target: %q in dir %s", target.Name, wd)
252252

253253
// This gets weird because go's exec shell doesn't like the arg format that gox expects
254254
// Building it thusly keeps the various quoting levels straight
@@ -280,7 +280,7 @@ func (g Golang) Build(gopath string, meta Metadata, skipTargets string) (err err
280280

281281
args := gox + cgo + ldflags + ` -osarch="` + target.Name + `"` + " ./..."
282282

283-
logrus.Debugf("Running gox with: %s", args)
283+
logrus.Debugf("Running gox with: %s in dir %s", args, wd)
284284

285285
// Calling it through sh makes everything happy
286286
cmd := exec.Command("sh", "-c", args)

pkg/gomason/golang_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ Expire-Date: 0
343343
t.Errorf("post-build processing failed: %s", err)
344344
}
345345

346-
err = g.HandleExtras(meta, gopath, cwd, false, true)
346+
err = g.HandleExtras(meta, gopath, cwd, false, true, true)
347347
if err != nil {
348348
t.Errorf("Extra artifact processing failed: %s", err)
349349
}

pkg/gomason/gomason.go

+42-14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99
"regexp"
1010
"strings"
11+
"time"
1112

1213
"github.com/pkg/errors"
1314
"gopkg.in/ini.v1"
@@ -18,7 +19,7 @@ func init() {
1819
}
1920

2021
// VERSION is the current gomason version
21-
const VERSION = "2.12.0"
22+
const VERSION = "2.12.1"
2223

2324
// METADATA_FILENAME The default gomason metadata file name
2425
const METADATA_FILENAME = "metadata.json"
@@ -181,7 +182,7 @@ func (g *Gomason) HandleArtifacts(meta Metadata, gopath string, cwd string, sign
181182
return err
182183
}
183184

184-
targetSuffix := fmt.Sprintf(".+_%s_%s", osname, archname)
185+
targetSuffix := fmt.Sprintf(".+_%s_%s$", osname, archname)
185186
targetRegex := regexp.MustCompile(targetSuffix)
186187

187188
for _, file := range files {
@@ -190,15 +191,16 @@ func (g *Gomason) HandleArtifacts(meta Metadata, gopath string, cwd string, sign
190191
if matched {
191192
filename := fmt.Sprintf("%s/%s", workdir, file.Name())
192193

193-
logrus.Debugf("\tHandling %s\n", filename)
194+
logrus.Debugf("Handling %s", filename)
194195

195196
if _, err := os.Stat(filename); os.IsNotExist(err) {
196-
err = errors.Wrapf(err, "failed to build binary: %s\n", filename)
197+
err = errors.Wrapf(err, "failed building binary: %s\n", filename)
197198
return err
198199
}
199200

200201
// sign 'em if we're signing
201202
if sign {
203+
logrus.Debugf("Signing %s", filename)
202204
err = g.SignBinary(meta, filename)
203205
if err != nil {
204206
err = errors.Wrapf(err, "failed to sign binary %s", filename)
@@ -208,15 +210,17 @@ func (g *Gomason) HandleArtifacts(meta Metadata, gopath string, cwd string, sign
208210

209211
// publish and return if we're publishing
210212
if publish {
213+
logrus.Debugf("Publishing %s", filename)
211214
err = g.PublishFile(meta, filename)
212215
if err != nil {
213216
err = errors.Wrap(err, "failed to publish binary")
214217
return err
215218
}
216219
}
217220

221+
// Collect up the stuff we built, and dump 'em into the cwd where we called gomason
218222
if collect {
219-
// if we're not publishing, collect up the stuff we built, and dump 'em into the cwd where we called gomason
223+
logrus.Debugf("Collecting %s", filename)
220224
err := CollectFileAndSignature(cwd, filename)
221225
if err != nil {
222226
err = errors.Wrap(err, "failed to collect binaries")
@@ -234,7 +238,7 @@ func (g *Gomason) HandleArtifacts(meta Metadata, gopath string, cwd string, sign
234238
// HandleExtras loops over the expected files built by Build() and optionally signs them and publishes them along with their signatures (if signing).
235239
//
236240
// If not publishing, the binaries (and their optional signatures) are collected and dumped into the directory where gomason was called. (Typically the root of a go project).
237-
func (g *Gomason) HandleExtras(meta Metadata, gopath string, cwd string, sign bool, publish bool) (err error) {
241+
func (g *Gomason) HandleExtras(meta Metadata, gopath string, cwd string, sign bool, publish bool, collect bool) (err error) {
238242

239243
// loop through the built things for each type of build target
240244
for _, extra := range meta.BuildInfo.Extras {
@@ -250,6 +254,7 @@ func (g *Gomason) HandleExtras(meta Metadata, gopath string, cwd string, sign bo
250254

251255
// sign 'em if we're signing
252256
if sign {
257+
logrus.Debugf("Signing %s", filename)
253258
err = g.SignBinary(meta, filename)
254259
if err != nil {
255260
err = errors.Wrapf(err, "failed to sign extra artifact %s", filename)
@@ -259,14 +264,18 @@ func (g *Gomason) HandleExtras(meta Metadata, gopath string, cwd string, sign bo
259264

260265
// publish and return if we're publishing
261266
if publish {
267+
logrus.Debugf("Publishing %s", filename)
262268
err = g.PublishFile(meta, filename)
263269
if err != nil {
264270
err = errors.Wrapf(err, "failed to publish extra artifact %s", filename)
265271
return err
266272
}
267273

268-
} else {
269-
// if we're not publishing, collect up the stuff we built, and dump 'em into the cwd where we called gomason
274+
}
275+
276+
if collect {
277+
// Collect up the stuff we built, and dump 'em into the cwd where we called gomason
278+
logrus.Debugf("Collecting %s", filename)
270279
err := CollectFileAndSignature(cwd, filename)
271280
if err != nil {
272281
err = errors.Wrapf(err, "failed to collect binary %s", filename)
@@ -278,16 +287,30 @@ func (g *Gomason) HandleExtras(meta Metadata, gopath string, cwd string, sign bo
278287
return err
279288
}
280289

290+
func DebugPrint(filename, tag string) {
291+
ts := time.Now().UnixNano()
292+
cwd, _ := os.Getwd()
293+
logrus.Debugf("%s Current working directory: %s\n", tag, cwd)
294+
295+
_, err := os.Stat(filename)
296+
if err != nil {
297+
logrus.Debugf("%s File %s missing at %v", tag, filename, ts)
298+
} else {
299+
logrus.Debugf("%s File %s exists at %v", tag, filename, ts)
300+
}
301+
}
302+
281303
// CollectFileAndSignature grabs a file and the signature if it exists and copies it from the temp workspace into the CWD where gomason was called. Does nothing at all if the file is currently in cwd.
282304
func CollectFileAndSignature(cwd string, filename string) (err error) {
283-
fmt.Printf("[DEBUG] Collecting Binaries and Signatures (if signing)\n")
305+
logrus.Debugf("Collecting Files and Signatures")
284306

285307
binaryDestinationPath := fmt.Sprintf("%s/%s", cwd, filepath.Base(filename))
286308

287309
if binaryDestinationPath != filename {
288310
fileInfo, err := os.Stat(filename)
289311
if err != nil {
290312
err = errors.Wrapf(err, "failed statting file %s", filename)
313+
return err
291314
}
292315

293316
contents, err := os.ReadFile(filename)
@@ -312,21 +335,26 @@ func CollectFileAndSignature(cwd string, filename string) (err error) {
312335
if _, err := os.Stat(sigName); !os.IsNotExist(err) {
313336
signatureDestinationPath := fmt.Sprintf("%s/%s", cwd, sigName)
314337
if signatureDestinationPath != sigName {
315-
contents, err := os.ReadFile(filename)
338+
fileInfo, err := os.Stat(sigName)
339+
if err != nil {
340+
err = errors.Wrapf(err, "failed statting file %s", sigName)
341+
return err
342+
}
343+
contents, err := os.ReadFile(sigName)
316344
if err != nil {
317-
err = errors.Wrapf(err, "failed reading file %q", filename)
345+
err = errors.Wrapf(err, "failed reading file %q", sigName)
318346
return err
319347
}
320348

321-
err = os.WriteFile(signatureDestinationPath, contents, 0644)
349+
err = os.WriteFile(signatureDestinationPath, contents, fileInfo.Mode())
322350
if err != nil {
323351
err = errors.Wrapf(err, "failed writing file %s", signatureDestinationPath)
324352
return err
325353
}
326354

327-
err = os.Remove(filename)
355+
err = os.Remove(sigName)
328356
if err != nil {
329-
err = errors.Wrapf(err, "failed removing file %s", filename)
357+
err = errors.Wrapf(err, "failed removing file %s", sigName)
330358
return err
331359
}
332360
}

pkg/gomason/signing.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const defaultSigningProgram = "gpg"
1414

1515
// SignBinary signs the given binary based on the entity and program given in metadata file, possibly overridden by information in ~/.gomason
1616
func (g *Gomason) SignBinary(meta Metadata, binary string) (err error) {
17-
logrus.Debugf("Preparing to sign binary %s", binary)
17+
logrus.Debugf("Preparing to sign file %s", binary)
1818

1919
// pull signing info out of metadata file
2020
signInfo := meta.SignInfo
@@ -110,11 +110,16 @@ func SignGPG(binary string, signingEntity string, meta Metadata) (err error) {
110110

111111
cmd.Env = os.Environ()
112112

113-
err = cmd.Run()
113+
err = cmd.Start()
114114
if err != nil {
115115
err = errors.Wrap(err, fmt.Sprintf("failed to run %q", shellCmd))
116116
}
117117

118+
err = cmd.Wait()
119+
if err != nil {
120+
err = errors.Wrap(err, fmt.Sprintf("error waiting for %q to finish", shellCmd))
121+
}
122+
118123
return err
119124
}
120125

0 commit comments

Comments
 (0)