pelican/magefiles/mage.go
Dan Anglin 5189ebe7bb
All checks were successful
/ test (pull_request) Successful in 40s
/ lint (pull_request) Successful in 42s
refactor: remove duplicate write function in db
Remove the original 'Write' function from the db package and rename the
'WriteMany' function to 'Write' as it can already write one or many Bolt
items to the database.

Also update the test suites and add more coverage in the board package.
2024-01-23 18:31:01 +00:00

157 lines
3.2 KiB
Go

//go:build mage
package main
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
const (
binary = "pelican"
defaultInstallPrefix = "/usr/local"
)
var Default = Build
// Test run the go tests
// To enable verbose mode set PELICAN_TEST_VERBOSE=1.
// To enable coverage mode set PELICAN_TEST_COVER=1.
// To produce a coverage report set PELICAN_TEST_COVER_REPORT=1.
func Test() error {
test := sh.RunCmd("go", "test")
args := []string{"./..."}
if os.Getenv("PELICAN_TEST_VERBOSE") == "1" {
args = append(args, "-v")
}
if os.Getenv("PELICAN_TEST_COVER") == "1" {
args = append(args, "-cover")
}
if os.Getenv("PELICAN_TEST_COVER_REPORT") == "1" {
args = append(args, "-coverprofile=cover.out")
}
if err := test(args...); err != nil {
return err
}
if os.Getenv("PELICAN_TEST_COVER_REPORT") == "1" {
return sh.Run("go", "tool", "cover", "-html=cover.out", "-o", "code-coverage.html")
}
return nil
}
// Lint runs golangci-lint against the code.
func Lint() error {
return sh.RunV("golangci-lint", "run", "--color", "always")
}
// Build build the executable
// To rebuild packages that are already up-to-date set PELICAN_BUILD_REBUILD_ALL=1
// To enable verbose mode set PELICAN_BUILD_VERBOSE=1
func Build() error {
main := "./cmd/" + binary
flags := ldflags()
build := sh.RunCmd("go", "build")
args := []string{"-ldflags=" + flags, "-o", binary}
if os.Getenv("PELICAN_BUILD_REBUILD_ALL") == "1" {
args = append(args, "-a")
}
if os.Getenv("PELICAN_BUILD_VERBOSE") == "1" {
args = append(args, "-v")
}
args = append(args, main)
return build(args...)
}
func Install() error {
mg.Deps(Build)
installPrefix := os.Getenv("PELICAN_INSTALL_PREFIX")
if installPrefix == "" {
installPrefix = defaultInstallPrefix
}
dest := filepath.Join(installPrefix, "bin", binary)
if err := sh.Copy(dest, binary); err != nil {
return fmt.Errorf("unable to install %s; %w", dest, err)
}
fmt.Printf("%s successfully installed to %s\n", binary, dest)
return nil
}
// Clean clean the workspace
func Clean() error {
if err := sh.Rm(binary); err != nil {
return err
}
if err := sh.Run("go", "clean", "./..."); err != nil {
return err
}
testDBDir := "./test/databases"
files, err := os.ReadDir(testDBDir)
if err != nil {
return err
}
for _, f := range files {
filename := f.Name()
if strings.HasSuffix(filename, ".db") {
sh.Rm(testDBDir + "/" + filename)
}
}
return nil
}
// ldflags returns the build flags.
func ldflags() string {
ldflagsfmt := "-s -w -X main.binaryVersion=%s -X main.gitCommit=%s -X main.goVersion=%s -X main.buildTime=%s"
buildTime := time.Now().UTC().Format(time.RFC3339)
return fmt.Sprintf(ldflagsfmt, version(), gitCommit(), runtime.Version(), buildTime)
}
// version returns the latest git tag using git describe.
func version() string {
version, err := sh.Output("git", "describe", "--tags")
if err != nil {
version = "N/A"
}
return version
}
// gitCommit returns the current git commit
func gitCommit() string {
commit, err := sh.Output("git", "rev-parse", "--short", "HEAD")
if err != nil {
commit = "N/A"
}
return commit
}