Skip to content

Commit

Permalink
Add golang sub package
Browse files Browse the repository at this point in the history
  • Loading branch information
heppu committed Nov 16, 2023
1 parent 5e07f96 commit 66da2e5
Show file tree
Hide file tree
Showing 12 changed files with 465 additions and 469 deletions.
26 changes: 7 additions & 19 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,36 +38,24 @@ jobs:
- run: mage go:lint
working-directory: ./godemo
# backwards compatibility check
- run: mage ensure
- run: mage go:tidy
working-directory: ./godemo
# backwards compatibility check
- run: mage ensureInSync
working-directory: ./godemo
- run: mage tidy
working-directory: ./godemo
- run: mage tidyAndVerifyNoChanges
- run: mage go:tidyAndVerify
working-directory: ./godemo
- run: mage go:vulnCheck
continue-on-error: true
working-directory: ./godemo
- run: mage go:licenses
working-directory: ./godemo
- run: mage unittest
working-directory: ./godemo
- run: mage integrationtest
working-directory: ./godemo
- run: mage mergecoverprofiles
working-directory: ./godemo
- run: mage build
- run: mage go:unitTest
working-directory: ./godemo
- run: mage buildforlinux
- run: mage go:integrationTest
working-directory: ./godemo
- run: mage buildformac
- run: mage go:coverProfile
working-directory: ./godemo
- run: mage buildforarmmac
- run: mage go:build
working-directory: ./godemo
- run: mage buildforwindows
- run: mage go:crossBuild
working-directory: ./godemo
- run: mage docker:build
working-directory: ./godemo
Expand Down
26 changes: 7 additions & 19 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,36 +40,24 @@ jobs:
- run: mage go:lint
working-directory: ./godemo
# backwards compatibility check
- run: mage ensure
- run: mage go:tidy
working-directory: ./godemo
# backwards compatibility check
- run: mage ensureInSync
working-directory: ./godemo
- run: mage tidy
working-directory: ./godemo
- run: mage tidyAndVerifyNoChanges
- run: mage go:tidyAndVerify
working-directory: ./godemo
- run: mage go:vulnCheck
continue-on-error: true
working-directory: ./godemo
- run: mage go:licenses
working-directory: ./godemo
- run: mage unittest
working-directory: ./godemo
- run: mage integrationtest
working-directory: ./godemo
- run: mage mergecoverprofiles
working-directory: ./godemo
- run: mage build
- run: mage go:unitTest
working-directory: ./godemo
- run: mage buildforlinux
- run: mage go:integrationTest
working-directory: ./godemo
- run: mage buildformac
- run: mage go:coverProfile
working-directory: ./godemo
- run: mage buildforarmmac
- run: mage go:build
working-directory: ./godemo
- run: mage buildforwindows
- run: mage go:crossBuild
working-directory: ./godemo
- run: mage docker:build
working-directory: ./godemo
Expand Down
48 changes: 28 additions & 20 deletions go.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"log"
"os"
"path"
"strings"

"github.com/elisasre/mageutil/golang"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
Expand All @@ -33,17 +33,19 @@ type BuildInfo struct {
}

// Go is shorthand for go executable provided by system.
// Deprecated: use sub package.
func Go(ctx context.Context, args ...string) error {
return GoWith(ctx, nil, args...)
return golang.Go(ctx, args...)
}

// GoWith is shorthand for go executable provided by system.
// Deprecated: use sub package.
func GoWith(ctx context.Context, env map[string]string, args ...string) error {
fmt.Printf("env: %s\n", env)
return sh.RunWithV(env, "go", args...)
return golang.GoWith(ctx, env, args...)
}

