diff --git a/ignite/internal/tools/gen-mig-diffs/cmd/root.go b/ignite/internal/tools/gen-mig-diffs/cmd/root.go index 0f3061c227..4ff10199e0 100644 --- a/ignite/internal/tools/gen-mig-diffs/cmd/root.go +++ b/ignite/internal/tools/gen-mig-diffs/cmd/root.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "os" "path/filepath" "github.com/Masterminds/semver/v3" @@ -93,7 +94,11 @@ func NewRootCmd() *cobra.Command { } // Scaffold the default commands for each version. - scaffoldOptions := make([]scaffold.Options, 0) + scaffoldOptions := []scaffold.Option{ + scaffold.WithStderr(os.Stderr), + scaffold.WithStdout(os.Stdout), + scaffold.WithStdin(os.Stdin), + } if scaffoldOutput != "" { scaffoldOptions = append(scaffoldOptions, scaffold.WithOutput(scaffoldOutput)) } diff --git a/ignite/internal/tools/gen-mig-diffs/go.mod b/ignite/internal/tools/gen-mig-diffs/go.mod index 931888d888..613bc863be 100644 --- a/ignite/internal/tools/gen-mig-diffs/go.mod +++ b/ignite/internal/tools/gen-mig-diffs/go.mod @@ -80,7 +80,6 @@ require ( github.com/spf13/pflag v1.0.5 // indirect github.com/xanzy/ssh-agent v0.3.3 // indirect golang.org/x/crypto v0.22.0 // indirect - golang.org/x/exp v0.0.0-20240404231335-c0f41cb1a7a0 // indirect golang.org/x/mod v0.17.0 // indirect golang.org/x/net v0.24.0 // indirect golang.org/x/sync v0.7.0 // indirect diff --git a/ignite/internal/tools/gen-mig-diffs/go.sum b/ignite/internal/tools/gen-mig-diffs/go.sum index 0587461407..fc3c682d60 100644 --- a/ignite/internal/tools/gen-mig-diffs/go.sum +++ b/ignite/internal/tools/gen-mig-diffs/go.sum @@ -226,8 +226,6 @@ golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2Uz golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30= golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M= -golang.org/x/exp v0.0.0-20240404231335-c0f41cb1a7a0 h1:985EYyeCOxTpcgOTJpflJUwOeEz0CQOdPt73OzpE9F8= -golang.org/x/exp v0.0.0-20240404231335-c0f41cb1a7a0/go.mod h1:/lliqkxwWAhPjf5oSOIJup2XcqJaw8RGS6k3TGEc7GI= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= diff --git a/ignite/internal/tools/gen-mig-diffs/pkg/repo/repo.go b/ignite/internal/tools/gen-mig-diffs/pkg/repo/repo.go index 8eeee4f1ca..b267718f48 100644 --- a/ignite/internal/tools/gen-mig-diffs/pkg/repo/repo.go +++ b/ignite/internal/tools/gen-mig-diffs/pkg/repo/repo.go @@ -335,7 +335,7 @@ func validateVersionRange(fromVer, toVer *semver.Version, versions semver.Collec } // Picking default values for fromVer and toVer such that: - // If both fromVer and toVer are not provided, then generate migration document for second last and last semver tags + // If both fromVer and toVer are not provided, then generate migration document for second last and last semver major tags // If only fromVer is not provided, then use the tag before toVer as fromVer // If only toVer is not provided, then use the last tag as toVer if toVer != nil { @@ -352,13 +352,17 @@ func validateVersionRange(fromVer, toVer *semver.Version, versions semver.Collec return nil, nil, errors.Errorf("tag %s not found", fromVer) } } else { - sort.Search(versions.Len(), func(i int) bool { - if versions[i].LessThan(toVer) { - fromVer = versions[i] - return false + // Find the last major release version. + sort.Sort(sort.Reverse(versions)) + for _, ver := range versions { + if ver.Major() < toVer.Major() { + fromVer = ver + break } - return true - }) + } + if fromVer == nil { + return nil, nil, errors.Errorf("can't find an older major release from %s", toVer.Original()) + } } // Unable to generate migration document if fromVer is greater or equal to toVer diff --git a/ignite/internal/tools/gen-mig-diffs/pkg/scaffold/scaffold.go b/ignite/internal/tools/gen-mig-diffs/pkg/scaffold/scaffold.go index db7affe7eb..4156c884f1 100644 --- a/ignite/internal/tools/gen-mig-diffs/pkg/scaffold/scaffold.go +++ b/ignite/internal/tools/gen-mig-diffs/pkg/scaffold/scaffold.go @@ -2,6 +2,7 @@ package scaffold import ( "context" + "io" "os" "path/filepath" "strings" @@ -9,6 +10,7 @@ import ( "github.com/Masterminds/semver/v3" "github.com/ignite/cli/v29/ignite/pkg/cmdrunner/exec" + "github.com/ignite/cli/v29/ignite/pkg/cmdrunner/step" "github.com/ignite/cli/v29/ignite/pkg/errors" "github.com/ignite/cli/v29/ignite/pkg/randstr" @@ -26,22 +28,28 @@ type ( cache *cache.Cache cachePath string commandList Commands + stdout io.Writer + stderr io.Writer + stdin io.Reader } - // options represents configuration for the generator. - options struct { + // option represents configuration for the generator. + option struct { cachePath string output string commands Commands + stdout io.Writer + stderr io.Writer + stdin io.Reader } - // Options configures the generator. - Options func(*options) + // Option configures the generator. + Option func(*option) ) -// newOptions returns a options with default options. -func newOptions() options { +// newOptions returns a option with default option. +func newOptions() option { tmpDir := filepath.Join(os.TempDir(), randstr.Runes(4)) - return options{ + return option{ cachePath: filepath.Join(tmpDir, "migration-cache"), output: filepath.Join(tmpDir, "migration"), commands: defaultCommands, @@ -49,28 +57,39 @@ func newOptions() options { } // WithOutput set the ignite scaffold Output. -func WithOutput(output string) Options { - return func(o *options) { +func WithOutput(output string) Option { + return func(o *option) { o.output = output } } // WithCachePath set the ignite scaffold cache path. -func WithCachePath(cachePath string) Options { - return func(o *options) { +func WithCachePath(cachePath string) Option { + return func(o *option) { o.cachePath = cachePath } } -// WithCommandList set the migration docs Output. -func WithCommandList(commands Commands) Options { - return func(o *options) { - o.commands = commands +func WithStdout(w io.Writer) Option { + return func(o *option) { + o.stdout = w + } +} + +func WithStderr(w io.Writer) Option { + return func(o *option) { + o.stderr = w + } +} + +func WithStdin(r io.Reader) Option { + return func(o *option) { + o.stdin = r } } // New returns a new Scaffold. -func New(binary string, ver *semver.Version, options ...Options) (*Scaffold, error) { +func New(binary string, ver *semver.Version, options ...Option) (*Scaffold, error) { opts := newOptions() for _, apply := range options { apply(&opts) @@ -91,6 +110,9 @@ func New(binary string, ver *semver.Version, options ...Options) (*Scaffold, err } return &Scaffold{ + stdout: opts.stdout, + stderr: opts.stderr, + stdin: opts.stdin, binary: binary, version: ver, cache: c, @@ -157,7 +179,13 @@ func (s *Scaffold) executeScaffold(ctx context.Context, cmd, path string) error args = append(args, "--path", path) args = applyPreExecuteExceptions(s.version, args) - if err := exec.Exec(ctx, args); err != nil { + if err := exec.Exec( + ctx, + args, + exec.StepOption(step.Stdout(s.stdout)), + exec.StepOption(step.Stderr(s.stderr)), + exec.StepOption(step.Stdin(s.stdin)), + ); err != nil { return errors.Wrapf(err, "failed to execute ignite scaffold command: %s", cmd) } return nil