-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
552 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
62
cmd/commands/plugins/commands/protoc-gen-go/commands/global.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
35
cmd/commands/plugins/commands/protoc-gen-go/commands/init_cmd.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
91
cmd/commands/plugins/commands/protoc-gen-go/commands/install.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
46
cmd/commands/plugins/commands/protoc-gen-go/commands/local.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.