From f2108290c0014a043c9f08f4f4e9c461697d4498 Mon Sep 17 00:00:00 2001 From: Ehsan-saradar Date: Wed, 27 Sep 2023 10:46:16 +0300 Subject: [PATCH] Use go list cmd to find ignite cli package (#3666) Co-authored-by: Danilo Pantani --- go.sum | 2 -- ignite/pkg/gocmd/gocmd.go | 19 +++++++++++++++++++ ignite/pkg/gocmd/gocmd_test.go | 13 +++++++++++++ integration/env.go | 9 ++++++--- 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/go.sum b/go.sum index 9d3d5e374a..6baf08233d 100644 --- a/go.sum +++ b/go.sum @@ -1354,8 +1354,6 @@ golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2 golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk= -golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20180719180050-a680a1efc54d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= diff --git a/ignite/pkg/gocmd/gocmd.go b/ignite/pkg/gocmd/gocmd.go index 657d0490f9..f9a99c7330 100644 --- a/ignite/pkg/gocmd/gocmd.go +++ b/ignite/pkg/gocmd/gocmd.go @@ -39,6 +39,9 @@ const ( // CommandEnv represents go "env" command. CommandEnv = "env" + // CommandList represents go "list" command. + CommandList = "list" + // EnvGOARCH represents GOARCH variable. EnvGOARCH = "GOARCH" // EnvGOMOD represents GOMOD variable. @@ -168,6 +171,22 @@ func Get(ctx context.Context, path string, pkgs []string, options ...exec.Option return exec.Exec(ctx, command, append(options, exec.StepOption(step.Workdir(path)))...) } +// List returns the list of packages in path. +func List(ctx context.Context, path string, flags []string, options ...exec.Option) ([]string, error) { + command := []string{ + Name(), + CommandList, + } + command = append(command, flags...) + var b bytes.Buffer + err := exec.Exec(ctx, command, + append(options, exec.StepOption(step.Workdir(path)), exec.StepOption(step.Stdout(&b)))...) + if err != nil { + return nil, err + } + return strings.Fields(b.String()), nil +} + // Ldflags returns a combined ldflags set from flags. func Ldflags(flags ...string) string { return strings.Join(flags, " ") diff --git a/ignite/pkg/gocmd/gocmd_test.go b/ignite/pkg/gocmd/gocmd_test.go index 1f13e41f90..e1baae53a9 100644 --- a/ignite/pkg/gocmd/gocmd_test.go +++ b/ignite/pkg/gocmd/gocmd_test.go @@ -1,7 +1,9 @@ package gocmd_test import ( + "context" "errors" + "os" "testing" "github.com/stretchr/testify/assert" @@ -16,3 +18,14 @@ func TestIsInstallError(t *testing.T) { go get github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2`) assert.True(t, gocmd.IsInstallError(err)) } + +func TestList(t *testing.T) { + wd, err := os.Getwd() + assert.NoError(t, err) + + ctx := context.Background() + packages, err := gocmd.List(ctx, wd, []string{"-m", "-f={{.Path}}", "github.com/ignite/cli"}) + assert.NoError(t, err) + + assert.Contains(t, packages, "github.com/ignite/cli") +} diff --git a/integration/env.go b/integration/env.go index 95b241bdfa..394a818464 100644 --- a/integration/env.go +++ b/integration/env.go @@ -20,7 +20,6 @@ import ( "github.com/ignite/cli/ignite/pkg/cosmosfaucet" "github.com/ignite/cli/ignite/pkg/env" "github.com/ignite/cli/ignite/pkg/gocmd" - "github.com/ignite/cli/ignite/pkg/gomodulepath" "github.com/ignite/cli/ignite/pkg/httpstatuschecker" "github.com/ignite/cli/ignite/pkg/xurl" ) @@ -71,10 +70,14 @@ func compileBinary(ctx context.Context) { if err != nil { panic(fmt.Sprintf("unable to get working dir: %v", err)) } - _, appPath, err := gomodulepath.Find(wd) + pkgs, err := gocmd.List(ctx, wd, []string{"-m", "-f={{.Dir}}", "github.com/ignite/cli"}) if err != nil { - panic(fmt.Sprintf("unable to read go module path: %v", err)) + panic(fmt.Sprintf("unable to list ignite cli package: %v", err)) } + if len(pkgs) != 1 { + panic(fmt.Sprintf("expected only one package, got %d", len(pkgs))) + } + appPath := pkgs[0] var ( output, binary = filepath.Split(IgniteApp) path = path.Join(appPath, "ignite", "cmd", "ignite")