Skip to content

Commit

Permalink
Add some fix
Browse files Browse the repository at this point in the history
  • Loading branch information
shiv3 committed Jul 29, 2021
1 parent 8b96551 commit 0c2341c
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 131 deletions.
5 changes: 0 additions & 5 deletions adapter/github/protoc/protobuf.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goinstall
package gomodule

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goinstall
package gomodule

import (
"context"
Expand Down
31 changes: 31 additions & 0 deletions adapter/gomodule/go_version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package gomodule

import (
"context"
"encoding/json"
"fmt"
"os/exec"
)

type jsonOut struct {
Version string
Versions []string
GoVersion string
}

func GoGetVersions(ctx context.Context, target string) ([]string, error) {
cmdString := []string{
"go", "list", "-u", "-m", "-versions", "-json", target,
}
cmd := exec.Command(cmdString[0], cmdString[1:]...)
//fmt.Printf("running...\n%s \n", strings.Join(cmdString, " "))
out, err := cmd.Output()
if err != nil {
if ee, ok := err.(*exec.ExitError); ok {
fmt.Printf("error:\n%s", ee.Stderr)
}
}
var v jsonOut
err = json.Unmarshal(out, &v)
return v.Versions, err
}
24 changes: 14 additions & 10 deletions cmd/commands/plugins/commands/protoc-gen-go/commands/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,31 @@ import (
"errors"
"fmt"

"github.com/shiv3/protoenv/adapter/goinstall"
"github.com/shiv3/protoenv/adapter/gomodule"

protoc_gen_go "github.com/shiv3/protoenv/adapter/github/protoc-gen-go"
"github.com/shiv3/protoenv/domain/installer"

"github.com/spf13/cobra"
)

type Install struct {
InstallDirectoryPath string
ShowVersionFormatSimple string
TargetBinaryFileName string
TargetUrl string
installer installer.Installer
}

func NewInstall(parentCmd *cobra.Command, installDirectoryPath string, ShowVersionFormatSimple string, TargetBinaryFileName string) Install {
install := Install{
InstallDirectoryPath: installDirectoryPath,
ShowVersionFormatSimple: ShowVersionFormatSimple,
TargetBinaryFileName: TargetBinaryFileName,
TargetUrl: "google.golang.org/protobuf",
installer: installer.NewInstaller(installer.InstallTypeGoInstall, installer.InstallConfig{
TargetUrl: "google.golang.org/protobuf",
TargetVersion: "",
TargetPath: installDirectoryPath,
TargetBinaryName: TargetBinaryFileName,
}),
}
cmd := &cobra.Command{
Use: "install (version)",
Expand Down Expand Up @@ -58,7 +65,7 @@ func (i Install) RunE(cmd *cobra.Command, args []string) error {
}

func (i Install) showVersion(ctx context.Context) error {
versions, err := protoc_gen_go.GetprotocGenGoRepoGoVersions(ctx)
versions, err := gomodule.GoGetVersions(ctx, i.TargetUrl)
if err != nil {
return err
}
Expand All @@ -69,9 +76,6 @@ func (i Install) showVersion(ctx context.Context) error {
}

func (i Install) installVersion(ctx context.Context, version string) error {
_, err := goinstall.GoInstall(ctx, "google.golang.org/protobuf", version, i.InstallDirectoryPath)
if err != nil {
return err
}
return nil
i.installer.SetVersion(version)
return i.installer.Install(ctx)
}
41 changes: 15 additions & 26 deletions cmd/commands/protoc/commands/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,32 @@ import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"runtime"

"github.com/shiv3/protoenv/adapter/archiver"
"github.com/shiv3/protoenv/adapter/github/protoc"
"github.com/shiv3/protoenv/adapter/github"

"github.com/shiv3/protoenv/domain/installer"

"github.com/spf13/cobra"
)

type Install struct {
InstallDirectoryPath string
ShowVersionFormatSimple string
TargetBinaryFileName string
TargetUrl string
installer installer.Installer
}

func NewInstall(parentCmd *cobra.Command, installDirectoryPath string, ShowVersionFormatSimple string, TargetBinaryFileName string) Install {
install := Install{
InstallDirectoryPath: installDirectoryPath,
ShowVersionFormatSimple: ShowVersionFormatSimple,
TargetBinaryFileName: TargetBinaryFileName,
TargetUrl: "github.com/protocolbuffers/protobuf",
installer: installer.NewInstaller(installer.InstallTypeGitHubReleaseZip, installer.InstallConfig{
TargetUrl: "github.com/protocolbuffers/protobuf",
TargetVersion: "",
TargetPath: installDirectoryPath,
TargetBinaryName: TargetBinaryFileName,
}),
}
cmd := &cobra.Command{
Use: "install (version)",
Expand Down Expand Up @@ -60,7 +65,7 @@ func (c Install) RunE(cmd *cobra.Command, args []string) error {
}

func (i Install) showVersion(ctx context.Context) error {
versions, err := protoc.GetProtobufVersions(ctx)
versions, err := github.GetReleaseVersions(ctx, i.TargetUrl)
if err != nil {
return err
}
Expand All @@ -71,22 +76,6 @@ func (i Install) showVersion(ctx context.Context) error {
}

func (i Install) installVersion(ctx context.Context, version string) error {
url, err := protoc.GetProtobufGetReleaseAssetURL(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 := archiver.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
i.installer.SetVersion(version)
return i.installer.Install(ctx)
}
19 changes: 11 additions & 8 deletions domain/installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ const (
InstallTypeGitHubReleaseZip InstallType = "github_release_zip"
)

func NewInstallers(installConfig InstallConfig) map[InstallType]Installer {
return map[InstallType]Installer{
InstallTypeGoInstall: NewInstallerGoInstall(InstallTypeGoInstall, installConfig),
InstallTypeGitHubReleaseZip: NewInstallerGithubReleaseZip(InstallTypeGitHubReleaseZip, installConfig),
func NewInstaller(installType InstallType, installConfig InstallConfig) Installer {
switch installType {
case InstallTypeGoInstall:
return NewInstallerGoInstall(InstallTypeGoInstall, installConfig)
case InstallTypeGitHubReleaseZip:
return NewInstallerGithubReleaseZip(InstallTypeGitHubReleaseZip, installConfig)
}
}

type InstallOption interface {
return nil
}

type Installer interface {
Install(ctx context.Context) error
Install(ctx context.Context, options ...InstallOption) error
SetVersion(version string)
}
type InstallOption struct {
}

type InstallConfig struct {
Expand Down
6 changes: 5 additions & 1 deletion domain/installer/installer_github_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewInstallerGithubReleaseZip(installType InstallType, installConfig Install
return &InstallerGithubReleaseZip{InstallType: installType, InstallConfig: installConfig}
}

func (i *InstallerGithubReleaseZip) Install(ctx context.Context) error {
func (i *InstallerGithubReleaseZip) Install(ctx context.Context, options ...InstallOption) error {
url, err := getReleaseAssetURL(ctx, i.InstallConfig.TargetUrl, i.InstallConfig.TargetVersion, i.ArchMatcher)
if err != nil {
return err
Expand Down Expand Up @@ -56,3 +56,7 @@ func archMarch(os, arch string, matcher map[string]string) string {
}
return ""
}

func (i *InstallerGithubReleaseZip) SetVersion(version string) {
i.InstallConfig.TargetVersion = version
}
3 changes: 1 addition & 2 deletions domain/installer/installer_github_release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ func TestInstallerGithubReleaseZip_Install(t *testing.T) {
InstallConfig InstallConfig
}
type args struct {
ctx context.Context
option []InstallOption
ctx context.Context
}
tests := []struct {
name string
Expand Down
10 changes: 7 additions & 3 deletions domain/installer/installer_go_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package installer
import (
"context"

"github.com/shiv3/protoenv/adapter/goinstall"
"github.com/shiv3/protoenv/adapter/gomodule"
)

type InstallerGoInstall struct {
Expand All @@ -15,6 +15,10 @@ func NewInstallerGoInstall(installType InstallType, installConfig InstallConfig)
return &InstallerGoInstall{InstallType: installType, InstallConfig: installConfig}
}

func (i *InstallerGoInstall) Install(ctx context.Context) error {
return goinstall.GoInstall(ctx, i.InstallConfig.TargetUrl, i.InstallConfig.TargetVersion, i.InstallConfig.TargetPath)
func (i *InstallerGoInstall) Install(ctx context.Context, options ...InstallOption) error {
return gomodule.GoInstall(ctx, i.InstallConfig.TargetUrl, i.InstallConfig.TargetVersion, i.InstallConfig.TargetPath)
}

func (i *InstallerGoInstall) SetVersion(version string) {
i.InstallConfig.TargetVersion = version
}
6 changes: 2 additions & 4 deletions domain/installer/installer_go_install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ func TestInstallerGoInstall_Install(t *testing.T) {
InstallConfig InstallConfig
}
type args struct {
ctx context.Context
option []InstallOption
ctx context.Context
}
tests := []struct {
name string
Expand All @@ -44,8 +43,7 @@ func TestInstallerGoInstall_Install(t *testing.T) {
},
},
args: args{
ctx: nil,
option: nil,
ctx: nil,
},
wantErr: false,
},
Expand Down
69 changes: 0 additions & 69 deletions domain/installer/installer_test.go

This file was deleted.

1 change: 0 additions & 1 deletion domain/installer/installer_type.go

This file was deleted.

7 changes: 7 additions & 0 deletions example/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM golang:1.16

WORKDIR /
RUN go install github.com/shiv3/protoenv@latest

RUN protoenv protoc install v3.17.3
RUN protoenv plugins protoc-gen-go install v1.20.0

0 comments on commit 0c2341c

Please sign in to comment.