Skip to content

Commit

Permalink
feat: by default generate the migration docs from a major version to …
Browse files Browse the repository at this point in the history
…another (#4153)

* by default generate the migration docs from a major version to another

* add std out in and err for scaffold commands

* add std to scaffold runner

---------

Co-authored-by: Pantani <Pantani>
  • Loading branch information
Pantani committed May 21, 2024
1 parent c22073b commit f5a502b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 28 deletions.
7 changes: 6 additions & 1 deletion ignite/internal/tools/gen-mig-diffs/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"os"
"path/filepath"

"github.com/Masterminds/semver/v3"
Expand Down Expand Up @@ -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))
}
Expand Down
1 change: 0 additions & 1 deletion ignite/internal/tools/gen-mig-diffs/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions ignite/internal/tools/gen-mig-diffs/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
18 changes: 11 additions & 7 deletions ignite/internal/tools/gen-mig-diffs/pkg/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
62 changes: 45 additions & 17 deletions ignite/internal/tools/gen-mig-diffs/pkg/scaffold/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package scaffold

import (
"context"
"io"
"os"
"path/filepath"
"strings"

"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"

Expand All @@ -26,51 +28,68 @@ 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,
}
}

// 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)
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f5a502b

Please sign in to comment.