Skip to content

Commit

Permalink
add gogenproto generate package (#14)
Browse files Browse the repository at this point in the history
### Background

→ I want to be able to generate specific protos relative to a specific
path.

### Changes

- new gogenproto generator based on the script I keep copying around.

### Testing

- added duh
  • Loading branch information
drshriveer authored Sep 27, 2023
1 parent 9089801 commit 23da8c6
Show file tree
Hide file tree
Showing 14 changed files with 663 additions and 3 deletions.
2 changes: 2 additions & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use (
./gencommon
./genum
./gerror
./gogenproto
./gogenproto/internal
./gsort
./gsync
./log
Expand Down
4 changes: 1 addition & 3 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
github.com/drshriveer/gtools/gencommon v0.0.0-20230921080944-8f1b8f9e43fd/go.mod h1:ER4mobl8fvlqt9fpQSph/LpbiPyNfmR+4QszQrJgcZg=
github.com/drshriveer/gtools/rutils v0.0.0-20230921080944-8f1b8f9e43fd/go.mod h1:ixRN41tPraNiLhfegjbhtt9zFLW8XXhZxUlmSaGGU3w=
github.com/drshriveer/gtools/set v0.0.0-20230921080944-8f1b8f9e43fd/go.mod h1:oKjRVsjkkR+xeL/D3IqO/QrSSibcoXYuDapbc46LQyA=
github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
Expand Down
28 changes: 28 additions & 0 deletions gogenproto/cmd/gogenproto/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"flag"
"log"
"os"

"github.com/itzg/go-flagsfiller"

"github.com/drshriveer/gtools/gogenproto/gen"
)

func main() {
g := gen.Generate{}
filler := flagsfiller.New()
if err := filler.Fill(flag.CommandLine, &g); err != nil {
gen.Logger.Fatal(err)
}
flag.Parse()

for _, s := range os.Environ() {
println("env: " + s)
}

if err := g.Run(); err != nil {
log.Fatalf("run failed: %+v", err)
}
}
65 changes: 65 additions & 0 deletions gogenproto/gen/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package gen

import (
"io/fs"
"log"
"os/exec"
"path"
"path/filepath"
"strings"
)

// Logger is the name-spaced logger for this script.
// It is exposed for use in the main package.
var Logger = log.New(log.Writer(), "[gogenproto] ", log.LstdFlags)

// Generate is a simple script for generating proto files with a go:generate directive
// relative to the input directory.
type Generate struct {
InputDir string `alias:"inputDir" env:"PWD" usage:"path to root directory for proto generation"`
OutputDir string `alias:"outputDir" default:"../" usage:"relative output path for generated files"`
// TODO: other flags, like VTProto, GRPC, TS, etc,
}

// Run runs the generate command.
func (g Generate) Run() error {
paths, err := g.findProtos()
if err != nil {
return err
}
args := []string{
"--proto_path=" + path.Dir(g.InputDir),
"--go_out=" + g.OutputDir,
"--fatal_warnings",
}
args = append(args, paths...)
cmd := exec.Command("protoc", args...)
cmd.Stdout = logPipe{}
cmd.Stderr = logPipe{}
return cmd.Run()
}

func (g Generate) findProtos() ([]string, error) {
protoList := []string{}
err := filepath.WalkDir(g.InputDir,
func(pathname string, d fs.DirEntry, err error) error {
if err != nil || pathname == "." {
return err
} else if d.Type().IsRegular() {
if filepath.Ext(d.Name()) == ".proto" {
protoList = append(protoList, pathname)
}
}
return nil
},
)
return protoList, err
}

type logPipe struct{}

func (lw logPipe) Write(p []byte) (n int, err error) {
toLog := strings.TrimSuffix(string(p), "\n")
Logger.Println(toLog)
return len(p), nil
}
10 changes: 10 additions & 0 deletions gogenproto/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/drshriveer/gtools/gogenproto

go 1.21.1

require github.com/itzg/go-flagsfiller v1.12.0

require (
github.com/iancoleman/strcase v0.3.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
)
12 changes: 12 additions & 0 deletions gogenproto/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/itzg/go-flagsfiller v1.12.0 h1:LSwSUGxzZqueprm0D8FBCAG0JMgwAkkh2UjtwreNgAg=
github.com/itzg/go-flagsfiller v1.12.0/go.mod h1:47WeO9fl+QyS48AdRHfarhF3rKBh0enbe90tTko03gg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3 changes: 3 additions & 0 deletions gogenproto/internal/gen_proto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package internal

//go:generate gogenproto
11 changes: 11 additions & 0 deletions gogenproto/internal/gen_proto_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package internal

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGenProto(t *testing.T) {
assert.FileExistsf(t, "./test.pb.go", "you must run go generate for this test to work.")
}
15 changes: 15 additions & 0 deletions gogenproto/internal/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module github.com/drshriveer/gtools/gogenproto/internal

go 1.21

require (
github.com/stretchr/testify v1.8.4
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.3.0
google.golang.org/protobuf v1.31.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
20 changes: 20 additions & 0 deletions gogenproto/internal/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.3.0 h1:rNBFJjBCOgVr9pWD7rs/knKL4FRTKgpZmsRfV214zcA=
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.3.0/go.mod h1:Dk1tviKTvMCz5tvh7t+fh94dhmQVHuCt2OzJB3CTW9Y=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit 23da8c6

Please sign in to comment.