//go:build mage // +build mage package main import ( "fmt" "os" "os/exec" "github.com/magefile/mage/mg" // mg contains helpful utility functions, like Deps ) // Default target to run when none is specified // If not set, running mage will list available targets // var Default = Build // A build step that requires additional params, or platform specific steps for example // Build builds the application for multiple platforms. func Build() error { mg.Deps(InstallDeps) // Define target platforms platforms := []struct { OS string Arch string }{ {"linux", "amd64"}, {"linux", "arm64"}, {"darwin", "amd64"}, {"darwin", "arm64"}, {"windows", "amd64"}, } for _, p := range platforms { fmt.Printf("Building for %s/%s...\n", p.OS, p.Arch) // Set environment variables for cross-compilation env := append(os.Environ(), fmt.Sprintf("GOOS=%s", p.OS), fmt.Sprintf("GOARCH=%s", p.Arch)) // Determine output name outputName := fmt.Sprintf("./bin/zzyxyz_go_api-%s-%s", p.OS, p.Arch) if p.OS == "windows" { outputName += ".exe" } // Run build command cmd := exec.Command("go", "build", "-o", outputName, ".") cmd.Env = env if err := cmd.Run(); err != nil { return err } } return nil } // A custom install step if you need your bin someplace other than go/bin func Install() error { mg.Deps(Build) // fmt.Println("Installing...") // return os.Rename("./MyApp", "/usr/bin/MyApp") return nil } // Manage your deps, or running package managers. func InstallDeps() error { fmt.Println("Installing Deps...") // cmd := exec.Command("go", "get", "github.com/stretchr/piglatin") // return cmd.Run() return nil } // Clean up after yourself func Clean() { fmt.Println("Cleaning...") // os.RemoveAll("MyApp") }