From c7695ea2798bd77fab03089cabee77efbc00a4b1 Mon Sep 17 00:00:00 2001 From: bwireman Date: Sun, 6 Feb 2022 15:07:29 -0600 Subject: [PATCH] cleanup flags and module --- README.md | 2 +- pkg/gleam/format.go | 8 +++----- pkg/gleam/gpb.go | 10 +++++----- pkg/gleam/module.go | 12 +++++++++--- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 5d918bd..a5991cc 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/pkg/gleam/format.go b/pkg/gleam/format.go index 38efdc1..1b2f546 100644 --- a/pkg/gleam/format.go +++ b/pkg/gleam/format.go @@ -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 diff --git a/pkg/gleam/gpb.go b/pkg/gleam/gpb.go index 21d5e1b..9aa619f 100644 --- a/pkg/gleam/gpb.go +++ b/pkg/gleam/gpb.go @@ -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) @@ -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) +} diff --git a/pkg/gleam/module.go b/pkg/gleam/module.go index e62bf89..530e16a 100644 --- a/pkg/gleam/module.go +++ b/pkg/gleam/module.go @@ -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" } @@ -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()) }