Skip to content
Snippets Groups Projects
Commit 1ce6f2fe authored by Will Rouesnel's avatar Will Rouesnel
Browse files

WIP: conversion to go-only mage build.

parent a52beb60
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -17,16 +17,19 @@ docker run -e DATA_SOURCE_NAME="postgresql://postgres:password@localhost:5432/?s
```
 
## Building and running
The build system is based on [Mage](https://magefile.org)
The default make file behavior is to build the binary:
```
go get github.com/wrouesnel/postgres_exporter
cd ${GOPATH-$HOME/go}/src/github.com/wrouesnel/postgres_exporter
make
export DATA_SOURCE_NAME="postgresql://login:password@hostname:port/dbname"
./postgres_exporter <flags>
$ go get github.com/wrouesnel/postgres_exporter
$ cd ${GOPATH-$HOME/go}/src/github.com/wrouesnel/postgres_exporter
$ go run mage.go
$ export DATA_SOURCE_NAME="postgresql://login:password@hostname:port/dbname"
$ ./postgres_exporter <flags>
```
 
To build the dockerfile, run `make docker`.
To build the dockerfile, run `go run mage.go docker`.
 
This will build the docker image as `wrouesnel/postgres_exporter:latest`. This
is a minimal docker image containing *just* postgres_exporter. By default no SSL
Loading
Loading
@@ -130,9 +133,6 @@ GRANT SELECT ON postgres_exporter.pg_stat_replication TO postgres_exporter;
> ```
 
