package main // auto run protoc with the correct args import ( "errors" "fmt" "os" "path/filepath" "strings" "github.com/go-cmd/cmd" "go.wit.com/lib/gui/shell" "go.wit.com/log" ) // test.pb.go: test.proto // cd ~/go/src && protoc --go_out=. --proto_path=go.wit.com/apps/autogenpb/testautogen \ // --go_opt=Mtest.proto=go.wit.com/apps/autogenpb/testautogen \ // test.proto // forgeConfig.pb.go: forgeConfig.proto // cd ~/go/src && protoc --go_out=. --proto_path=go.wit.com/apps/autogenpb/testautogen \ // --go_opt=MforgeConfig.proto=go.wit.com/apps/autogenpb/testautogen \ // forgeConfig.proto func protocBuild(names map[string]string) error { // read in the .proto file data, err := os.ReadFile(names["protofile"]) if err != nil { // log.Info("open config file :", err) return err } log.Info("") if shell.Exists(names["protoc"]) { log.Info("protoc file already created", names["protoc"]) // return nil } log.Info("make protoc file:", names["protoc"]) // log.Info("go src", forge.GetGoSrc()) pwd, _ := os.Getwd() log.Info("go.Getwd()", pwd) if !strings.HasPrefix(pwd, argv.GoSrc) { return errors.New("paths don't match") } gopath := strings.TrimPrefix(pwd, argv.GoSrc) gopath = strings.Trim(gopath, "/") log.Info("gopath", gopath) cmd := []string{"protoc", "--go_out=."} cmd = append(cmd, "--proto_path="+gopath) cmd = append(cmd, "--go_opt=M"+names["protofile"]+"="+gopath) // look for included proto files lines := strings.Split(string(data), "\n") for _, line := range lines { // log.Info("line:", line) if strings.HasPrefix(line, "import ") { parts := strings.Split(line, "\"") protofile := parts[1] if shell.Exists(protofile) { log.Info("adding import", protofile) cmd = append(cmd, "--go_opt=M"+protofile+"="+gopath) } else { basepath, pname := filepath.Split(protofile) if basepath == "" { log.Warn("import line:", line, "missing proto file:", pname) log.Warn("protoc will probably fail here") } else { log.Warn("need to check basepath here", basepath, pname) } } } /* start := "type " + names["Bases"] + " struct {" if strings.HasSuffix(line, start) { found = true log.Info("FOUND line:", line) fmt.Fprintln(w, line) fmt.Fprintln(w, "\tsync.RWMutex // auto-added by go.wit.com/apps/autogenpb") fmt.Fprintln(w, "") } else { fmt.Fprintln(w, line) } */ } cmd = append(cmd, names["protofile"]) log.Info("\tpwd", argv.GoSrc) for i, s := range cmd { log.Info("\t", i, s) } return runprotoc(argv.GoSrc, cmd) } func runprotoc(pwd string, mycmd []string) error { result := shell.PathRun(argv.GoSrc, mycmd) if result.Error != nil { return userNotes(result) } if result.Exit != 0 { return userNotes(result) } return nil } func userNotes(result cmd.Status) error { log.Info("protoc failed", result.Cmd, "with", result.Exit) for _, line := range result.Stdout { log.Info("STDOUT:", line) } for _, line := range result.Stderr { log.Info("STDERR:", line) } log.Info("This is likely because you don't have protoc and protoc-gen-go installed") log.Info("") log.Info("On debian, you can:") log.Info(" apt install protobuf-compiler # for protoc") log.Info(" apt install protoc-gen-go # for protoc-gen-go") log.Info("") return fmt.Errorf("protoc failed with %d %v", result.Exit, result.Error) }