Skip to content

Commit

Permalink
cleanup flags and module
Browse files Browse the repository at this point in the history
  • Loading branch information
bwireman committed Feb 6, 2022
1 parent 888f728 commit c7695ea
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ Example Script

```bash
# make sure protoc-gen-gleam is in you're path or add it manually using --plugin
protoc --plugin=protoc-gen-gleam -I . --gleam_out="src" protos/*.proto
protoc --plugin=protoc-gen-gleam -I . --gleam_out="output_path=./src:./src" protos/*.proto
```

### `gleam_pb` Flags
Expand Down
8 changes: 3 additions & 5 deletions pkg/gleam/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ func (gf GleamFormatter) Match(a pgs.Artifact) bool {
}
}

func (gf GleamFormatter) Process(in []byte) (stdout []byte, err error) {
func (gf GleamFormatter) Process(in []byte) ([]byte, error) {
cmd := exec.Command("gleam", "format", "--stdin")
cmd.Stdin = bytes.NewReader(in)

stdout, err = cmd.Output()
stdout, err := cmd.Output()
if err != nil {
fmt.Println(err.Error())
return
return nil, fmt.Errorf("Error formating generated gleam code: %s", string(stdout))
}

return stdout, nil
Expand Down
10 changes: 5 additions & 5 deletions pkg/gleam/gpb.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ type gpbWrapper struct {
pathToBinary string
}

func exists(path string) bool {
_, err := os.Open(path)
return !errors.Is(err, os.ErrNotExist)
}

func newGPBWrapper(pathToBinary string) (*gpbWrapper, error) {
if !exists(pathToBinary) {
return nil, fmt.Errorf("protoc-erl could not be found at %s", pathToBinary)
Expand All @@ -39,3 +34,8 @@ func (g *gpbWrapper) generate(targets []string, outputPath string) (err error) {

return nil
}

func exists(path string) bool {
_, err := os.Open(path)
return !errors.Is(err, os.ErrNotExist)
}
12 changes: 9 additions & 3 deletions pkg/gleam/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@ import (

type GleamModule struct {
*pgs.ModuleBase
tpl *template.Template
tpl *template.Template
protocErlPath string
output string
}

func Gleam() *GleamModule { return &GleamModule{ModuleBase: &pgs.ModuleBase{}} }

func (g *GleamModule) InitContext(c pgs.BuildContext) {
g.ModuleBase.InitContext(c)

g.protocErlPath = g.Parameters().StrDefault("protoc_erl_path", "./deps/gpb/bin/protoc-erl")
g.tpl = template.Must(template.New("gleam-package-template").Parse(fields.GleamTemplate))

if g.output = g.Parameters().Str("output_path"); g.output == "" {
g.Fail("please specify the `output_path` flag")
}
}

func (g *GleamModule) Name() string { return "gleam" }
Expand All @@ -31,7 +37,7 @@ func (g *GleamModule) Execute(targets map[string]pgs.File, pkgs map[string]pgs.P
}
}

wrapper, err := newGPBWrapper(g.Parameters().StrDefault("protoc_erl_path", "./deps/gpb/bin/protoc-erl"))
wrapper, err := newGPBWrapper(g.protocErlPath)
if err != nil {
g.Fail(err.Error())
}
Expand Down

0 comments on commit c7695ea

Please sign in to comment.