github.com/evrenkutar/randevent@v0.0.0-20210506235643-7d1e39a375e1/cmd/generate_proto.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os/exec" 7 "path" 8 9 log "github.com/sirupsen/logrus" 10 ) 11 12 func GenerateProto(f string) error { 13 protocExc, err := exec.LookPath("protoc") 14 if err != nil { 15 return &exec.Error{ 16 Name: "protoc executable not found", 17 Err: err, 18 } 19 } 20 21 d, err := ioutil.ReadFile(f) 22 if err != nil { 23 return &exec.Error{ 24 Name: "file cannot be read", 25 Err: err, 26 } 27 } 28 _, protoFileName := path.Split(f) 29 err = ioutil.WriteFile(fmt.Sprintf("samples/%s", protoFileName), d, 0655) 30 if err != nil { 31 return &exec.Error{ 32 Name: "file cannot be saved", 33 Err: err, 34 } 35 } 36 37 command := &exec.Cmd{ 38 Path: protocExc, 39 Args: []string{"", 40 "-I=samples", "--go_out=pb", 41 fmt.Sprintf("samples/%s", protoFileName)}, 42 } 43 log.Infof("proto generate command: %s", command.String()) 44 err = command.Run() 45 if err != nil { 46 return err 47 } 48 return nil 49 // protoc -I=$SRC_DIR --go_out=$DST_DIR $SRC_DIR/addressbook.proto 50 }