Skip to content

Commit

Permalink
Add protoc-gen-go plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
shiv3 committed Jul 5, 2021
1 parent 691cb06 commit 2d47558
Show file tree
Hide file tree
Showing 14 changed files with 552 additions and 13 deletions.
47 changes: 47 additions & 0 deletions adapter/github/protoc-gen-go/protoc-gen-go.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package protoc_gen_go

import (
"context"
"fmt"
"github.com/shiv3/protoenv/adapter/github"
"path/filepath"
"strings"
)

var (
owner = "protocolbuffers"
repo = "protobuf-go"
)

func GetprotocGenGoRepoGoVersions(ctx context.Context) ([]string, error) {
return github.GetVersions(ctx, owner, repo)
}

func GetprotocGenGoRepoGoGetReleaseAssetURL(ctx context.Context, tag string, os, arch string) (string, error) {
assets, err := github.GetReleaseAssets(ctx, owner, repo, tag)
if err != nil {
return "", err
}
for _, asset := range assets {
url := asset.GetBrowserDownloadURL()
filename := filepath.Base(url)
archString := Get(os, arch)
if strings.Contains(filename, "protoc") && strings.Contains(filename, archString) {
return url, nil
}
}
return "", fmt.Errorf("Unknown version")
}

func Get(os, arch string) string {
switch fmt.Sprintf("%s/%s", os, arch) {
case "darwin/amd64":
return "darwin.amd64"
case "linux/386":
return "linux.386"
case "linux/amd64":
return "linux.amd64"
default:
return "Unknown"
}
}
17 changes: 9 additions & 8 deletions adapter/github/protobuf.go → adapter/github/protoc/protobuf.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package github
package protoc

import (
"context"
"fmt"
"github.com/shiv3/protoenv/adapter/github"
"path/filepath"
"strings"
)
Expand All @@ -13,27 +14,27 @@ var (
)

func GetProtobufVersions(ctx context.Context) ([]string, error) {
return GetVersions(ctx, owner, repo)
return github.GetVersions(ctx, owner, repo)
}

