Skip to content

Commit c064884

Browse files
committed
feat: CI for Protocol Buffers
Resolves: #71, resolves: #72
1 parent 874d567 commit c064884

File tree

5 files changed

+151
-0
lines changed

5 files changed

+151
-0
lines changed

internal/git/git.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package git
2+
3+
import "github.com/magefile/mage/sh"
4+
5+
// LatestTag returns the
6+
func LatestTag() (string, error) {
7+
return sh.Output("git", "describe", "--tags", "--abbrev=0")
8+
}

internal/targets/proto/proto.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package proto
2+
3+
import (
4+
"context"
5+
_ "embed"
6+
"fmt"
7+
8+
"github.com/coopnorge/mage/internal/devtool"
9+
"github.com/coopnorge/mage/internal/git"
10+
devtoolTarget "github.com/coopnorge/mage/internal/targets/devtool"
11+
"github.com/magefile/mage/mg"
12+
)
13+
14+
var (
15+
//go:embed tools.Dockerfile
16+
toolsDockerfile string
17+
)
18+
19+
// Generate all code
20+
func Generate(ctx context.Context) error {
21+
mg.CtxDeps(ctx, Validate, mg.F(generate, "."))
22+
return nil
23+
}
24+
25+
func generate(ctx context.Context, input string) error {
26+
mg.CtxDeps(ctx, mg.F(devtoolTarget.Build, "buf", toolsDockerfile))
27+
return devtool.Run("buf", "buf", "generate", input)
28+
}
29+
30+
// Validate all code
31+
func Validate(ctx context.Context) error {
32+
mg.CtxDeps(ctx, mg.F(validate, "."))
33+
return nil
34+
}
35+
36+
func validate(ctx context.Context, input string) error {
37+
mg.SerialCtxDeps(ctx, mg.F(lint, input), mg.F(formatCheck, input), mg.F(breaking, input))
38+
return nil
39+
}
40+
41+
func lint(ctx context.Context, input string) error {
42+
mg.CtxDeps(ctx, mg.F(devtoolTarget.Build, "buf", toolsDockerfile))
43+
return devtool.Run("buf", "buf", "lint", input)
44+
}
45+
46+
func formatCheck(ctx context.Context, input string) error {
47+
mg.CtxDeps(ctx, mg.F(devtoolTarget.Build, "buf", toolsDockerfile))
48+
return devtool.Run("buf", "buf", "format", "--diff", "--exit-code", input)
49+
}
50+
51+
func breaking(ctx context.Context, input string) error {
52+
mg.CtxDeps(ctx, mg.F(devtoolTarget.Build, "buf", toolsDockerfile))
53+
tag, err := git.LatestTag()
54+
if err != nil {
55+
return err
56+
}
57+
return devtool.Run("buf", "buf", "breaking", input, "--against", fmt.Sprintf(".git#tag=%s", tag))
58+
}
59+
60+
// Fix files
61+
func Fix(ctx context.Context) error {
62+
mg.CtxDeps(ctx, mg.F(fix, "."))
63+
return nil
64+
}
65+
66+
func fix(ctx context.Context, input string) error {
67+
mg.CtxDeps(ctx, mg.F(devtoolTarget.Build, "buf", toolsDockerfile))
68+
return devtool.Run("buf", "buf", "format", input, "--write")
69+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM docker.io/bufbuild/buf:1.50.0@sha256:c34c81ac26044490a10fb5009eb618640834b9048f38d4717538421c6a25e4d7 AS bufsource
2+
FROM docker.io/library/golang:1.24.1@sha256:ceb568d0de81fbef40ce4fee77eab524a0a0a8536065c51866ad8c59b7a912cf AS buf
3+
4+
COPY --from=bufsource /usr/local/bin/buf /usr/local/bin/

targets/proto/main.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package proto
2+
3+
import (
4+
"context"
5+
6+
"github.com/magefile/mage/mg"
7+
"github.com/magefile/mage/sh"
8+
)
9+
10+
// Build runs Validate
11+
func Build(ctx context.Context) error {
12+
mg.SerialCtxDeps(ctx, Validate, Proto.Generate)
13+
return nil
14+
}
15+
16+
// Generate output
17+
func Generate(ctx context.Context) error {
18+
mg.SerialCtxDeps(ctx, Validate, Proto.Generate)
19+
return nil
20+
}
21+
22+
// Validate all code
23+
func Validate(ctx context.Context) error {
24+
mg.CtxDeps(ctx, Proto.Validate)
25+
return nil
26+
}
27+
28+
// Fix files
29+
func Fix(ctx context.Context) error {
30+
mg.CtxDeps(ctx, Proto.Fix)
31+
return nil
32+
}
33+
34+
// Clean validate and build output
35+
func Clean(_ context.Context) error {
36+
return sh.Rm(outputDir)
37+
}

targets/proto/proto.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package proto
2+
3+
import (
4+
"context"
5+
6+
"github.com/coopnorge/mage/internal/targets/proto"
7+
"github.com/magefile/mage/mg"
8+
)
9+
10+
// Proto is the magefile namespace to group Protocol Buffers commands
11+
type Proto mg.Namespace
12+
13+
const (
14+
outputDir = "./var"
15+
)
16+
17+
// Generate all code
18+
func (Proto) Generate(ctx context.Context) error {
19+
mg.SerialCtxDeps(ctx, Proto.Validate, proto.Generate)
20+
return nil
21+
}
22+
23+
// Validate all code
24+
func (Proto) Validate(ctx context.Context) error {
25+
mg.CtxDeps(ctx, proto.Validate)
26+
return nil
27+
}
28+
29+
// Fix files
30+
func (Proto) Fix(ctx context.Context) error {
31+
mg.CtxDeps(ctx, proto.Fix)
32+
return nil
33+
}

0 commit comments

Comments
 (0)