Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/protoc-gen-gclient: factor out captureInput #70

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 28 additions & 23 deletions generator/cmd/protoc-gen-gclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"flag"
"fmt"
"io"
"log"
"log/slog"
"os"
"slices"
Expand All @@ -37,18 +38,19 @@ func main() {
templateDir := flag.String("template-dir", "templates/", "the path to the template directory")
flag.Parse()

if err := run(*inputPath, *outDir, *templateDir); err != nil {
slog.Error(err.Error())
os.Exit(1)
if err := run(os.Stdin, os.Stdout, *inputPath, *outDir, *templateDir); err != nil {
log.Fatal(err)
}
slog.Info("Generation Completed Successfully")
}

func run(inputPath, outDir, templateDir string) error {
var reqBytes []byte
var err error
func run(r io.Reader, w io.Writer, inputPath, outDir, templateDir string) error {
var (
reqBytes []byte
err error
)
if inputPath == "" {
reqBytes, err = io.ReadAll(os.Stdin)
reqBytes, err = io.ReadAll(r)
if err != nil {
return err
}
Expand All @@ -63,23 +65,16 @@ func run(inputPath, outDir, templateDir string) error {
if err := proto.Unmarshal(reqBytes, genReq); err != nil {
return err
}

opts, err := parseOpts(genReq.GetParameter())
if err != nil {
return err
}

// If capture-input is set, content pass to protoc will be written to a
// sample-input-{timestamp}.bin file, so that protoc does not need to be
// used on future iterations.
if opts.CaptureInput {
// Remove capture-input param from the captured input
ss := slices.DeleteFunc(strings.Split(genReq.GetParameter(), ","), func(s string) bool {
return strings.Contains(s, "capture-input")
})
genReq.Parameter = proto.String(strings.Join(ss, ","))
reqBytes, err = proto.Marshal(genReq)
if err != nil {
return err
}
if err := os.WriteFile(fmt.Sprintf("sample-input-%s.bin", time.Now().Format(time.RFC3339)), reqBytes, 0644); err != nil {
if err := captureInput(genReq); err != nil {
return err
}
}
Expand All @@ -102,11 +97,8 @@ func run(inputPath, outDir, templateDir string) error {
if err != nil {
return err
}
if _, err := os.Stdout.Write(outBytes); err != nil {
return err
}

return nil
_, err = w.Write(outBytes)
return err
}

type protobufOptions struct {
Expand Down Expand Up @@ -142,3 +134,16 @@ func parseOpts(optStr string) (*protobufOptions, error) {
}
return opts, nil
}

func captureInput(genReq *pluginpb.CodeGeneratorRequest) error {
// Remove capture-input param from the captured input
ss := slices.DeleteFunc(strings.Split(genReq.GetParameter(), ","), func(s string) bool {
return strings.Contains(s, "capture-input")
})
genReq.Parameter = proto.String(strings.Join(ss, ","))
reqBytes, err := proto.Marshal(genReq)
if err != nil {
return err
}
return os.WriteFile(fmt.Sprintf("sample-input-%s.bin", time.Now().Format(time.RFC3339)), reqBytes, 0644)
}
2 changes: 1 addition & 1 deletion generator/cmd/protoc-gen-gclient/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestRun_Rust(t *testing.T) {
outDir = "../../testdata/rust/gclient/golden"
templateDir = "../../templates"
)
if err := run(inputPath, outDir, templateDir); err != nil {
if err := run(os.Stdin, os.Stdout, inputPath, outDir, templateDir); err != nil {
t.Fatal(err)
}
}
Loading