Skip to content

Commit

Permalink
added gogo.proto check using GOPATH not hardcoded
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Jeannopoulos committed Jul 8, 2020
1 parent 7b1741e commit a07fbce
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
github.com/karrick/godirwalk v1.15.6 // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/lib/pq v1.3.0
github.com/logrusorgru/aurora v2.0.3+incompatible // indirect
github.com/mattn/go-sqlite3 v2.0.3+incompatible
github.com/mwitkow/go-proto-validators v0.3.0 // indirect
github.com/ompluscator/dynamic-struct v1.2.0
Expand Down
36 changes: 19 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"database/sql"
"encoding/json"
"fmt"
"go/build"
"io"
"io/ioutil"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"

Expand All @@ -20,6 +20,7 @@ import (
"github.com/jimsmart/schema"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/lib/pq"
"github.com/logrusorgru/aurora"
_ "github.com/mattn/go-sqlite3"

"github.com/smallnest/gen/dbmeta"
Expand All @@ -46,6 +47,7 @@ var (
module = goopt.String([]string{"--module"}, "example.com/example", "module path")
overwrite = goopt.Flag([]string{"--overwrite"}, []string{"--no-overwrite"}, "Overwrite existing files (default)", "disable overwriting files")
windows = goopt.Flag([]string{"--windows"}, []string{}, "use windows line endings in generated files", "")
noColorOutput = goopt.Flag([]string{"--no-color"}, []string{}, "disable color output", "")

contextFileName = goopt.String([]string{"--context"}, "", "context file (json) to populate context with")
mappingFileName = goopt.String([]string{"--mapping"}, "", "mapping file (json) to map sql types to golang/protobuf etc")
Expand Down Expand Up @@ -89,6 +91,7 @@ var (

baseTemplates *packr.Box
tableInfos map[string]*dbmeta.ModelInfo
au aurora.Aurora
)

func init() {
Expand Down Expand Up @@ -147,6 +150,7 @@ func main() {
//for i, arg := range os.Args {
// fmt.Printf("[%2d] %s\n", i, arg)
//}
au = aurora.NewAurora(!*noColorOutput)

baseTemplates = packr.New("gen", "./template")

Expand Down Expand Up @@ -722,27 +726,25 @@ func createProtocCmdLine(protoBufDir, protoBufOutDir, protoBufFile string) ([]st
}
}

usr, err := user.Current()
if err == nil {
dir := usr.HomeDir
srcPath := filepath.Join(dir, "go/src")

//srcDirExists := dbmeta.Exists(srcPath)
gopath := os.Getenv("GOPATH")
if gopath == "" {
gopath = build.Default.GOPATH
}

gogoPath := filepath.Join(dir, "go/src/github.com/gogo/protobuf/gogoproto/gogo.proto")
gogoImportExists := dbmeta.Exists(gogoPath)
srcPath := filepath.Join(gopath, "src")
gogoPath := filepath.Join(gopath, "src/github.com/gogo/protobuf/gogoproto/gogo.proto")
gogoImportExists := dbmeta.Exists(gogoPath)

//fmt.Printf("path : %s srcDirExists: %t\n", srcPath, srcDirExists)
//fmt.Printf("gogoPath: %s gogoImportExists: %t\n", gogoPath, gogoImportExists)
//fmt.Printf("path : %s srcDirExists: %t\n", srcPath, srcDirExists)
//fmt.Printf("gogoPath: %s gogoImportExists: %t\n", gogoPath, gogoImportExists)

if !gogoImportExists {
fmt.Printf("github.com/gogo/protobuf/gogoproto/gogo.proto does not exist on path - install with\ngo get -u github.com/gogo/protobuf/proto\n\n")
return nil, fmt.Errorf("github.com/gogo/protobuf/gogoproto/gogo.proto does not exist")
}

*gogoProtoImport = srcPath
if !gogoImportExists {
fmt.Print(au.Red("github.com/gogo/protobuf/gogoproto/gogo.proto does not exist on path - install with\ngo get -u github.com/gogo/protobuf/proto\n\n"))
return nil, fmt.Errorf("github.com/gogo/protobuf/gogoproto/gogo.proto does not exist")
}

*gogoProtoImport = srcPath

fmt.Printf("----------------------------\n")

args := []string{fmt.Sprintf("-I%s", *gogoProtoImport),
Expand Down

0 comments on commit a07fbce

Please sign in to comment.