func GetProtobufGetReleaseAssetURL(ctx context.Context, tag string,os ,arch string) (string, error) {
assets, err := GetReleaseAssets(ctx, owner, repo, tag)
func GetProtobufGetReleaseAssetURL(ctx context.Context, tag string, os, arch string) (string, error) {
assets, err := github.GetReleaseAssets(ctx, owner, repo, tag)
if err != nil {
return "", err
}
for _, asset := range assets {
url := asset.GetBrowserDownloadURL()
filename := filepath.Base(url)
archString := Get(os,arch)
if strings.Contains(filename,"protoc") && strings.Contains(filename,archString) {
archString := Get(os, arch)
if strings.Contains(filename, "protoc") && strings.Contains(filename, archString) {
return url, nil
}
}
return "", fmt.Errorf("Unknown version")
}

func Get(os,arch string) string {
switch fmt.Sprintf("%s/%s",os,arch) {
func Get(os, arch string) string {
switch fmt.Sprintf("%s/%s", os, arch) {
case "darwin/386":
return "osx-x86_64"
case "darwin/amd64":
Expand Down
31 changes: 31 additions & 0 deletions cmd/commands/plugins/commands/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package commands

import (
"fmt"
"github.com/spf13/cobra"
)

type List struct {
plugins []string
}

func NewList(parentCmd *cobra.Command, plugins []string) List {
list := List{
plugins: plugins,
}
cmd := &cobra.Command{
Use: "list",
Short: fmt.Sprintf("list plugins"),
Long: fmt.Sprintf(`list plugins`),
RunE: list.RunE,
}
parentCmd.AddCommand(cmd)
return list
}

func (i *List) RunE(cmd *cobra.Command, args []string) error {
for _, plugin := range i.plugins {
fmt.Printf("%s\n", plugin)
}
return nil
}
62 changes: 62 additions & 0 deletions cmd/commands/plugins/commands/protoc-gen-go/commands/global.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package commands

import (
"fmt"
"github.com/spf13/cobra"
"os"
"path/filepath"
)

type Global struct {
InstallDirectoryPath string
ShowVersionFormatSimple string
TargetBinaryFileName string
}

func NewGlobal(parentCmd *cobra.Command, installDirectoryPath string, ShowVersionFormatSimple string, TargetBinaryFileName string) Global {
global := Global{
InstallDirectoryPath: installDirectoryPath,
ShowVersionFormatSimple: ShowVersionFormatSimple,
TargetBinaryFileName: TargetBinaryFileName,
}
cmd := &cobra.Command{
Use: "global",
Short: fmt.Sprintf("Set or show the global %s version", TargetBinaryFileName),
Long: fmt.Sprintf(`Set or show the global %s version`, TargetBinaryFileName),
RunE: global.RunE,
}
parentCmd.AddCommand(cmd)
return global
}

func (i *Global) RunE(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
version := args[0]
if err := setVersion(getVersionsPath(i.InstallDirectoryPath), getGlobalVersionFilePath(i.InstallDirectoryPath), version); err != nil {
return err
}
return setShims(GetShimsFileDir(i.InstallDirectoryPath), i.TargetBinaryFileName, i.InstallDirectoryPath, version)
}
v, err := getVersion(getGlobalVersionFilePath(i.InstallDirectoryPath))
if err != nil {
return err
}
fmt.Printf(i.ShowVersionFormatSimple, v)
return nil
}

func setShims(shimsDir, targetFileName, installPath, version string) error {
if _, err := os.Stat(filepath.Join(shimsDir)); os.IsNotExist(err) {
err = os.MkdirAll(shimsDir, os.ModePerm)
if err != nil {
return err
}
}
shimsPath := filepath.Join(shimsDir, targetFileName)
if _, err := os.Lstat(shimsPath); err == nil {
if err := os.Remove(shimsPath); err != nil {
return err
}
}
return nil
}
35 changes: 35 additions & 0 deletions cmd/commands/plugins/commands/protoc-gen-go/commands/init_cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package commands

import (
"fmt"
"github.com/spf13/cobra"
"os"
)

type Init struct {
InstallDirectoryPath string
TargetBinaryFileName string
}

func NewInit(parentCmd *cobra.Command, installDirectoryPath string, TargetBinaryFileName string) Init {
init := Init{
InstallDirectoryPath: installDirectoryPath,
TargetBinaryFileName: TargetBinaryFileName,
}
cmd := &cobra.Command{
Use: "init",
Short: fmt.Sprintf("Set or show the global Go version", TargetBinaryFileName),
Long: fmt.Sprintf(`Set or show the global Go version`, TargetBinaryFileName),
RunE: init.RunE,
}
parentCmd.AddCommand(cmd)
return init
}

func (i *Init) RunE(cmd *cobra.Command, args []string) error {
if _, err := os.Stat(i.InstallDirectoryPath); os.IsNotExist(err) {
return os.Mkdir(i.InstallDirectoryPath, os.ModePerm)
}
fmt.Printf(`export PATH=$PATH:%s`, GetShimsFileDir(i.InstallDirectoryPath))
return nil
}
91 changes: 91 additions & 0 deletions cmd/commands/plugins/commands/protoc-gen-go/commands/install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package commands

import (
"context"
"errors"
"fmt"
protoc_gen_go "github.com/shiv3/protoenv/adapter/github/protoc-gen-go"
"github.com/shiv3/protoenv/adapter/installer"
"os"
"path/filepath"
"runtime"

"github.com/spf13/cobra"
)

type Install struct {
InstallDirectoryPath string
ShowVersionFormatSimple string
TargetBinaryFileName string
}

func NewInstall(parentCmd *cobra.Command, installDirectoryPath string, ShowVersionFormatSimple string, TargetBinaryFileName string) Install {
install := Install{
InstallDirectoryPath: installDirectoryPath,
ShowVersionFormatSimple: ShowVersionFormatSimple,
TargetBinaryFileName: TargetBinaryFileName,
}
cmd := &cobra.Command{
Use: "install (version)",
Short: "install specified version",
Long: `install specified version`,
RunE: install.RunE,
}
cmd.PersistentFlags().BoolP("list", "l", false, "show install list flag")
parentCmd.AddCommand(cmd)
return install
}

func (c Install) RunE(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

//c.installOptions.ShowVersionList {
if list, err := cmd.PersistentFlags().GetBool("list"); err == nil && list {
if err := c.showVersion(ctx); err != nil {
return err
}
return nil
}

if len(args) >= 1 {
err := c.installVersion(ctx, args[0])
if err != nil {
return err
}
return nil
}

return errors.New("requires a installing version or some flags")
}

func (i Install) showVersion(ctx context.Context) error {
versions, err := protoc_gen_go.GetprotocGenGoRepoGoVersions(ctx)
if err != nil {
return err
}
for _, version := range versions {
fmt.Printf(i.ShowVersionFormatSimple, version)
}
return nil
}

func (i Install) installVersion(ctx context.Context, version string) error {
url, err := protoc_gen_go.GetprotocGenGoRepoGoGetReleaseAssetURL(ctx, version, runtime.GOOS, runtime.GOARCH)
if err != nil {
return err
}
targetDirPath := filepath.Join(i.InstallDirectoryPath, "versions", version)
err = os.MkdirAll(targetDirPath, os.ModePerm)
if err != nil {
return err
}
filePath, err := installer.GetTargetFile(url, i.TargetBinaryFileName, targetDirPath)
if err != nil {
return err
}
fmt.Printf("installed %s %s\n", i.TargetBinaryFileName, filePath)
if err := setVersion(getVersionsPath(i.InstallDirectoryPath), getGlobalVersionFilePath(i.InstallDirectoryPath), version); err != nil {
return err
}
return nil
}
46 changes: 46 additions & 0 deletions cmd/commands/plugins/commands/protoc-gen-go/commands/local.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package commands

import (
"fmt"
"github.com/spf13/cobra"
"os"
)

type Local struct {
InstallDirectoryPath string
ShowVersionFormatSimple string
TargetBinaryFileName string
}

func NewLocal(parentCmd *cobra.Command, installDirectoryPath string, ShowVersionFormatSimple string, TargetBinaryFileName string) Local {
local := Local{
InstallDirectoryPath: installDirectoryPath,
ShowVersionFormatSimple: ShowVersionFormatSimple,
TargetBinaryFileName: TargetBinaryFileName,
}
cmd := &cobra.Command{
Use: "local",
Short: fmt.Sprintf("Set or show the local %s version", TargetBinaryFileName),
Long: fmt.Sprintf(`Set or show the local %s version`, TargetBinaryFileName),
RunE: local.RunE,
}
parentCmd.AddCommand(cmd)
return local
}

func (i *Local) RunE(cmd *cobra.Command, args []string) error {
currentDirectory, err := os.Getwd()
if err != nil {
return err
}
localVersionFilePath := getLocalVersionFilePath(currentDirectory)
if len(args) > 0 {
return setVersion(getVersionsPath(i.InstallDirectoryPath), localVersionFilePath, args[0])
}
if v, err := getVersion(localVersionFilePath); err != nil {
return err
} else {
fmt.Printf(i.ShowVersionFormatSimple, v)
}
return nil
}
Loading

0 comments on commit 2d47558

Please sign in to comment.