From 886bc69f7ad2ca1a029e38740a45bdd5b74e7111 Mon Sep 17 00:00:00 2001 From: Niels Henrik Hagen Date: Mon, 10 Mar 2025 18:17:16 +0100 Subject: [PATCH] feat: CI for Protocol Buffers Resolves: #71, resolves: #72 --- internal/git/git.go | 5 ++ internal/targets/proto/proto.go | 69 +++++++++++++++++++++++++ internal/targets/proto/tools.Dockerfile | 4 ++ targets/proto/main.go | 37 +++++++++++++ targets/proto/proto.go | 33 ++++++++++++ 5 files changed, 148 insertions(+) create mode 100644 internal/targets/proto/proto.go create mode 100644 internal/targets/proto/tools.Dockerfile create mode 100644 targets/proto/main.go create mode 100644 targets/proto/proto.go diff --git a/internal/git/git.go b/internal/git/git.go index fae953f0..fc569e85 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -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://") { diff --git a/internal/targets/proto/proto.go b/internal/targets/proto/proto.go new file mode 100644 index 00000000..f4db082e --- /dev/null +++ b/internal/targets/proto/proto.go @@ -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") +} diff --git a/internal/targets/proto/tools.Dockerfile b/internal/targets/proto/tools.Dockerfile new file mode 100644 index 00000000..fa12a3a6 --- /dev/null +++ b/internal/targets/proto/tools.Dockerfile @@ -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/ diff --git a/targets/proto/main.go b/targets/proto/main.go new file mode 100644 index 00000000..71f67713 --- /dev/null +++ b/targets/proto/main.go @@ -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) +} diff --git a/targets/proto/proto.go b/targets/proto/proto.go new file mode 100644 index 00000000..de82cac1 --- /dev/null +++ b/targets/proto/proto.go @@ -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 +}