# Hacking
* The build system is currently only supported for Linux-like platforms. It
depends on GNU Make.
* To build a copy for your current architecture run `make binary` or just `make`
* To build a copy for your current architecture run `go run mage.go binary` or just `go run mage.go`
This will create a symlink to the just built binary in the root directory.
* To build release tar balls run `make release`.
* To build release tar balls run `go run mage.go release`.
mage.go 0 → 100644
// +build ignore
package main
import (
"os"
"github.com/magefile/mage/mage"
)
func main() { os.Exit(mage.Main()) }
\ No newline at end of file
// +build mage
// Self-contained go-project magefile.
package main
import (
"fmt"
"github.com/wrouesnel/go.sysutil/executil"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"runtime"
"strings"
"time"
)
const coverageDir = ".coverage"
const toolDir = "tools"
const binDir = "bin"
const releaseDir = "release"
const cmdDir = "cmd"
var outputDirs = []string{cmdDir, releaseDir}
var curDir = func() string {
name, _ := os.Getwd()
return name
}()
type Platform struct {
OS string
Arch string
BinSuffix string
}
// Supported platforms
var platforms []Platform = []Platform{
Platform{"linux", "amd64", ""},
Platform{"linux", "386", ""},
Platform{"darwin", "amd64", ""},
Platform{"darwin", "386", ""},
Platform{"windows", "amd64", ".exe"},
Platform{"windows", "386", ".exe"},
}
// productName can be overridden by environ product name
var productName = func() string {
if name := os.Getenv("PRODUCT_NAME"); name != "" {
return name
}
name, _ := os.Getwd()
return path.Base(name)
}()
// Source files
var goSrc []string
var goDirs []string
var goPkgs []string
var goCmds []string
var version = func() string {
if v := os.Getenv("VERSION"); v != "" {
return v
}
out, _ := executil.MustExecWithOutput("git", "describe", "--dirty")
if out == "" {
return "v0.0.0"
}
return out
}()
var versionShort = func() string {
if v := os.Getenv("VERSION_SHORT"); v != "" {
return v
}
out, _ := executil.MustExecWithOutput("git", "describe", "--abbrev=0")
if out == "" {
return "v0.0.0"
}
return out
}()
var platformBins []string
var platformDirs []string
var platformTars []string
var currentPlatformBins []string
var concurrentLinters = runtime.NumCPU()
var linterDeadline = func() time.Duration {
if v := os.Getenv("LINTER_DEADLINE"); v != "" {
d, _ := time.ParseDuration(v)
if d != 0 {
return d
}
}
return time.Second * 60
}
func buildTools() {
origGoPath := os.Getenv("GOPATH")
os.Setenv("GOPATH", path.Join(curDir, toolDir))
// TODO: vendor/src misdirect
staticTools := []string{
"github.com/kardianos/govendor",
"github.com/wadey/gocovmerge",
"github.com/mattn/goveralls",
"github.com/alecthomas/gometalinter",
}
// Build static tools first
for _, pkg := range staticTools {
executil.MustExec("go", "install", "-v", pkg)
}
// Gometalinter should now be on the command line
dynamicTools := []string{}
out, _, _ := executil.CheckExecWithOutput("gometalinter", "--help")
linterRx := regexp.MustCompile(`\s+\w+:\s*\((.+)\)`)
for _, l := range strings.Split(out, "\n") {
linter := linterRx.FindStringSubmatch(l)
if len(linter) > 1 {
dynamicTools = append(dynamicTools, linter[1])
}
}
// Build dynamic tools
for _, pkg := range dynamicTools {
executil.MustExec("go", "install", "-v", pkg)
}
// Restore original GOPATH
os.Setenv("GOPATH", origGoPath)
}
func init() {
// Set environment
os.Setenv("PATH", fmt.Sprintf("%s/bin:%s", path.Join(curDir, toolDir), os.Getenv("PATH")))
goSrc = func() []string {
results := new([]string)
filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
// Look for files
if info.IsDir() {
return nil
}
// Exclusions
if matched, _ := filepath.Match("*/vendor/*", path); matched {
return nil
} else if matched, _ := filepath.Match(fmt.Sprintf("%s/%s/*", curDir, toolDir), path); matched {
return nil
} else if matched, _ := filepath.Match(fmt.Sprintf("%s/%s/*", curDir, binDir), path); matched {
return nil
} else if matched, _ := filepath.Match(fmt.Sprintf("%s/%s/*", curDir, releaseDir), path); matched {
return nil
}
if matched, _ := filepath.Match("*.go", path); !matched {
return nil
}
*results = append(*results, path)
return nil
})
return *results
}()
goDirs = func() []string {
resultMap := make(map[string]struct{})
for _, path := range goSrc {
resultMap[filepath.Dir(path)] = struct{}{}
}
results := []string{}
for k, _ := range resultMap {
results = append(results, k)
}
return results
}()
goPkgs = func() []string {
results := []string{}
out, _ := executil.MustExecWithOutput("go", "list", "./...")
for _, line := range strings.Split(out, "\n") {
if !strings.Contains(line, "/vendor/") {
results = append(results, line)
}
}
return results
}()
goCmds = func() []string {
results := []string{}
finfos, _ := ioutil.ReadDir(path.Join(curDir, cmdDir))
for _, finfo := range finfos {
results = append(results, finfo.Name())
}
return results
}()
// Populate platform output types
for _, cmd := range goCmds {
for _, platform := range platforms {
platformTars = append(platformTars, fmt.Sprintf("%s/%s_%s_%s_%s-%s.tar.gz", releaseDir, productName, versionShort, platform.OS, platform.Arch))
platformDirs = append(platformDirs, fmt.Sprintf("%s/%s_%s_%s_%s-%s", binDir, productName, versionShort, platform.OS, platform.Arch))
platformBins = append(platformBins, fmt.Sprintf("%s/%s_%s_%s_%s-%s/%s%s", binDir, productName, versionShort, platform.OS, platform.Arch, cmd, platform.BinSuffix))
if runtime.GOOS == platform.OS && runtime.GOARCH == platform.Arch {
currentPlatformBins = append(currentPlatformBins, fmt.Sprintf("%s/%s_%s_%s_%s-%s/%s%s", binDir, productName, versionShort, platform.OS, platform.Arch, cmd, platform.BinSuffix))
}
}
}
// Ensure output dirs exist
for _, dir := range outputDirs {
os.MkdirAll(dir, os.FileMode(0777))
}
buildTools()
}
func Tools() {
buildTools()
}
func Assets() {
}
func Style() {
}
func Lint() {
}
func Fmt() {
}
func Test() {
}
func Clean() {
}
func Autogen() error {
fmt.Println("Installing git hooks in local repository...")
return os.Link(path.Join(curDir, toolDir, "pre-commit"), ".git/hooks/pre-commit")
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment