Skip to content

Commit

Permalink
modified HandleExtras so collection is optional
Browse files Browse the repository at this point in the history
fixed bug in signature collection
  • Loading branch information
nikogura committed Dec 15, 2022
1 parent 316a595 commit d0c605f
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 48 deletions.
9 changes: 1 addition & 8 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package cmd

import (
"fmt"
"github.com/nikogura/gomason/pkg/gomason"
"github.com/spf13/cobra"
"io/ioutil"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -87,23 +84,19 @@ 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)
if err != nil {
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)
}
Expand Down
15 changes: 3 additions & 12 deletions cmd/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package cmd

import (
"fmt"
"io/ioutil"
"log"
"os"

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -114,24 +108,21 @@ 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)
if err != nil {
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)
if err != nil {
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)
}
Expand All @@ -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)
}
Expand Down
6 changes: 1 addition & 5 deletions cmd/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 0 additions & 4 deletions cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package cmd

import (
"fmt"
"github.com/nikogura/gomason/pkg/gomason"
"github.com/spf13/cobra"
"io/ioutil"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
},
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/gomason/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/gomason/golang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
56 changes: 42 additions & 14 deletions pkg/gomason/gomason.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"regexp"
"strings"
"time"

"github.com/pkg/errors"
"gopkg.in/ini.v1"
Expand All @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -208,15 +210,17 @@ 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")
return err
}
}

// 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")
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -278,16 +287,30 @@ 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))

if binaryDestinationPath != filename {
fileInfo, err := os.Stat(filename)
if err != nil {
err = errors.Wrapf(err, "failed statting file %s", filename)
return err
}

contents, err := os.ReadFile(filename)
Expand All @@ -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
}
}
Expand Down
9 changes: 7 additions & 2 deletions pkg/gomason/signing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit d0c605f

Please sign in to comment.