Skip to content

Commit

Permalink
Use go list cmd to find ignite cli package (#3666)
Browse files Browse the repository at this point in the history
Co-authored-by: Danilo Pantani <[email protected]>
  • Loading branch information
Ehsan-saradar and Pantani authored Sep 27, 2023
1 parent b0f75fa commit f210829
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
19 changes: 19 additions & 0 deletions ignite/pkg/gocmd/gocmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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, " ")
Expand Down
13 changes: 13 additions & 0 deletions ignite/pkg/gocmd/gocmd_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package gocmd_test

import (
"context"
"errors"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -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")
}
9 changes: 6 additions & 3 deletions integration/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit f210829

Please sign in to comment.