This repository has been archived by the owner on Sep 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expand mage and add build options (#73)
* trial * change * add pkg * update * update * work on mage * work on mage * work on ci * work on ci * fix * remove * notice * forbidigo * forbidigo * sort imports * lint * lint * work on build * undo installer * work on version * unitTest * test binaries * remove local * rename * remore extra logging * review * add flags * version update * version update * version fix * lint * add binary check * add binary check * remove * rename env
- Loading branch information
Showing
13 changed files
with
732 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,5 @@ fleet.yml | |
# agent | ||
build/ | ||
elastic-agent-shipper | ||
dist/ | ||
|
||
|
||
# VSCode | ||
/.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Make sure to check the documentation at https://goreleaser.com | ||
before: | ||
hooks: | ||
- go mod tidy | ||
- go generate ./... | ||
builds: | ||
- id: darwin | ||
binary: '{{ .ProjectName }}-{{ .Env.DEFAULT_VERSION }}-{{ .Os }}-{{ if eq .Arch "amd64" }}x86_64{{ end }}{{ if eq .Arch "arm64" }}aarch64{{ end }}/{{ .ProjectName }}' | ||
goos: | ||
- darwin | ||
goarch: | ||
- amd64 | ||
- arm64 | ||
flags: | ||
- -buildmode=pie | ||
ldflags: | ||
- '{{ if eq .Env.DEV "false" }}-s -w{{ end }}' | ||
- -X main.Version={{ .Env.DEFAULT_VERSION }} -X main.Commit={{.Commit}} -X main.BuildTime={{.Date}} | ||
gcflags: | ||
- '{{ if eq .Env.DEV "true" }}all=-N -l{{ end }}' | ||
no_unique_dist_dir: true | ||
- id: linux | ||
binary: '{{ .ProjectName }}-{{ .Env.DEFAULT_VERSION }}-{{ .Os }}-{{ if eq .Arch "amd64" }}x86_64{{ end }}{{ if eq .Arch "386" }}x86{{ end }}{{ if eq .Arch "arm64" }}{{ .Arch }}{{ end }}/{{ .ProjectName }}' | ||
goos: | ||
- linux | ||
goarch: | ||
- amd64 | ||
- arm64 | ||
- 386 | ||
no_unique_dist_dir: true | ||
ldflags: | ||
- '{{ if eq .Env.DEV "false" }}-s -w{{end}}' | ||
- -X main.Version={{ .Env.DEFAULT_VERSION }} -X main.Commit={{.Commit}} -X main.BuildTime={{.Date}} | ||
gcflags: | ||
- '{{ if eq .Env.DEV "true" }}all=-N -l{{ end }}' | ||
overrides: | ||
- goos: linux | ||
goarch: amd64 | ||
goarm: '' | ||
gomips: '' | ||
flags: | ||
- -buildmode=pie | ||
- goos: linux | ||
goarch: arm64 | ||
goarm: '' | ||
gomips: '' | ||
flags: | ||
- -buildmode=pie | ||
- id: windows | ||
binary: '{{ .ProjectName }}-{{ .Env.DEFAULT_VERSION }}-{{ .Os }}-{{ if eq .Arch "amd64" }}x86_64{{ end }}{{ if eq .Arch "386" }}x86{{ end }}/{{ .ProjectName }}' | ||
goos: | ||
- windows | ||
goarch: | ||
- amd64 | ||
- 386 | ||
no_unique_dist_dir: true | ||
flags: | ||
- -buildmode=pie | ||
ldflags: | ||
- '{{ if eq .Env.DEV "false" }}-s -w{{ end }}' | ||
- -X main.Version={{ .Env.DEFAULT_VERSION }} -X main.Commit={{.Commit}} -X main.BuildTime={{.Date}} | ||
gcflags: | ||
- '{{ if eq .Env.DEV "true" }}all=-N -l{{ end }}' | ||
changelog: | ||
skip: true | ||
dist: build/binaries |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package common | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/magefile/mage/mg" | ||
"github.com/pkg/errors" | ||
|
||
"github.com/elastic/elastic-agent-libs/dev-tools/mage/gotool" | ||
) | ||
|
||
const ProjectName = "elastic-agent-shipper" | ||
|
||
var PlatformFiles = map[string][]string{ | ||
"windows": {"windows-x86_64", "windows-x86"}, | ||
"linux": {"linux-arm64", "linux-x86_64", "linux-x86"}, | ||
"darwin": {"darwin-aarch64", "darwin-x86_64"}, | ||
"darwin/amd64": {"darwin-x86_64"}, | ||
"darwin/arm64": {"darwin-aarch64"}, | ||
"linux/386": {"linux-x86"}, | ||
"linux/amd64": {"linux-x86_64"}, | ||
"linux/arm64": {"linux-arm64"}, | ||
"windows/386": {"windows-x86"}, | ||
"windows/amd64": {"windows-x86_64"}, | ||
"all": {"darwin-aarch64", "darwin-x86_64", "linux-arm64", "linux-x86_64", "linux-x86", "windows-x86_64", "windows-x86"}, | ||
} | ||
|
||
// CreateDir creates the parent directory for the given file. | ||
func CreateDir(file string) string { | ||
// Create the output directory. | ||
if dir := filepath.Dir(file); dir != "." { | ||
if err := os.MkdirAll(dir, 0755); err != nil { | ||
panic(errors.Wrapf(err, "failed to create parent dir for %v", file)) | ||
} | ||
} | ||
return file | ||
} | ||
|
||
func InstallGoTestTools() { | ||
gotool.Install(gotool.Install.Package("gotest.tools/gotestsum")) //nolint:errcheck //not required | ||
} | ||
|
||
func MakeCommand(ctx context.Context, env map[string]string, cmd string, args ...string) *exec.Cmd { | ||
c := exec.CommandContext(ctx, cmd, args...) | ||
c.Env = os.Environ() | ||
for k, v := range env { | ||
c.Env = append(c.Env, k+"="+v) | ||
} | ||
c.Stdout = ioutil.Discard | ||
if mg.Verbose() { | ||
c.Stdout = os.Stdout | ||
} | ||
c.Stderr = os.Stderr | ||
c.Stdin = os.Stdin | ||
fmt.Println("exec:", cmd, strings.Join(args, " ")) //nolint:forbidigo // just for mage | ||
return c | ||
} | ||
|
||
func EnvOrDefault(buildEnv string, defaultValue string) string { | ||
if val := os.Getenv(buildEnv); val != "" { | ||
boolVal, err := strconv.ParseBool(val) | ||
if err != nil { | ||
return defaultValue | ||
} | ||
return strconv.FormatBool(boolVal) | ||
} | ||
return defaultValue | ||
} | ||
|
||
func BoolEnvOrFalse(buildEnv string) bool { | ||
if val := os.Getenv(buildEnv); val != "" { | ||
boolVal, err := strconv.ParseBool(val) | ||
if err != nil { | ||
return false | ||
} | ||
return boolVal | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package common | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"path/filepath" | ||
"strings" | ||
|
||
"io" | ||
"os" | ||
"os/exec" | ||
|
||
"github.com/magefile/mage/mg" | ||
"github.com/magefile/mage/sh" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func GoUnitTest(ctx context.Context, testCoverage bool) error { | ||
mg.Deps(InstallGoTestTools) | ||
|
||
fmt.Println(">> go test:", "Unit Testing") //nolint:forbidigo // just for tests | ||
var testArgs []string | ||
|
||
gotestsumArgs := []string{"--no-color"} | ||
if mg.Verbose() { | ||
gotestsumArgs = append(gotestsumArgs, "-f", "standard-verbose") | ||
} else { | ||
gotestsumArgs = append(gotestsumArgs, "-f", "standard-quiet") | ||
} | ||
//create report files | ||
fileName := filepath.Join("build", "TEST-go-unit") | ||
CreateDir(fileName + ".xml") | ||
gotestsumArgs = append(gotestsumArgs, "--junitfile", fileName+".xml") | ||
CreateDir(fileName + ".out") | ||
gotestsumArgs = append(gotestsumArgs, "--jsonfile", fileName+".out"+".json") | ||
|
||
covFile := fileName + ".cov" | ||
if testCoverage { | ||
CreateDir(covFile) | ||
covFile = CreateDir(filepath.Clean(covFile)) | ||
testArgs = append(testArgs, | ||
"-covermode=atomic", | ||
"-coverprofile="+covFile, | ||
) | ||
} | ||
|
||
testArgs = append(testArgs, []string{"./..."}...) | ||
|
||
args := append(gotestsumArgs, append([]string{"--"}, testArgs...)...) | ||
|
||
goTest := MakeCommand(ctx, map[string]string{}, "gotestsum", args...) | ||
// Wire up the outputs. | ||
var outputs []io.Writer | ||
fileOutput, err := os.Create(CreateDir(fileName + ".out")) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to create go test output file") | ||
} | ||
defer fileOutput.Close() | ||
outputs = append(outputs, fileOutput) | ||
|
||
output := io.MultiWriter(outputs...) | ||
goTest.Stdout = io.MultiWriter(output, os.Stdout) | ||
goTest.Stderr = io.MultiWriter(output, os.Stderr) | ||
err = goTest.Run() | ||
|
||
var goTestErr *exec.ExitError | ||
if err != nil { | ||
// Command ran. | ||
var exitErr *exec.ExitError | ||
if !errors.As(err, &exitErr) { | ||
return errors.Wrap(err, "failed to execute go") | ||
} | ||
// Command ran but failed. Process the output. | ||
goTestErr = exitErr | ||
} | ||
|
||
// Generate a HTML code coverage report. | ||
var htmlCoverReport string | ||
if testCoverage { | ||
htmlCoverReport = strings.TrimSuffix(covFile, | ||
filepath.Ext(covFile)) + ".html" | ||
coverToHTML := sh.RunCmd("go", "tool", "cover", | ||
"-html="+covFile, | ||
"-o", htmlCoverReport) | ||
if err = coverToHTML(); err != nil { | ||
return errors.Wrap(err, "failed to write HTML code coverage report") | ||
} | ||
} | ||
|
||
// Generate an XML code coverage report. | ||
var codecovReport string | ||
if testCoverage { | ||
fmt.Println(">> go run gocover-cobertura:", covFile, "Started") //nolint:forbidigo // just for tests | ||
|
||
// execute gocover-cobertura in order to create cobertura report | ||
// install pre-requisites | ||
installCobertura := sh.RunCmd("go", "install", "github.com/boumenot/gocover-cobertura@latest") | ||
if err = installCobertura(); err != nil { | ||
return errors.Wrap(err, "failed to install gocover-cobertura") | ||
} | ||
|
||
codecovReport = strings.TrimSuffix(covFile, | ||
filepath.Ext(covFile)) + "-cov.xml" | ||
|
||
coverage, err := ioutil.ReadFile(covFile) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to read code coverage report") | ||
} | ||
|
||
coberturaFile, err := os.Create(codecovReport) | ||
if err != nil { | ||
return err | ||
} | ||
defer coberturaFile.Close() | ||
|
||
coverToXML := exec.Command("gocover-cobertura") | ||
coverToXML.Stdout = coberturaFile | ||
coverToXML.Stderr = os.Stderr | ||
coverToXML.Stdin = bytes.NewReader(coverage) | ||
if err = coverToXML.Run(); err != nil { | ||
return errors.Wrap(err, "failed to write XML code coverage report") | ||
} | ||
fmt.Println(">> go run gocover-cobertura:", covFile, "Created") //nolint:forbidigo // just for tests | ||
} | ||
// Return an error indicating that testing failed. | ||
if goTestErr != nil { | ||
fmt.Println(">> go test:", "Unit Tests : Test Failed") //nolint:forbidigo // just for tests | ||
return errors.Wrap(goTestErr, "go test returned a non-zero value") | ||
} | ||
|
||
fmt.Println(">> go test:", "Unit Tests : Test Passed") //nolint:forbidigo // just for tests | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.