(Go version) Multi platform build (#319)

This commit is contained in:
Max Horstmann
2020-11-18 10:42:08 -05:00
committed by GitHub
parent 4807dc527c
commit b71378db82
7 changed files with 104 additions and 11 deletions

View File

@@ -20,8 +20,7 @@ jobs:
with:
go-version: ^1.15
- name: Build binaries
working-directory: cmd/blackbox
run: go build
run: go run build/build.go
- name: Run unit tests
run: go test ./...
- name: Run integration tests

1
.gitignore vendored
View File

@@ -8,7 +8,6 @@ __pycache__/
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
eggs/

75
build/build.go Normal file
View File

@@ -0,0 +1,75 @@
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"strings"
"time"
)
var sha = flag.String("sha", "", "SHA of current commit")
var goos = flag.String("os", "", "OS to build (linux, windows, or darwin) Defaults to all.")
func main() {
flag.Parse()
flags := fmt.Sprintf(`-s -w -X main.SHA="%s" -X main.BuildTime=%d`, getVersion(), time.Now().Unix())
pkg := "github.com/StackExchange/blackbox/v2/cmd/blackbox"
build := func(out, goos string) {
log.Printf("Building %s", out)
cmd := exec.Command("go", "build", "-o", out, "-ldflags", flags, pkg)
os.Setenv("GOOS", goos)
os.Setenv("GO111MODULE", "on")
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
}
for _, env := range []struct {
binary, goos string
}{
{"blackbox-Linux", "linux"},
{"blackbox.exe", "windows"},
{"blackbox-Darwin", "darwin"},
} {
if *goos == "" || *goos == env.goos {
build(env.binary, env.goos)
}
}
}
func getVersion() string {
if *sha != "" {
return *sha
}
// check teamcity build version
if v := os.Getenv("BUILD_VCS_NUMBER"); v != "" {
return v
}
// check git
cmd := exec.Command("git", "rev-parse", "HEAD")
v, err := cmd.CombinedOutput()
if err != nil {
return ""
}
ver := strings.TrimSpace(string(v))
// see if dirty
cmd = exec.Command("git", "diff-index", "--quiet", "HEAD", "--")
err = cmd.Run()
// exit status 1 indicates dirty tree
if err != nil {
if err.Error() == "exit status 1" {
ver += "[dirty]"
} else {
log.Printf("!%s!", err.Error())
}
}
return ver
}

View File

@@ -4,9 +4,10 @@ package main
import (
"fmt"
"syscall"
"github.com/urfave/cli/v2"
"github.com/StackExchange/blackbox/v2/pkg/bbutil"
)
func flags() *cli.App {
@@ -14,8 +15,8 @@ func flags() *cli.App {
app.Version = "2.0.0"
app.Usage = "Maintain encrypted files in a VCS (Git, Hg, Svn)"
defUmask := syscall.Umask(0)
syscall.Umask(defUmask)
defUmask := bbutil.Umask(0)
bbutil.Umask(defUmask)
defUmaskS := fmt.Sprintf("%04o", defUmask)
app.Flags = []cli.Flag{

11
pkg/bbutil/umask_posix.go Normal file
View File

@@ -0,0 +1,11 @@
// +build !windows
package bbutil
import "syscall"
// Umask is a no-op on Windows, and calls syscall.Umask on all other
// systems. On Windows it returns 0, which is a decoy.
func Umask(mask int) int {
return syscall.Umask(mask)
}

View File

@@ -0,0 +1,9 @@
// +build windows
package bbutil
// Umask is a no-op on Windows, and calls syscall.Umask on all other
// systems. On Windows it returns 0, which is a decoy.
func Umask(mask int) int {
return 0o000
}

View File

@@ -7,7 +7,6 @@ import (
"os"
"os/exec"
"path/filepath"
"syscall"
"github.com/StackExchange/blackbox/v2/pkg/bblog"
"github.com/StackExchange/blackbox/v2/pkg/bbutil"
@@ -66,9 +65,9 @@ func (crypt CrypterHandle) Decrypt(filename string, umask int, overwrite bool) e
}
a = append(a, filename+".gpg")
oldumask := syscall.Umask(umask)
oldumask := bbutil.Umask(umask)
err := bbutil.RunBash(crypt.GPGCmd, a...)
syscall.Umask(oldumask)
bbutil.Umask(oldumask)
return err
}
@@ -118,10 +117,10 @@ func (crypt CrypterHandle) Encrypt(filename string, umask int, receivers []strin
a = append(a, filename)
//err = bbutil.RunBash("ls", "-la")
oldumask := syscall.Umask(umask)
oldumask := bbutil.Umask(umask)
crypt.logDebug.Printf("Args = %q", a)
err = bbutil.RunBash(crypt.GPGCmd, a...)
syscall.Umask(oldumask)
bbutil.Umask(oldumask)
return encrypted, err
}