diff --git a/go.mod b/go.mod index 46260a8e9..aafd64548 100644 --- a/go.mod +++ b/go.mod @@ -39,7 +39,7 @@ require ( github.com/fatih/color v1.7.0 // indirect github.com/fatih/structs v1.0.0 // indirect github.com/go-redis/redis v6.14.1+incompatible - github.com/gogo/protobuf v1.1.1 + github.com/gogo/protobuf v1.1.2-0.20181116123445-07eab6a8298c github.com/golang-migrate/migrate/v3 v3.5.2 github.com/golang/mock v1.1.1 github.com/golang/protobuf v1.2.0 diff --git a/go.sum b/go.sum index 739693d36..777ec8bd5 100644 --- a/go.sum +++ b/go.sum @@ -98,6 +98,8 @@ github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG github.com/gocql/gocql v0.0.0-20180913072538-864d5908455a/go.mod h1:4Fw1eo5iaEhDUs8XyuhSVCVy52Jq3L+/3GJgYkwc+/0= github.com/gogo/protobuf v1.1.1 h1:72R+M5VuhED/KujmZVcIquuo8mBgX4oVda//DQb3PXo= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= +github.com/gogo/protobuf v1.1.2-0.20181116123445-07eab6a8298c h1:c8VQNu/587ErbVKJSz6kKVdrf3kS18Sn50UShPyJ7Wc= +github.com/gogo/protobuf v1.1.2-0.20181116123445-07eab6a8298c/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/golang-migrate/migrate/v3 v3.5.2 h1:SUWSv6PD8Lr2TGx1lmVW7W2lRoQiVny3stM4He6jczQ= github.com/golang-migrate/migrate/v3 v3.5.2/go.mod h1:QDa9JH6Bdwbi+1jIEpOIWnTNoYRbtaVgByErXU0844E= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= diff --git a/scripts/protobuf.go b/scripts/protobuf.go index 0405829f4..5ebcc9b9b 100644 --- a/scripts/protobuf.go +++ b/scripts/protobuf.go @@ -13,6 +13,7 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "sort" "strings" ) @@ -31,56 +32,79 @@ func main() { root = "." } - var err error - - switch flag.Arg(0) { - case "install": - // TODO: lock versions - err = install( - "github.com/ckaznocha/protoc-gen-lint", - // See https://github.com/gogo/protobuf#most-speed-and-most-customization - "github.com/gogo/protobuf/proto", - "github.com/gogo/protobuf/jsonpb", - "github.com/gogo/protobuf/protoc-gen-gogo", - "github.com/gogo/protobuf/gogoproto", - ) - case "generate": - err = walkdirs(root, generate) - case "lint": - err = walkdirs(root, lint) - default: - fmt.Fprintf(os.Stderr, "unknown command %q\n", flag.Arg(0)) - os.Exit(1) - } - + err := run(flag.Arg(0), root) if err != nil { - fmt.Fprintf(os.Stderr, "failure: %v\n", err) + fmt.Fprintf(os.Stderr, "failed: %v\n", err) os.Exit(1) } } -func install(deps ...string) error { - gomod, err := ioutil.ReadFile("go.mod") - if err != nil { - return err +func run(command, root string) error { + switch command { + case "install": + err := installGoBin() + if err != nil { + return err + } + + gogoVersion, err := versionOf("github.com/gogo/protobuf") + if err != nil { + return err + } + + return install( + "github.com/ckaznocha/protoc-gen-lint@68a05858965b31eb872cbeb8d027507a94011acc", + // See https://github.com/gogo/protobuf#most-speed-and-most-customization + "github.com/gogo/protobuf/protoc-gen-gogo@"+gogoVersion, + ) + case "generate": + return walkdirs(root, generate) + case "lint": + return walkdirs(root, lint) + default: + return errors.New("unknown command " + command) } - defer func() { - stat, err := os.Stat("go.mod") - if err != nil { - panic(err) - } - err = ioutil.WriteFile("go.mod", gomod, stat.Mode()) - if err != nil { - panic(err) - } - }() + return nil +} - args := []string{"get", "-u"} - args = append(args, deps...) +func installGoBin() error { + // already installed? + path, err := exec.LookPath("gobin") + if path != "" && err == nil { + return nil + } - cmd := exec.Command("go", args...) + cmd := exec.Command("go", "get", "-u", "github.com/myitcv/gobin") fmt.Println(strings.Join(cmd.Args, " ")) + cmd.Env = append(os.Environ(), "GO111MODULE=off") + + out, err := cmd.CombinedOutput() + if len(out) > 0 { + fmt.Println(string(out)) + } + return err +} + +func versionOf(dep string) (string, error) { + moddata, err := ioutil.ReadFile("go.mod") + if err != nil { + return "", err + } + + rxMatch := regexp.MustCompile(regexp.QuoteMeta(dep) + `\s+(.*)\n`) + matches := rxMatch.FindAllStringSubmatch(string(moddata), 1) + if len(matches) == 0 { + return "", errors.New("go.mod missing github.com/gogo/protobuf entry") + } + + return matches[0][1], nil +} + +func install(deps ...string) error { + cmd := exec.Command("gobin", deps...) + fmt.Println(strings.Join(cmd.Args, " ")) + out, err := cmd.CombinedOutput() if len(out) > 0 { fmt.Println(string(out))