// Targets returns list of main pkgs under CmdDir.
// Deprecated: use sub package.
func Targets(ctx context.Context) ([]string, error) {
entries, err := os.ReadDir(CmdDir)
if err != nil {
Expand All @@ -60,6 +62,7 @@ func Targets(ctx context.Context) ([]string, error) {
}

// BuildAll binaries for targets returned by utils.Targets using utils.Build.
// Deprecated: use sub package.
func BuildAll(ctx context.Context) error {
targets, err := Targets(ctx)
if err != nil {
Expand All @@ -74,12 +77,14 @@ func BuildAll(ctx context.Context) error {
}

// Build binary using settings from system env.
// Deprecated: use sub package.
func Build(ctx context.Context, name string) error {
_, err := BuildWithInfo(ctx, name)
return err
}

// BuildWithInfo builds binary using settings from system env and returns additional build information.
// Deprecated: use sub package.
func BuildWithInfo(ctx context.Context, name string) (BuildInfo, error) {
goos, err := sh.Output("go", "env", "GOOS")
if err != nil {
Expand All @@ -95,6 +100,7 @@ func BuildWithInfo(ctx context.Context, name string) (BuildInfo, error) {
}

// BuildDefault binary and SHA256 sum using settings from system env.
// Deprecated: use sub package.
func BuildWithSHA(ctx context.Context, goos, goarch, name string) {
mg.CtxDeps(ctx, func() error {
_, err := BuildWithSHAWithInfo(ctx, goos, goarch, name)
Expand All @@ -103,6 +109,7 @@ func BuildWithSHA(ctx context.Context, goos, goarch, name string) {
}

// BuildDefault binary and SHA256 sum using settings from system env
// Deprecated: use sub package.
func BuildWithSHAWithInfo(ctx context.Context, goos, goarch, name string) (BuildInfo, error) {
info, err := BuildForWithInfo(ctx, goos, goarch, name)
if err != nil {
Expand All @@ -113,13 +120,15 @@ func BuildWithSHAWithInfo(ctx context.Context, goos, goarch, name string) (Build
}

// BuildFor builds binary for wanted architecture using default directory schema for output path.
// Deprecated: use sub package.
func BuildFor(ctx context.Context, goos, goarch, name string) error {
_, err := BuildForWithInfo(ctx, goos, goarch, name)
return err
}

// BuildForWithInfo builds binary for wanted architecture using default directory schema for output path
// and returns additional build information.
// Deprecated: use sub package.
func BuildForWithInfo(ctx context.Context, goos, goarch, name string) (BuildInfo, error) {
cmdPath := CmdDir + name
binaryPath := path.Join(TargetDir, "bin", goos, goarch, name)
Expand All @@ -133,11 +142,13 @@ func BuildForWithInfo(ctx context.Context, goos, goarch, name string) (BuildInfo
}

// BuildForLinux builds binary for amd64 based linux systems.
// Deprecated: use sub package.
func BuildForLinux(ctx context.Context, name string) {
BuildWithSHA(ctx, "linux", "amd64", name)
}

// BuildForLinuxWithInfo builds binary for amd64 based linux systems and returns additional build information.
// Deprecated: use sub package.
func BuildForLinuxWithInfo(ctx context.Context, name string) (BuildInfo, error) {
return BuildWithSHAWithInfo(ctx, "linux", "amd64", name)
}
Expand All @@ -148,6 +159,7 @@ func BuildForMac(ctx context.Context, name string) {
}

// BuildForMacWithInfo builds binary for amd64 based Mac systems and returns additional build information.
// Deprecated: use sub package.
func BuildForMacWithInfo(ctx context.Context, name string) (BuildInfo, error) {
return BuildWithSHAWithInfo(ctx, "darwin", "amd64", name)
}
Expand All @@ -158,6 +170,7 @@ func BuildForArmMac(ctx context.Context, name string) {
}

// BuildForArmMacWithInfo builds binary for amd64 based Mac systems and returns additional build information.
// Deprecated: use sub package.
func BuildForArmMacWithInfo(ctx context.Context, name string) (BuildInfo, error) {
return BuildWithSHAWithInfo(ctx, "darwin", "arm64", name)
}
Expand All @@ -168,11 +181,13 @@ func BuildForWindows(ctx context.Context, name string) {
}

// BuildForWindowsWithInfo builds binary for amd64 based Windows systems and returns additional build information.
// Deprecated: use sub package.
func BuildForWindowsWithInfo(ctx context.Context, name string) (BuildInfo, error) {
return BuildWithSHAWithInfo(ctx, "windows", "amd64", name)
}

// Run executes app binary from default path.
// Deprecated: use sub package.
func Run(ctx context.Context, name string, args ...string) error {
bd, err := BinDir()
if err != nil {
Expand All @@ -184,16 +199,13 @@ func Run(ctx context.Context, name string, args ...string) error {
}

// GoList lists all packages in given target.
// Deprecated: use sub package.
func GoList(ctx context.Context, target string) ([]string, error) {
pkgsRaw, err := sh.Output("go", "list", target)
if err != nil {
return nil, err
}
pkgs := strings.Split(strings.ReplaceAll(pkgsRaw, "\r\n", ","), "\n")
return pkgs, nil
return golang.ListPackages(ctx, target)
}

// BinDir returns path in format of target/bin/{GOOS}/{GOARCH}
// Deprecated: use sub package.
func BinDir() (string, error) {
goos, err := sh.Output("go", "env", "GOOS")
if err != nil {
Expand All @@ -212,30 +224,26 @@ func BinDir() (string, error) {
// Deprecated: use Tidy instead
func Ensure(ctx context.Context) error {
log.Println("WARNING: Ensure is deprecated, use Tidy instead")
return Tidy(ctx)
return golang.Tidy(ctx)
}

// EnsureInSync checks that all dependencies are up to date
// useful in CI/CD pipelines to validate that dependencies match go.mod
// Deprecated: use TidyAndVerifyNoChanges instead
func EnsureInSync(ctx context.Context) error {
log.Println("WARNING: EnsureInSync is deprecated, use TidyAndVerifyNoChanges instead")
return TidyAndVerifyNoChanges(ctx)
return golang.TidyAndVerify(ctx)
}

// Tidy runs go mod tidy
// Deprecated: use sub package.
func Tidy(ctx context.Context) error {
return Go(ctx, "mod", "tidy")
return golang.Tidy(ctx)
}

// TidyAndVerifyNoChanges runs go mod tidy and verifies that there are no changes to go.mod or go.sum
// useful in CI/CD pipelines to validate that dependencies match go.mod
// Deprecated: use sub package.
func TidyAndVerifyNoChanges(ctx context.Context) error {
if err := Tidy(ctx); err != nil {
return err
}
if err := Git(ctx, "diff", "--exit-code", "--", "go.mod", "go.sum"); err != nil {
return fmt.Errorf("go.mod and go.sum are not in sync. run `go mod tidy` and commit changes")
}
return nil
return golang.TidyAndVerify(ctx)
}
7 changes: 6 additions & 1 deletion godemo/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ func (a *App) Init() error {
return fmt.Errorf("failed to load spec: %w", err)
}

addr := os.Getenv("LISTEN_ADDR")
if addr == "" {
addr = ":8080"
}

srv := &http.Server{
Addr: os.Getenv("LISTEN_ADDR"),
Addr: addr,
Handler: api.NewRouter(spec),
}

Expand Down
Loading

0 comments on commit 66da2e5

Please sign in to comment.