Skip to content

Commit

Permalink
setup: add search for current gopath and return detailed error
Browse files Browse the repository at this point in the history
  • Loading branch information
wineguo authored and WineChord committed May 17, 2024
1 parent 2f827d3 commit 40e6ffc
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## v1.0.9 2024-05-09

- setup: add search for current gopath and return detailed error (#40)

## v1.0.8 2024-05-09

- fix: url reference error when generating trpc-cpp project (#38)
Expand Down
4 changes: 2 additions & 2 deletions cmd/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func CMD() *cobra.Command {
// Load dependencies according to languages specified.
deps, err := config.LoadDependencies(languages...)
if err != nil {
return err
return fmt.Errorf("load dependencies failed: %w", err)
}
// Setup dependencies.
return config.SetupDependencies(deps)
Expand All @@ -52,7 +52,7 @@ func CMD() *cobra.Command {
}
// Do setup according to language.
if err := setup(lang); err != nil {
return fmt.Errorf("setup failed: %v", err)
return fmt.Errorf("setup failed: %w", err)
}
log.Info("Setup completed")
return nil
Expand Down
50 changes: 47 additions & 3 deletions config/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ package config

import (
"fmt"
"go/build"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"

"trpc.group/trpc-go/trpc-cmdline/util/log"
)
Expand Down Expand Up @@ -69,11 +72,10 @@ func LoadDependencies(languages ...string) ([]*Dependency, error) {

// SetupDependencies configures dependency installation.
func SetupDependencies(deps []*Dependency) error {
userHome, err := os.UserHomeDir()
path, err := getCandidate()
if err != nil {
return fmt.Errorf("get user home dir err for installation: %w", err)
return err
}
path := filepath.Join(userHome, "go", "bin")
for _, dep := range deps {
// Check whether installed, if no, try to install it.
if dep.Installed() {
Expand Down Expand Up @@ -108,3 +110,45 @@ func SetupDependencies(deps []*Dependency) error {
}
return nil
}

func getCandidate() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("install dependency error: %w", err)
}
cmd := exec.Command("go", "env", "GOPATH")
output, _ := cmd.Output()
gopath := strings.TrimSpace(string(output))
candidates := []string{
filepath.Join(gopath, "bin"),
filepath.Join(build.Default.GOPATH, "bin"),
filepath.Join(home, "go", "bin"),
filepath.Join(home, "bin"),
}
dest := candidates[0]
path := os.ExpandEnv(os.Getenv("PATH"))
var found bool
for _, p := range candidates {
// Must be a directory to avoid interference from files with the same name.
fin, err := os.Lstat(p)
if err != nil {
continue
}
if !fin.IsDir() {
continue
}

// Should be searchable in `PATH`.
if !strings.Contains(path, p) {
continue
}
dest = p
found = true
break
}
if !found {
return "", fmt.Errorf("get install path error: %s does not exist or cannot be found under $PATH, "+
"consider creating it manually and adding to $PATH", dest)
}
return dest, nil
}
2 changes: 1 addition & 1 deletion config/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
package config

// TRPCCliVersion is the version of trpc-cmdline tool.
var TRPCCliVersion = "v1.0.8"
var TRPCCliVersion = "v1.0.9"
2 changes: 1 addition & 1 deletion gobin/assets.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion install/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.0.8
v1.0.9

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions install/submodules/trpc-protocol/pb/go/trpc/api/http.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions install/submodules/trpc-protocol/pb/go/trpc/trpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions parser/fill.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"go.uber.org/multierr"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"

annotations "trpc.group/trpc/trpc-protocol/pb/go/trpc/api"
trpc "trpc.group/trpc/trpc-protocol/pb/go/trpc/proto"
"trpc.group/trpc/trpc-protocol/pb/go/trpc/swagger"
Expand Down
1 change: 1 addition & 0 deletions plugin/goimports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"testing"

"github.com/stretchr/testify/require"

"trpc.group/trpc-go/trpc-cmdline/descriptor"
"trpc.group/trpc-go/trpc-cmdline/params"
"trpc.group/trpc-go/trpc-cmdline/plugin"
Expand Down
1 change: 1 addition & 0 deletions plugin/gotag.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/jhump/protoreflect/desc"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/descriptorpb"

trpc "trpc.group/trpc/trpc-protocol/pb/go/trpc/proto"

"trpc.group/trpc-go/trpc-cmdline/descriptor"
Expand Down
1 change: 1 addition & 0 deletions util/apidocs/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"strings"

"github.com/hashicorp/go-multierror"

"trpc.group/trpc-go/trpc-cmdline/descriptor"
"trpc.group/trpc-go/trpc-cmdline/params"
)
Expand Down

0 comments on commit 40e6ffc

Please sign in to comment.