Some checks are pending
Docs Deploy / build_and_deploy (push) Waiting to run
Generate Docs / cli (push) Waiting to run
Generate Config Doc / cli (push) Waiting to run
Go formatting / go-formatting (push) Waiting to run
Check links / markdown-link-check (push) Waiting to run
Integration / pre-test (push) Waiting to run
Integration / test on (push) Blocked by required conditions
Integration / status (push) Blocked by required conditions
Lint / Lint Go code (push) Waiting to run
Test / test (ubuntu-latest) (push) Waiting to run
36 lines
822 B
Go
36 lines
822 B
Go
package protoutil
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/emicklei/proto"
|
|
"github.com/emicklei/proto-contrib/pkg/protofmt"
|
|
)
|
|
|
|
// ParseProtoPath opens the file denoted by path and parses it
|
|
// into a proto file.
|
|
func ParseProtoPath(path string) (pf *proto.Proto, err error) {
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer f.Close()
|
|
|
|
return proto.NewParser(f).Parse()
|
|
}
|
|
|
|
// ParseProtoFile parses the given file.
|
|
func ParseProtoFile(r io.Reader) (*proto.Proto, error) {
|
|
return proto.NewParser(r).Parse()
|
|
}
|
|
|
|
// Print formats the proto file using proto-contrib/pkg/protofmt.
|
|
// This does have certain opinions on how formatting is done.
|
|
func Print(pf *proto.Proto) string {
|
|
output := new(strings.Builder)
|
|
protofmt.NewFormatter(output, " ").Format(pf) // 2 spaces
|
|
|
|
return output.String()
|
|
}
|