Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ func SHA256() (string, error) {
return sh.Output("git", "rev-parse", "HEAD")
}

// LatestTag returns the
func LatestTag() (string, error) {
return sh.Output("git", "describe", "--tags", "--abbrev=0")
}

// NormalizeGitURL parses git or https git URLs and returns an https URL.
func NormalizeGitURL(url string) (string, error) {
if strings.HasPrefix(url, "https://") {
Expand Down
69 changes: 69 additions & 0 deletions internal/targets/proto/proto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package proto

import (
"context"
_ "embed"
"fmt"

"github.com/coopnorge/mage/internal/devtool"
"github.com/coopnorge/mage/internal/git"
devtoolTarget "github.com/coopnorge/mage/internal/targets/devtool"
"github.com/magefile/mage/mg"
)

var (
//go:embed tools.Dockerfile
toolsDockerfile string
)

// Generate all code
func Generate(ctx context.Context) error {
mg.CtxDeps(ctx, Validate, mg.F(generate, "."))
return nil
}

func generate(ctx context.Context, input string) error {
mg.CtxDeps(ctx, mg.F(devtoolTarget.Build, "buf", toolsDockerfile))
return devtool.Run("buf", "buf", "generate", input)
}

// Validate all code
func Validate(ctx context.Context) error {
mg.CtxDeps(ctx, mg.F(validate, "."))
return nil
}

func validate(ctx context.Context, input string) error {
mg.SerialCtxDeps(ctx, mg.F(lint, input), mg.F(formatCheck, input), mg.F(breaking, input))
return nil
}

func lint(ctx context.Context, input string) error {
mg.CtxDeps(ctx, mg.F(devtoolTarget.Build, "buf", toolsDockerfile))
return devtool.Run("buf", "buf", "lint", input)
}

func formatCheck(ctx context.Context, input string) error {
mg.CtxDeps(ctx, mg.F(devtoolTarget.Build, "buf", toolsDockerfile))
return devtool.Run("buf", "buf", "format", "--diff", "--exit-code", input)
}

func breaking(ctx context.Context, input string) error {
mg.CtxDeps(ctx, mg.F(devtoolTarget.Build, "buf", toolsDockerfile))
tag, err := git.LatestTag()
if err != nil {
return err
}
return devtool.Run("buf", "buf", "breaking", input, "--against", fmt.Sprintf(".git#tag=%s", tag))
}

// Fix files
func Fix(ctx context.Context) error {
mg.CtxDeps(ctx, mg.F(fix, "."))
return nil
}

func fix(ctx context.Context, input string) error {
mg.CtxDeps(ctx, mg.F(devtoolTarget.Build, "buf", toolsDockerfile))
return devtool.Run("buf", "buf", "format", input, "--write")
}
4 changes: 4 additions & 0 deletions internal/targets/proto/tools.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM docker.io/bufbuild/buf:1.50.0@sha256:c34c81ac26044490a10fb5009eb618640834b9048f38d4717538421c6a25e4d7 AS bufsource
FROM docker.io/library/golang:1.24.1@sha256:ceb568d0de81fbef40ce4fee77eab524a0a0a8536065c51866ad8c59b7a912cf AS buf

COPY --from=bufsource /usr/local/bin/buf /usr/local/bin/
37 changes: 37 additions & 0 deletions targets/proto/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package proto

import (
"context"

"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)

// Build runs Validate
func Build(ctx context.Context) error {
mg.SerialCtxDeps(ctx, Validate, Proto.Generate)
return nil
}

// Generate output
func Generate(ctx context.Context) error {
mg.SerialCtxDeps(ctx, Validate, Proto.Generate)
return nil
}

// Validate all code
func Validate(ctx context.Context) error {
mg.CtxDeps(ctx, Proto.Validate)
return nil
}

// Fix files
func Fix(ctx context.Context) error {
mg.CtxDeps(ctx, Proto.Fix)
return nil
}

// Clean validate and build output
func Clean(_ context.Context) error {
return sh.Rm(outputDir)
}
33 changes: 33 additions & 0 deletions targets/proto/proto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package proto

import (
"context"

"github.com/coopnorge/mage/internal/targets/proto"
"github.com/magefile/mage/mg"
)

// Proto is the magefile namespace to group Protocol Buffers commands
type Proto mg.Namespace

const (
outputDir = "./var"
)

// Generate all code
func (Proto) Generate(ctx context.Context) error {
mg.SerialCtxDeps(ctx, Proto.Validate, proto.Generate)
return nil
}

// Validate all code
func (Proto) Validate(ctx context.Context) error {
mg.CtxDeps(ctx, proto.Validate)
return nil
}

// Fix files
func (Proto) Fix(ctx context.Context) error {
mg.CtxDeps(ctx, proto.Fix)
return nil
}
Loading