From d0c605f26c2516fe7dbab00d6ece472986d21edf Mon Sep 17 00:00:00 2001 From: Nik Ogura Date: Wed, 14 Dec 2022 17:00:03 -0800 Subject: [PATCH] modified HandleExtras so collection is optional fixed bug in signature collection --- cmd/build.go | 9 +----- cmd/publish.go | 15 ++-------- cmd/sign.go | 6 +--- cmd/test.go | 4 --- pkg/gomason/golang.go | 4 +-- pkg/gomason/golang_test.go | 2 +- pkg/gomason/gomason.go | 56 ++++++++++++++++++++++++++++---------- pkg/gomason/signing.go | 9 ++++-- 8 files changed, 57 insertions(+), 48 deletions(-) diff --git a/cmd/build.go b/cmd/build.go index f61eb2a..c0eae34 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -15,7 +15,6 @@ package cmd import ( - "fmt" "github.com/nikogura/gomason/pkg/gomason" "github.com/spf13/cobra" "io/ioutil" @@ -53,8 +52,6 @@ Binaries are dropped into the current working directory. log.Fatalf("Failed to create temp dir: %s", err) } - log.Printf("[DEBUG] Created temp dir %s", rootWorkDir) - defer os.RemoveAll(rootWorkDir) meta, err := gomason.ReadMetadata(gomason.METADATA_FILENAME) @@ -87,8 +84,6 @@ Binaries are dropped into the current working directory. if err != nil { log.Fatalf("error running go test: %s", err) } - - fmt.Print("Tests Succeeded!\n\n") } err = lang.Build(workDir, meta, buildSkipTargets) @@ -96,14 +91,12 @@ Binaries are dropped into the current working directory. log.Fatalf("build failed: %s", err) } - fmt.Print("Build Succeeded!\n\n") - err = gm.HandleArtifacts(meta, workDir, cwd, false, false, true, buildSkipTargets) if err != nil { log.Fatalf("signing failed: %s", err) } - err = gm.HandleExtras(meta, workDir, cwd, false, false) + err = gm.HandleExtras(meta, workDir, cwd, false, false, true) if err != nil { log.Fatalf("Extra artifact processing failed: %s", err) } diff --git a/cmd/publish.go b/cmd/publish.go index acd054f..8370288 100644 --- a/cmd/publish.go +++ b/cmd/publish.go @@ -15,8 +15,6 @@ package cmd import ( - "fmt" - "io/ioutil" "log" "os" @@ -46,14 +44,12 @@ Publish will upload your binaries to wherever it is you've configured them to go if err != nil { log.Fatalf("Failed to get current working directory: %s", err) } - rootWorkDir, err := ioutil.TempDir("", "gomason") + rootWorkDir, err := os.MkdirTemp("", "gomason") if err != nil { log.Fatalf("Failed to create temp dir: %s", err) } defer os.RemoveAll(rootWorkDir) - log.Printf("[DEBUG] Created temp dir %s", rootWorkDir) - meta, err := gomason.ReadMetadata(gomason.METADATA_FILENAME) if err != nil { 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 log.Fatalf("Failed getting current working directory.") } - fmt.Printf("Workdir is %s\n", workDir) - for _, t := range meta.PublishInfo.Targets { if meta.PublishInfo.SkipSigning { err = gm.PublishFile(meta, t.Source) @@ -114,7 +108,6 @@ Publish will upload your binaries to wherever it is you've configured them to go log.Fatalf("error running go test: %s", err) } - fmt.Print("Tests Succeeded!\n\n") } err = lang.Build(workDir, meta, buildSkipTargets) @@ -122,8 +115,6 @@ Publish will upload your binaries to wherever it is you've configured them to go log.Fatalf("build failed: %s", err) } - fmt.Print("Build Succeeded!\n\n") - if meta.PublishInfo.SkipSigning { log.Printf("[DEBUG] Skipping signing due to 'skip-signing': true in metadata file") err = gm.HandleArtifacts(meta, workDir, cwd, false, true, false, buildSkipTargets) @@ -131,7 +122,7 @@ Publish will upload your binaries to wherever it is you've configured them to go log.Fatalf("post-build processing failed: %s", err) } - err = gm.HandleExtras(meta, workDir, cwd, false, true) + err = gm.HandleExtras(meta, workDir, cwd, false, true, false) if err != nil { log.Fatalf("Extra artifact processing failed: %s", err) } @@ -142,7 +133,7 @@ Publish will upload your binaries to wherever it is you've configured them to go log.Fatalf("post-build processing failed: %s", err) } - err = gm.HandleExtras(meta, workDir, cwd, true, true) + err = gm.HandleExtras(meta, workDir, cwd, true, true, false) if err != nil { log.Fatalf("Extra artifact processing failed: %s", err) } diff --git a/cmd/sign.go b/cmd/sign.go index dfdec3c..3250096 100644 --- a/cmd/sign.go +++ b/cmd/sign.go @@ -49,8 +49,6 @@ Signing sorta implies something to sign, which in turn, implies that it built, w log.Fatalf("Failed to create temp dir: %s", err) } - log.Printf("[DEBUG] Created temp dir %s", rootWorkDir) - defer os.RemoveAll(rootWorkDir) 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 log.Fatalf("build failed: %s", err) } - log.Printf("Build Succeeded!\n\n") - err = gm.HandleArtifacts(meta, workDir, cwd, true, false, true, buildSkipTargets) if err != nil { log.Fatalf("signing failed: %s", err) } - err = gm.HandleExtras(meta, workDir, cwd, true, false) + err = gm.HandleExtras(meta, workDir, cwd, true, false, true) if err != nil { log.Fatalf("Extra artifact processing failed: %s", err) } diff --git a/cmd/test.go b/cmd/test.go index c4f0907..e0589b3 100644 --- a/cmd/test.go +++ b/cmd/test.go @@ -15,7 +15,6 @@ package cmd import ( - "fmt" "github.com/nikogura/gomason/pkg/gomason" "github.com/spf13/cobra" "io/ioutil" @@ -51,8 +50,6 @@ Sometimes you need the benefits of a full system here. Now. Right at your fing log.Fatalf("Failed to create temp dir: %s", err) } - log.Printf("[DEBUG] Created temp dir %s", rootWorkDir) - defer os.RemoveAll(rootWorkDir) 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 log.Fatalf("error running go test: %s", err) } - fmt.Print("Success!\n\n") }, } diff --git a/pkg/gomason/golang.go b/pkg/gomason/golang.go index 1e78727..8835921 100644 --- a/pkg/gomason/golang.go +++ b/pkg/gomason/golang.go @@ -248,7 +248,7 @@ func (g Golang) Build(gopath string, meta Metadata, skipTargets string) (err err continue } - logrus.Debugf("Building target: %q", target.Name) + logrus.Debugf("Building target: %q in dir %s", target.Name, wd) // This gets weird because go's exec shell doesn't like the arg format that gox expects // 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 args := gox + cgo + ldflags + ` -osarch="` + target.Name + `"` + " ./..." - logrus.Debugf("Running gox with: %s", args) + logrus.Debugf("Running gox with: %s in dir %s", args, wd) // Calling it through sh makes everything happy cmd := exec.Command("sh", "-c", args) diff --git a/pkg/gomason/golang_test.go b/pkg/gomason/golang_test.go index 488abb0..386c22e 100644 --- a/pkg/gomason/golang_test.go +++ b/pkg/gomason/golang_test.go @@ -343,7 +343,7 @@ Expire-Date: 0 t.Errorf("post-build processing failed: %s", err) } - err = g.HandleExtras(meta, gopath, cwd, false, true) + err = g.HandleExtras(meta, gopath, cwd, false, true, true) if err != nil { t.Errorf("Extra artifact processing failed: %s", err) } diff --git a/pkg/gomason/gomason.go b/pkg/gomason/gomason.go index 54d5ed6..3a9aafc 100644 --- a/pkg/gomason/gomason.go +++ b/pkg/gomason/gomason.go @@ -8,6 +8,7 @@ import ( "path/filepath" "regexp" "strings" + "time" "github.com/pkg/errors" "gopkg.in/ini.v1" @@ -18,7 +19,7 @@ func init() { } // VERSION is the current gomason version -const VERSION = "2.12.0" +const VERSION = "2.12.1" // METADATA_FILENAME The default gomason metadata file name const METADATA_FILENAME = "metadata.json" @@ -181,7 +182,7 @@ func (g *Gomason) HandleArtifacts(meta Metadata, gopath string, cwd string, sign return err } - targetSuffix := fmt.Sprintf(".+_%s_%s", osname, archname) + targetSuffix := fmt.Sprintf(".+_%s_%s$", osname, archname) targetRegex := regexp.MustCompile(targetSuffix) for _, file := range files { @@ -190,15 +191,16 @@ func (g *Gomason) HandleArtifacts(meta Metadata, gopath string, cwd string, sign if matched { filename := fmt.Sprintf("%s/%s", workdir, file.Name()) - logrus.Debugf("\tHandling %s\n", filename) + logrus.Debugf("Handling %s", filename) if _, err := os.Stat(filename); os.IsNotExist(err) { - err = errors.Wrapf(err, "failed to build binary: %s\n", filename) + err = errors.Wrapf(err, "failed building binary: %s\n", filename) return err } // sign 'em if we're signing if sign { + logrus.Debugf("Signing %s", filename) err = g.SignBinary(meta, filename) if err != nil { err = errors.Wrapf(err, "failed to sign binary %s", filename) @@ -208,6 +210,7 @@ func (g *Gomason) HandleArtifacts(meta Metadata, gopath string, cwd string, sign // publish and return if we're publishing if publish { + logrus.Debugf("Publishing %s", filename) err = g.PublishFile(meta, filename) if err != nil { err = errors.Wrap(err, "failed to publish binary") @@ -215,8 +218,9 @@ func (g *Gomason) HandleArtifacts(meta Metadata, gopath string, cwd string, sign } } + // Collect up the stuff we built, and dump 'em into the cwd where we called gomason if collect { - // if we're not publishing, collect up the stuff we built, and dump 'em into the cwd where we called gomason + logrus.Debugf("Collecting %s", filename) err := CollectFileAndSignature(cwd, filename) if err != nil { err = errors.Wrap(err, "failed to collect binaries") @@ -234,7 +238,7 @@ func (g *Gomason) HandleArtifacts(meta Metadata, gopath string, cwd string, sign // HandleExtras loops over the expected files built by Build() and optionally signs them and publishes them along with their signatures (if signing). // // 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). -func (g *Gomason) HandleExtras(meta Metadata, gopath string, cwd string, sign bool, publish bool) (err error) { +func (g *Gomason) HandleExtras(meta Metadata, gopath string, cwd string, sign bool, publish bool, collect bool) (err error) { // loop through the built things for each type of build target for _, extra := range meta.BuildInfo.Extras { @@ -250,6 +254,7 @@ func (g *Gomason) HandleExtras(meta Metadata, gopath string, cwd string, sign bo // sign 'em if we're signing if sign { + logrus.Debugf("Signing %s", filename) err = g.SignBinary(meta, filename) if err != nil { 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 // publish and return if we're publishing if publish { + logrus.Debugf("Publishing %s", filename) err = g.PublishFile(meta, filename) if err != nil { err = errors.Wrapf(err, "failed to publish extra artifact %s", filename) return err } - } else { - // if we're not publishing, collect up the stuff we built, and dump 'em into the cwd where we called gomason + } + + if collect { + // Collect up the stuff we built, and dump 'em into the cwd where we called gomason + logrus.Debugf("Collecting %s", filename) err := CollectFileAndSignature(cwd, filename) if err != nil { err = errors.Wrapf(err, "failed to collect binary %s", filename) @@ -278,9 +287,22 @@ func (g *Gomason) HandleExtras(meta Metadata, gopath string, cwd string, sign bo return err } +func DebugPrint(filename, tag string) { + ts := time.Now().UnixNano() + cwd, _ := os.Getwd() + logrus.Debugf("%s Current working directory: %s\n", tag, cwd) + + _, err := os.Stat(filename) + if err != nil { + logrus.Debugf("%s File %s missing at %v", tag, filename, ts) + } else { + logrus.Debugf("%s File %s exists at %v", tag, filename, ts) + } +} + // 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. func CollectFileAndSignature(cwd string, filename string) (err error) { - fmt.Printf("[DEBUG] Collecting Binaries and Signatures (if signing)\n") + logrus.Debugf("Collecting Files and Signatures") binaryDestinationPath := fmt.Sprintf("%s/%s", cwd, filepath.Base(filename)) @@ -288,6 +310,7 @@ func CollectFileAndSignature(cwd string, filename string) (err error) { fileInfo, err := os.Stat(filename) if err != nil { err = errors.Wrapf(err, "failed statting file %s", filename) + return err } contents, err := os.ReadFile(filename) @@ -312,21 +335,26 @@ func CollectFileAndSignature(cwd string, filename string) (err error) { if _, err := os.Stat(sigName); !os.IsNotExist(err) { signatureDestinationPath := fmt.Sprintf("%s/%s", cwd, sigName) if signatureDestinationPath != sigName { - contents, err := os.ReadFile(filename) + fileInfo, err := os.Stat(sigName) + if err != nil { + err = errors.Wrapf(err, "failed statting file %s", sigName) + return err + } + contents, err := os.ReadFile(sigName) if err != nil { - err = errors.Wrapf(err, "failed reading file %q", filename) + err = errors.Wrapf(err, "failed reading file %q", sigName) return err } - err = os.WriteFile(signatureDestinationPath, contents, 0644) + err = os.WriteFile(signatureDestinationPath, contents, fileInfo.Mode()) if err != nil { err = errors.Wrapf(err, "failed writing file %s", signatureDestinationPath) return err } - err = os.Remove(filename) + err = os.Remove(sigName) if err != nil { - err = errors.Wrapf(err, "failed removing file %s", filename) + err = errors.Wrapf(err, "failed removing file %s", sigName) return err } } diff --git a/pkg/gomason/signing.go b/pkg/gomason/signing.go index bc5bdcd..fa30b62 100644 --- a/pkg/gomason/signing.go +++ b/pkg/gomason/signing.go @@ -14,7 +14,7 @@ const defaultSigningProgram = "gpg" // SignBinary signs the given binary based on the entity and program given in metadata file, possibly overridden by information in ~/.gomason func (g *Gomason) SignBinary(meta Metadata, binary string) (err error) { - logrus.Debugf("Preparing to sign binary %s", binary) + logrus.Debugf("Preparing to sign file %s", binary) // pull signing info out of metadata file signInfo := meta.SignInfo @@ -110,11 +110,16 @@ func SignGPG(binary string, signingEntity string, meta Metadata) (err error) { cmd.Env = os.Environ() - err = cmd.Run() + err = cmd.Start() if err != nil { err = errors.Wrap(err, fmt.Sprintf("failed to run %q", shellCmd)) } + err = cmd.Wait() + if err != nil { + err = errors.Wrap(err, fmt.Sprintf("error waiting for %q to finish", shellCmd)) + } + return err }