From 8a00ebc4827be61ebfbfd88f55f1e7ae4d13e0ff Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Mon, 15 May 2023 16:06:35 +0200 Subject: [PATCH 01/15] create method to update go imports into a go file --- ignite/pkg/cosmosgen/tools.go | 25 +++ ignite/pkg/goanalysis/goanalysis.go | 39 +++- ignite/pkg/goanalysis/goanalysis_test.go | 233 +++++++++++++++++++++++ 3 files changed, 296 insertions(+), 1 deletion(-) create mode 100644 ignite/pkg/cosmosgen/tools.go diff --git a/ignite/pkg/cosmosgen/tools.go b/ignite/pkg/cosmosgen/tools.go new file mode 100644 index 0000000000..63b61a9c36 --- /dev/null +++ b/ignite/pkg/cosmosgen/tools.go @@ -0,0 +1,25 @@ +package cosmosgen + +import ( + "go/ast" + + "github.com/ignite/cli/ignite/pkg/goanalysis" +) + +func MissingTools(f *ast.File) (missingTools []string) { + imports := make(map[string]string) + for name, imp := range goanalysis.FormatImports(f) { + imports[imp] = name + } + + for _, tool := range DepTools() { + if _, ok := imports[tool]; !ok { + missingTools = append(missingTools, tool) + } + } + return +} + +func UpgradeTools(f *ast.File, addImports, removeImports []string) error { + return nil +} diff --git a/ignite/pkg/goanalysis/goanalysis.go b/ignite/pkg/goanalysis/goanalysis.go index 18af2fc939..215d97eef4 100644 --- a/ignite/pkg/goanalysis/goanalysis.go +++ b/ignite/pkg/goanalysis/goanalysis.go @@ -2,14 +2,19 @@ package goanalysis import ( + "bytes" "errors" "fmt" "go/ast" + "go/format" "go/parser" "go/token" "os" "path/filepath" + "strconv" "strings" + + "golang.org/x/tools/go/ast/astutil" ) const ( @@ -199,7 +204,7 @@ func FormatImports(f *ast.File) map[string]string { m := make(map[string]string) // name -> import for _, imp := range f.Imports { var importName string - if imp.Name != nil { + if imp.Name != nil && imp.Name.Name != "_" && imp.Name.Name != "." { importName = imp.Name.Name } else { importParts := strings.Split(imp.Path.Value, "/") @@ -211,3 +216,35 @@ func FormatImports(f *ast.File) map[string]string { } return m } + +// ImportExists helper function to check if an import exists in the *ast.File +func ImportExists(file *ast.File, imp string) bool { + for _, i := range file.Imports { + if i.Path.Value == strconv.Quote(imp) { + return true + } + } + return false +} + +// UpdateInitImports helper function to remove and add imports to an *ast.File +func UpdateInitImports(file *ast.File, importsToAdd, importsToRemove []string) ([]byte, error) { + fset := token.NewFileSet() + for _, imp := range importsToRemove { + astutil.DeleteNamedImport(fset, file, "_", imp) + astutil.DeleteImport(fset, file, imp) + } + + for _, imp := range importsToAdd { + if !ImportExists(file, imp) { + astutil.AddNamedImport(fset, file, "_", imp) + } + } + + // Format the modified AST + var buf bytes.Buffer + if err := format.Node(&buf, fset, file); err != nil { + return nil, fmt.Errorf("failed to format file: %v", err) + } + return buf.Bytes(), nil +} diff --git a/ignite/pkg/goanalysis/goanalysis_test.go b/ignite/pkg/goanalysis/goanalysis_test.go index 38462382ac..ed27cc4a49 100644 --- a/ignite/pkg/goanalysis/goanalysis_test.go +++ b/ignite/pkg/goanalysis/goanalysis_test.go @@ -2,14 +2,20 @@ package goanalysis_test import ( "errors" + "go/ast" + "go/parser" + "go/token" "os" "path/filepath" + "sort" "testing" "github.com/stretchr/testify/require" "github.com/ignite/cli/ignite/pkg/goanalysis" "github.com/ignite/cli/ignite/pkg/xast" + + "golang.org/x/tools/go/ast/astutil" ) var MainFile = []byte(`package main`) @@ -231,3 +237,230 @@ func TestFuncVarExists(t *testing.T) { }) } } + +func TestFormatImports(t *testing.T) { + tests := []struct { + name string + input *ast.File + want map[string]string + }{ + { + name: "Test one import", + input: &ast.File{ + Imports: []*ast.ImportSpec{ + { + Path: &ast.BasicLit{ + Kind: token.STRING, + Value: "\"fmt\"", + }, + }, + }, + }, + want: map[string]string{ + "fmt": "fmt", + }, + }, + { + name: "Test underscore import", + input: &ast.File{ + Imports: []*ast.ImportSpec{ + { + Path: &ast.BasicLit{ + Kind: token.STRING, + Value: "\"net/http\"", + }, + }, + { + Name: &ast.Ident{ + Name: "_", + }, + Path: &ast.BasicLit{ + Kind: token.STRING, + Value: "\"github.com/example/pkg\"", + }, + }, + }, + }, + want: map[string]string{ + "http": "net/http", + "pkg": "github.com/example/pkg", + }, + }, + { + name: "Test dot import", + input: &ast.File{ + Imports: []*ast.ImportSpec{ + { + Path: &ast.BasicLit{ + Kind: token.STRING, + Value: "\"net/http\"", + }, + }, + { + Name: &ast.Ident{ + Name: ".", + }, + Path: &ast.BasicLit{ + Kind: token.STRING, + Value: "\"github.com/example/pkg\"", + }, + }, + { + Path: &ast.BasicLit{ + Kind: token.STRING, + Value: "\"fmt\"", + }, + }, + }, + }, + want: map[string]string{ + "http": "net/http", + "pkg": "github.com/example/pkg", + "fmt": "fmt", + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + require.Equal(t, tt.want, goanalysis.FormatImports(tt.input)) + }) + } +} + +func TestUpdateInitImports(t *testing.T) { + type args struct { + fileImports []string + importsToAdd []string + importsToRemove []string + } + tests := []struct { + name string + args args + want []string + err error + }{ + { + name: "test one import to add", + args: args{ + fileImports: []string{"fmt"}, + importsToAdd: []string{"net/http"}, + }, + want: []string{"fmt", "net/http"}, + }, + { + name: "test one import to remove", + args: args{ + fileImports: []string{"fmt", "net/http"}, + importsToRemove: []string{"net/http"}, + }, + want: []string{"fmt"}, + }, + { + name: "test one import to add and remove", + args: args{ + fileImports: []string{"fmt"}, + importsToAdd: []string{"net/http"}, + importsToRemove: []string{"fmt"}, + }, + want: []string{"net/http"}, + }, + { + name: "test many imports", + args: args{ + fileImports: []string{ + "errors", + "github.com/stretchr/testify/require", + "go/ast", + "go/parser", + "go/token", + "os", + "path/filepath", + "testing", + }, + importsToAdd: []string{"net/http", "errors"}, + importsToRemove: []string{"go/parser", "path/filepath", "testing"}, + }, + want: []string{ + "errors", + "net/http", + "os", + "go/ast", + "go/token", + "github.com/stretchr/testify/require", + }, + }, + { + name: "test add and remove same imports already exist", + args: args{ + fileImports: []string{ + "errors", + "go/ast", + }, + importsToAdd: []string{ + "errors", + "go/ast", + }, + importsToRemove: []string{ + "errors", + "go/ast", + }, + }, + want: []string{ + "errors", + "go/ast", + }, + }, + { + name: "test add and remove same imports", + args: args{ + fileImports: []string{}, + importsToAdd: []string{ + "errors", + "go/ast", + }, + importsToRemove: []string{ + "errors", + "go/ast", + }, + }, + want: []string{ + "errors", + "go/ast", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Create a sample *ast.File + file := &ast.File{ + Name: ast.NewIdent("main"), + Imports: []*ast.ImportSpec{}, + } + fset := token.NewFileSet() + for _, imp := range tt.args.fileImports { + require.Truef(t, astutil.AddImport(fset, file, imp), "import %s cannot be added", imp) + } + + // test method + got, err := goanalysis.UpdateInitImports(file, tt.args.importsToAdd, tt.args.importsToRemove) + if tt.err != nil { + require.Error(t, err) + require.ErrorIs(t, err, tt.err) + return + } + require.NoError(t, err) + + gotFile, err := parser.ParseFile(token.NewFileSet(), "", got, parser.ParseComments) + require.NoError(t, err) + + gotImports := make([]string, 0) + for _, imp := range goanalysis.FormatImports(gotFile) { + gotImports = append(gotImports, imp) + } + sort.Strings(tt.want) + sort.Strings(gotImports) + require.EqualValues(t, tt.want, gotImports) + }) + } +} From 07724a9c208b68dfeb99b6ae13dc6f2b43fed78e Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Tue, 16 May 2023 00:58:38 +0200 Subject: [PATCH 02/15] add tools helpers --- ignite/pkg/cosmosgen/install.go | 13 +++++++ ignite/pkg/cosmosgen/tools.go | 5 +-- ignite/pkg/cosmosgen/tools_test.go | 54 +++++++++++++++++++++++++++++ ignite/pkg/goanalysis/goanalysis.go | 6 ++-- 4 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 ignite/pkg/cosmosgen/tools_test.go diff --git a/ignite/pkg/cosmosgen/install.go b/ignite/pkg/cosmosgen/install.go index 244ea8bd88..2c41e08d38 100644 --- a/ignite/pkg/cosmosgen/install.go +++ b/ignite/pkg/cosmosgen/install.go @@ -7,6 +7,19 @@ import ( "github.com/ignite/cli/ignite/pkg/gocmd" ) +// UnusedTools deprecated and not necessary tools. +func UnusedTools() []string { + return []string{ + // regen protoc plugin + "github.com/regen-network/cosmos-proto/protoc-gen-gocosmos", + + // old ignite repo. + "github.com/ignite-hq/cli/ignite/pkg/cmdrunner", + "github.com/ignite-hq/cli/ignite/pkg/cmdrunner/step", + } +} + +// DepTools necessary tools to build and run the chain. func DepTools() []string { return []string{ // the gocosmos plugin. diff --git a/ignite/pkg/cosmosgen/tools.go b/ignite/pkg/cosmosgen/tools.go index 63b61a9c36..c087b8f276 100644 --- a/ignite/pkg/cosmosgen/tools.go +++ b/ignite/pkg/cosmosgen/tools.go @@ -6,6 +6,7 @@ import ( "github.com/ignite/cli/ignite/pkg/goanalysis" ) +// MissingTools find missing tools import indo a *ast.File. func MissingTools(f *ast.File) (missingTools []string) { imports := make(map[string]string) for name, imp := range goanalysis.FormatImports(f) { @@ -19,7 +20,3 @@ func MissingTools(f *ast.File) (missingTools []string) { } return } - -func UpgradeTools(f *ast.File, addImports, removeImports []string) error { - return nil -} diff --git a/ignite/pkg/cosmosgen/tools_test.go b/ignite/pkg/cosmosgen/tools_test.go new file mode 100644 index 0000000000..b344f883ac --- /dev/null +++ b/ignite/pkg/cosmosgen/tools_test.go @@ -0,0 +1,54 @@ +package cosmosgen + +import ( + "go/ast" + "go/token" + "strconv" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestMissingTools(t *testing.T) { + tests := []struct { + name string + astFile *ast.File + want []string + }{ + { + name: "no missing tools", + astFile: createASTFileWithImports(DepTools()...), + want: nil, + }, + { + name: "some missing tools", + astFile: createASTFileWithImports("github.com/golang/protobuf/protoc-gen-go", "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway", "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger"), + want: []string{"github.com/cosmos/gogoproto/protoc-gen-gocosmos", "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2"}, + }, + { + name: "all tools missing", + astFile: createASTFileWithImports(), + want: DepTools(), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := MissingTools(tt.astFile) + require.EqualValues(t, tt.want, got) + }) + } +} + +// createASTFileWithImports helper function to create an AST file with given imports. +func createASTFileWithImports(imports ...string) *ast.File { + f := &ast.File{Imports: make([]*ast.ImportSpec, len(imports))} + for i, imp := range imports { + f.Imports[i] = &ast.ImportSpec{ + Path: &ast.BasicLit{ + Kind: token.STRING, + Value: strconv.Quote(imp), + }, + } + } + return f +} diff --git a/ignite/pkg/goanalysis/goanalysis.go b/ignite/pkg/goanalysis/goanalysis.go index 215d97eef4..1cfc827849 100644 --- a/ignite/pkg/goanalysis/goanalysis.go +++ b/ignite/pkg/goanalysis/goanalysis.go @@ -217,7 +217,7 @@ func FormatImports(f *ast.File) map[string]string { return m } -// ImportExists helper function to check if an import exists in the *ast.File +// ImportExists helper function to check if an import exists in the *ast.File. func ImportExists(file *ast.File, imp string) bool { for _, i := range file.Imports { if i.Path.Value == strconv.Quote(imp) { @@ -227,7 +227,7 @@ func ImportExists(file *ast.File, imp string) bool { return false } -// UpdateInitImports helper function to remove and add imports to an *ast.File +// UpdateInitImports helper function to remove and add imports to an *ast.File. func UpdateInitImports(file *ast.File, importsToAdd, importsToRemove []string) ([]byte, error) { fset := token.NewFileSet() for _, imp := range importsToRemove { @@ -241,7 +241,7 @@ func UpdateInitImports(file *ast.File, importsToAdd, importsToRemove []string) ( } } - // Format the modified AST + // Format the modified AST. var buf bytes.Buffer if err := format.Node(&buf, fset, file); err != nil { return nil, fmt.Errorf("failed to format file: %v", err) From 7ff1fe4f46ecfddf49496ef6c5e2333df27326da Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Tue, 16 May 2023 02:20:55 +0200 Subject: [PATCH 03/15] add migration pre handler --- ignite/cmd/chain.go | 64 ++++++++++++++++++- ignite/pkg/cosmosgen/install.go | 53 +++++++++++---- .../{tools_test.go => install_test.go} | 39 +++++++++++ ignite/pkg/cosmosgen/tools.go | 22 ------- 4 files changed, 141 insertions(+), 37 deletions(-) rename ignite/pkg/cosmosgen/{tools_test.go => install_test.go} (56%) delete mode 100644 ignite/pkg/cosmosgen/tools.go diff --git a/ignite/cmd/chain.go b/ignite/cmd/chain.go index 1b022ddc1f..4c2c6e1f47 100644 --- a/ignite/cmd/chain.go +++ b/ignite/cmd/chain.go @@ -5,6 +5,8 @@ import ( "errors" "fmt" "os" + "path/filepath" + "strings" "github.com/manifoldco/promptui" "github.com/spf13/cobra" @@ -13,13 +15,17 @@ import ( "github.com/ignite/cli/ignite/pkg/cliui" "github.com/ignite/cli/ignite/pkg/cliui/colors" "github.com/ignite/cli/ignite/pkg/cliui/icons" + "github.com/ignite/cli/ignite/pkg/cosmosgen" + "github.com/ignite/cli/ignite/pkg/events" + "github.com/ignite/cli/ignite/pkg/goanalysis" + "github.com/ignite/cli/ignite/pkg/xast" ) const ( msgMigration = "Migrating blockchain config file from v%d to v%d..." - msgMigrationCancel = "Stopping because config version v%d is required to run the command" msgMigrationPrefix = "Your blockchain config version is v%d and the latest is v%d." msgMigrationPrompt = "Would you like to upgrade your config file to v%d" + toolsFile = "tools/tools.go" ) // NewChain returns a command that groups sub commands related to compiling, serving @@ -78,7 +84,7 @@ chain. `, Aliases: []string{"c"}, Args: cobra.ExactArgs(1), - PersistentPreRunE: configMigrationPreRunHandler, + PersistentPreRunE: preRunHandler, } // Add flags required for the configMigrationPreRunHandler @@ -97,10 +103,62 @@ chain. return c } -func configMigrationPreRunHandler(cmd *cobra.Command, _ []string) (err error) { +func preRunHandler(cmd *cobra.Command, _ []string) error { session := cliui.New() defer session.End() + if err := configMigrationPreRunHandler(cmd, session); err != nil { + return err + } + return toolsMigrationPreRunHandler(cmd, session) +} + +func toolsMigrationPreRunHandler(cmd *cobra.Command, session *cliui.Session) (err error) { + // session.EventBus().Send("Checking missing tools...", events.ProgressUpdate()) + + appPath := flagGetPath(cmd) + toolsFilename := filepath.Join(appPath, toolsFile) + f, _, err := xast.ParseFile(toolsFilename) + if err != nil { + return err + } + + var ( + missing = cosmosgen.MissingTools(f) + unused = cosmosgen.UnusedTools(f) + ) + if len(missing) > 0 { + question := fmt.Sprintf( + "Some imports are missing into the tools file (%s): \n%s\nWould you like to add these missing imports?", + toolsFilename, + strings.Join(missing, "\n"), + ) + if err := session.AskConfirm(question); err != nil { + missing = []string{} + } + } + + if len(unused) > 0 { + question := fmt.Sprintf( + "Some imports are unused into the tools file (%s): \n%s\nWould you like to remove these unused imports?", + toolsFilename, + strings.Join(unused, "\n"), + ) + if err := session.AskConfirm(question); err != nil { + unused = []string{} + } + } + session.EventBus().Send("Migrating tools...", events.ProgressUpdate()) + + newTools, err := goanalysis.UpdateInitImports(f, missing, unused) + if err != nil { + return err + } + + return os.WriteFile(toolsFilename, newTools, 0o644) +} + +func configMigrationPreRunHandler(cmd *cobra.Command, session *cliui.Session) (err error) { appPath := flagGetPath(cmd) configPath := getConfig(cmd) if configPath == "" { diff --git a/ignite/pkg/cosmosgen/install.go b/ignite/pkg/cosmosgen/install.go index 2c41e08d38..0c459c2ebb 100644 --- a/ignite/pkg/cosmosgen/install.go +++ b/ignite/pkg/cosmosgen/install.go @@ -3,22 +3,12 @@ package cosmosgen import ( "context" "errors" + "go/ast" + "github.com/ignite/cli/ignite/pkg/goanalysis" "github.com/ignite/cli/ignite/pkg/gocmd" ) -// UnusedTools deprecated and not necessary tools. -func UnusedTools() []string { - return []string{ - // regen protoc plugin - "github.com/regen-network/cosmos-proto/protoc-gen-gocosmos", - - // old ignite repo. - "github.com/ignite-hq/cli/ignite/pkg/cmdrunner", - "github.com/ignite-hq/cli/ignite/pkg/cmdrunner/step", - } -} - // DepTools necessary tools to build and run the chain. func DepTools() []string { return []string{ @@ -46,3 +36,42 @@ func InstallDepTools(ctx context.Context, appPath string) error { } return err } + +// MissingTools find missing tools import indo a *ast.File. +func MissingTools(f *ast.File) (missingTools []string) { + imports := make(map[string]string) + for name, imp := range goanalysis.FormatImports(f) { + imports[imp] = name + } + + for _, tool := range DepTools() { + if _, ok := imports[tool]; !ok { + missingTools = append(missingTools, tool) + } + } + return +} + +// UnusedTools find unused tools import indo a *ast.File. +func UnusedTools(f *ast.File) (unusedTools []string) { + unused := []string{ + // regen protoc plugin + "github.com/regen-network/cosmos-proto/protoc-gen-gocosmos", + + // old ignite repo. + "github.com/ignite-hq/cli/ignite/pkg/cmdrunner", + "github.com/ignite-hq/cli/ignite/pkg/cmdrunner/step", + } + + imports := make(map[string]string) + for name, imp := range goanalysis.FormatImports(f) { + imports[imp] = name + } + + for _, tool := range unused { + if _, ok := imports[tool]; ok { + unusedTools = append(unusedTools, tool) + } + } + return +} diff --git a/ignite/pkg/cosmosgen/tools_test.go b/ignite/pkg/cosmosgen/install_test.go similarity index 56% rename from ignite/pkg/cosmosgen/tools_test.go rename to ignite/pkg/cosmosgen/install_test.go index b344f883ac..0f48f873f6 100644 --- a/ignite/pkg/cosmosgen/tools_test.go +++ b/ignite/pkg/cosmosgen/install_test.go @@ -39,6 +39,45 @@ func TestMissingTools(t *testing.T) { } } +func TestUnusedTools(t *testing.T) { + tests := []struct { + name string + astFile *ast.File + want []string + }{ + { + name: "all unused tools", + astFile: createASTFileWithImports( + "fmt", + "github.com/regen-network/cosmos-proto/protoc-gen-gocosmos", + "github.com/ignite-hq/cli/ignite/pkg/cmdrunner", + "github.com/ignite-hq/cli/ignite/pkg/cmdrunner/step", + ), + want: []string{"github.com/regen-network/cosmos-proto/protoc-gen-gocosmos", "github.com/ignite-hq/cli/ignite/pkg/cmdrunner", "github.com/ignite-hq/cli/ignite/pkg/cmdrunner/step"}, + }, + { + name: "some unused tools", + astFile: createASTFileWithImports( + "fmt", + "github.com/ignite-hq/cli/ignite/pkg/cmdrunner", + ), + want: []string{"github.com/ignite-hq/cli/ignite/pkg/cmdrunner"}, + }, + { + name: "no tools unused", + astFile: createASTFileWithImports("fmt"), + want: nil, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := UnusedTools(tt.astFile) + require.EqualValues(t, tt.want, got) + }) + } +} + // createASTFileWithImports helper function to create an AST file with given imports. func createASTFileWithImports(imports ...string) *ast.File { f := &ast.File{Imports: make([]*ast.ImportSpec, len(imports))} diff --git a/ignite/pkg/cosmosgen/tools.go b/ignite/pkg/cosmosgen/tools.go deleted file mode 100644 index c087b8f276..0000000000 --- a/ignite/pkg/cosmosgen/tools.go +++ /dev/null @@ -1,22 +0,0 @@ -package cosmosgen - -import ( - "go/ast" - - "github.com/ignite/cli/ignite/pkg/goanalysis" -) - -// MissingTools find missing tools import indo a *ast.File. -func MissingTools(f *ast.File) (missingTools []string) { - imports := make(map[string]string) - for name, imp := range goanalysis.FormatImports(f) { - imports[imp] = name - } - - for _, tool := range DepTools() { - if _, ok := imports[tool]; !ok { - missingTools = append(missingTools, tool) - } - } - return -} From 7f861997c10c1463f2e28ff99b7473c49016488e Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Tue, 16 May 2023 14:16:44 +0200 Subject: [PATCH 04/15] fix the method to update tools import --- ignite/cmd/chain.go | 51 ++++++++++--------- ignite/pkg/goanalysis/goanalysis.go | 62 ++++++++++++++++-------- ignite/pkg/goanalysis/goanalysis_test.go | 14 ++++++ 3 files changed, 83 insertions(+), 44 deletions(-) diff --git a/ignite/cmd/chain.go b/ignite/cmd/chain.go index 4c2c6e1f47..ccc1f6991b 100644 --- a/ignite/cmd/chain.go +++ b/ignite/cmd/chain.go @@ -6,7 +6,6 @@ import ( "fmt" "os" "path/filepath" - "strings" "github.com/manifoldco/promptui" "github.com/spf13/cobra" @@ -16,7 +15,6 @@ import ( "github.com/ignite/cli/ignite/pkg/cliui/colors" "github.com/ignite/cli/ignite/pkg/cliui/icons" "github.com/ignite/cli/ignite/pkg/cosmosgen" - "github.com/ignite/cli/ignite/pkg/events" "github.com/ignite/cli/ignite/pkg/goanalysis" "github.com/ignite/cli/ignite/pkg/xast" ) @@ -114,7 +112,7 @@ func preRunHandler(cmd *cobra.Command, _ []string) error { } func toolsMigrationPreRunHandler(cmd *cobra.Command, session *cliui.Session) (err error) { - // session.EventBus().Send("Checking missing tools...", events.ProgressUpdate()) + session.StartSpinner("Checking missing tools...") appPath := flagGetPath(cmd) toolsFilename := filepath.Join(appPath, toolsFile) @@ -127,28 +125,33 @@ func toolsMigrationPreRunHandler(cmd *cobra.Command, session *cliui.Session) (er missing = cosmosgen.MissingTools(f) unused = cosmosgen.UnusedTools(f) ) - if len(missing) > 0 { - question := fmt.Sprintf( - "Some imports are missing into the tools file (%s): \n%s\nWould you like to add these missing imports?", - toolsFilename, - strings.Join(missing, "\n"), - ) - if err := session.AskConfirm(question); err != nil { - missing = []string{} - } - } - if len(unused) > 0 { - question := fmt.Sprintf( - "Some imports are unused into the tools file (%s): \n%s\nWould you like to remove these unused imports?", - toolsFilename, - strings.Join(unused, "\n"), - ) - if err := session.AskConfirm(question); err != nil { - unused = []string{} - } - } - session.EventBus().Send("Migrating tools...", events.ProgressUpdate()) + //session.StopSpinner() + //if len(missing) > 0 { + // question := fmt.Sprintf( + // "Some imports are missing into the %s file: %s. Would you like to add?", + // toolsFilename, + // strings.Join(missing, ", "), + // ) + // if err := session.AskConfirm(question); err != nil { + // missing = []string{} + // } + //} + + //if len(unused) > 0 { + // question := fmt.Sprintf( + // "Some imports are unused into the %s file: %s. Would you like to remove?", + // toolsFilename, + // strings.Join(unused, ", "), + // ) + // if err := session.AskConfirm(question); err != nil { + // unused = []string{} + // } + //} + //if len(missing) == 0 && len(unused) == 0 { + // return nil + //} + session.StartSpinner("Migrating tools...") newTools, err := goanalysis.UpdateInitImports(f, missing, unused) if err != nil { diff --git a/ignite/pkg/goanalysis/goanalysis.go b/ignite/pkg/goanalysis/goanalysis.go index 1cfc827849..10314757df 100644 --- a/ignite/pkg/goanalysis/goanalysis.go +++ b/ignite/pkg/goanalysis/goanalysis.go @@ -13,13 +13,12 @@ import ( "path/filepath" "strconv" "strings" - - "golang.org/x/tools/go/ast/astutil" ) const ( mainPackage = "main" goFileExtension = ".go" + toolsBuildTag = "//go:build tools\n\n" ) // ErrMultipleMainPackagesFound is returned when multiple main packages found while expecting only one. @@ -217,34 +216,57 @@ func FormatImports(f *ast.File) map[string]string { return m } -// ImportExists helper function to check if an import exists in the *ast.File. -func ImportExists(file *ast.File, imp string) bool { - for _, i := range file.Imports { - if i.Path.Value == strconv.Quote(imp) { - return true - } - } - return false -} - // UpdateInitImports helper function to remove and add imports to an *ast.File. func UpdateInitImports(file *ast.File, importsToAdd, importsToRemove []string) ([]byte, error) { - fset := token.NewFileSet() - for _, imp := range importsToRemove { - astutil.DeleteNamedImport(fset, file, "_", imp) - astutil.DeleteImport(fset, file, imp) + // Create a map for faster lookup of items to remove + importMap := make(map[string]bool) + for _, astImport := range file.Imports { + value, err := strconv.Unquote(astImport.Path.Value) + if err != nil { + return nil, err + } + importMap[value] = true + } + for _, removeImport := range importsToRemove { + importMap[removeImport] = false + } + for _, addImport := range importsToAdd { + importMap[addImport] = true } - for _, imp := range importsToAdd { - if !ImportExists(file, imp) { - astutil.AddNamedImport(fset, file, "_", imp) + // Add the imports + for _, d := range file.Decls { + if dd, ok := d.(*ast.GenDecl); ok { + if dd.Tok == token.IMPORT { + file.Imports = make([]*ast.ImportSpec, 0) + dd.Specs = make([]ast.Spec, 0) + for imp, exist := range importMap { + if exist { + spec := createUnderscoreImport(imp) + file.Imports = append(file.Imports, spec) + dd.Specs = append(dd.Specs, spec) + } + } + } } } // Format the modified AST. var buf bytes.Buffer + fset := token.NewFileSet() if err := format.Node(&buf, fset, file); err != nil { return nil, fmt.Errorf("failed to format file: %v", err) } - return buf.Bytes(), nil + return append([]byte(toolsBuildTag), buf.Bytes()...), nil +} + +// createUnderscoreImports helper function to create an AST underscore import with given path. +func createUnderscoreImport(imp string) *ast.ImportSpec { + return &ast.ImportSpec{ + Name: ast.NewIdent("_"), + Path: &ast.BasicLit{ + Kind: token.STRING, + Value: strconv.Quote(imp), + }, + } } diff --git a/ignite/pkg/goanalysis/goanalysis_test.go b/ignite/pkg/goanalysis/goanalysis_test.go index ed27cc4a49..dd1c4cb046 100644 --- a/ignite/pkg/goanalysis/goanalysis_test.go +++ b/ignite/pkg/goanalysis/goanalysis_test.go @@ -424,6 +424,20 @@ func TestUpdateInitImports(t *testing.T) { "go/ast", }, }, + want: []string{}, + }, + { + name: "test remove not exist import", + args: args{ + fileImports: []string{ + "errors", + "go/ast", + }, + importsToAdd: []string{}, + importsToRemove: []string{ + "fmt", + }, + }, want: []string{ "errors", "go/ast", From c2168d6a0e83b87eb7f99d359aa1dd0314647ce4 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Tue, 16 May 2023 14:17:27 +0200 Subject: [PATCH 05/15] uncomment migrations questions --- ignite/cmd/chain.go | 51 +++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/ignite/cmd/chain.go b/ignite/cmd/chain.go index ccc1f6991b..8e27e19275 100644 --- a/ignite/cmd/chain.go +++ b/ignite/cmd/chain.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "github.com/manifoldco/promptui" "github.com/spf13/cobra" @@ -126,31 +127,31 @@ func toolsMigrationPreRunHandler(cmd *cobra.Command, session *cliui.Session) (er unused = cosmosgen.UnusedTools(f) ) - //session.StopSpinner() - //if len(missing) > 0 { - // question := fmt.Sprintf( - // "Some imports are missing into the %s file: %s. Would you like to add?", - // toolsFilename, - // strings.Join(missing, ", "), - // ) - // if err := session.AskConfirm(question); err != nil { - // missing = []string{} - // } - //} - - //if len(unused) > 0 { - // question := fmt.Sprintf( - // "Some imports are unused into the %s file: %s. Would you like to remove?", - // toolsFilename, - // strings.Join(unused, ", "), - // ) - // if err := session.AskConfirm(question); err != nil { - // unused = []string{} - // } - //} - //if len(missing) == 0 && len(unused) == 0 { - // return nil - //} + session.StopSpinner() + if len(missing) > 0 { + question := fmt.Sprintf( + "Some imports are missing into the %s file: %s. Would you like to add?", + toolsFilename, + strings.Join(missing, ", "), + ) + if err := session.AskConfirm(question); err != nil { + missing = []string{} + } + } + + if len(unused) > 0 { + question := fmt.Sprintf( + "Some imports are unused into the %s file: %s. Would you like to remove?", + toolsFilename, + strings.Join(unused, ", "), + ) + if err := session.AskConfirm(question); err != nil { + unused = []string{} + } + } + if len(missing) == 0 && len(unused) == 0 { + return nil + } session.StartSpinner("Migrating tools...") newTools, err := goanalysis.UpdateInitImports(f, missing, unused) From 2af0a9afe2bee75fd107b2834e1b4775b69fa28d Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Tue, 16 May 2023 14:30:43 +0200 Subject: [PATCH 06/15] add changelog --- changelog.md | 4 ++++ ignite/pkg/goanalysis/goanalysis.go | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index 658e6c0594..df179f6ffc 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,10 @@ ## Unreleased +### Features + +- [#3505](https://github.com/ignite/cli/pull/3505) Auto migrate dependency tools + ### Changes - [#3444](https://github.com/ignite/cli/pull/3444) Add support for ICS chains in ts-client generation diff --git a/ignite/pkg/goanalysis/goanalysis.go b/ignite/pkg/goanalysis/goanalysis.go index 10314757df..90e494e98e 100644 --- a/ignite/pkg/goanalysis/goanalysis.go +++ b/ignite/pkg/goanalysis/goanalysis.go @@ -255,7 +255,7 @@ func UpdateInitImports(file *ast.File, importsToAdd, importsToRemove []string) ( var buf bytes.Buffer fset := token.NewFileSet() if err := format.Node(&buf, fset, file); err != nil { - return nil, fmt.Errorf("failed to format file: %v", err) + return nil, fmt.Errorf("failed to format file: %w", err) } return append([]byte(toolsBuildTag), buf.Bytes()...), nil } From f885b4023e267c1c1242120a3260d268e6014e99 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Wed, 17 May 2023 17:35:35 +0200 Subject: [PATCH 07/15] idente ignite/pkg/cosmosgen/install_test.go tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jerónimo Albi --- ignite/pkg/cosmosgen/install_test.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/ignite/pkg/cosmosgen/install_test.go b/ignite/pkg/cosmosgen/install_test.go index 0f48f873f6..743399a393 100644 --- a/ignite/pkg/cosmosgen/install_test.go +++ b/ignite/pkg/cosmosgen/install_test.go @@ -22,8 +22,15 @@ func TestMissingTools(t *testing.T) { }, { name: "some missing tools", - astFile: createASTFileWithImports("github.com/golang/protobuf/protoc-gen-go", "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway", "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger"), - want: []string{"github.com/cosmos/gogoproto/protoc-gen-gocosmos", "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2"}, + astFile: createASTFileWithImports( + "github.com/golang/protobuf/protoc-gen-go", + "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway", + "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger", + ), + want: []string{ + "github.com/cosmos/gogoproto/protoc-gen-gocosmos", + "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2", + }, }, { name: "all tools missing", From 0624a88cf67b7b21e7d0e88615d19dada6d50963 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Wed, 17 May 2023 17:36:05 +0200 Subject: [PATCH 08/15] fix ignite/cmd/chain.go mesages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jerónimo Albi --- ignite/cmd/chain.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ignite/cmd/chain.go b/ignite/cmd/chain.go index 8e27e19275..9891243c6b 100644 --- a/ignite/cmd/chain.go +++ b/ignite/cmd/chain.go @@ -130,7 +130,7 @@ func toolsMigrationPreRunHandler(cmd *cobra.Command, session *cliui.Session) (er session.StopSpinner() if len(missing) > 0 { question := fmt.Sprintf( - "Some imports are missing into the %s file: %s. Would you like to add?", + "Some required imports are missing in %s file: %s. Would you like to add them?", toolsFilename, strings.Join(missing, ", "), ) From 2a5c703e8d33ca526aad37a1de28b9d7eece9295 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Wed, 17 May 2023 17:36:16 +0200 Subject: [PATCH 09/15] fix ignite/cmd/chain.go remove msg MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jerónimo Albi --- ignite/cmd/chain.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ignite/cmd/chain.go b/ignite/cmd/chain.go index 9891243c6b..b8bebc0c66 100644 --- a/ignite/cmd/chain.go +++ b/ignite/cmd/chain.go @@ -141,7 +141,7 @@ func toolsMigrationPreRunHandler(cmd *cobra.Command, session *cliui.Session) (er if len(unused) > 0 { question := fmt.Sprintf( - "Some imports are unused into the %s file: %s. Would you like to remove?", + "File %s contains deprecated imports: %s. Would you like to remove them?", toolsFilename, strings.Join(unused, ", "), ) From f1dc38b841539c57a51ecf3575d0bb461431c505 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Wed, 17 May 2023 17:36:29 +0200 Subject: [PATCH 10/15] ident ignite/pkg/cosmosgen/install_test.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jerónimo Albi --- ignite/pkg/cosmosgen/install_test.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ignite/pkg/cosmosgen/install_test.go b/ignite/pkg/cosmosgen/install_test.go index 743399a393..c979bfc4d2 100644 --- a/ignite/pkg/cosmosgen/install_test.go +++ b/ignite/pkg/cosmosgen/install_test.go @@ -60,7 +60,11 @@ func TestUnusedTools(t *testing.T) { "github.com/ignite-hq/cli/ignite/pkg/cmdrunner", "github.com/ignite-hq/cli/ignite/pkg/cmdrunner/step", ), - want: []string{"github.com/regen-network/cosmos-proto/protoc-gen-gocosmos", "github.com/ignite-hq/cli/ignite/pkg/cmdrunner", "github.com/ignite-hq/cli/ignite/pkg/cmdrunner/step"}, + want: []string{ + "github.com/regen-network/cosmos-proto/protoc-gen-gocosmos", + "github.com/ignite-hq/cli/ignite/pkg/cmdrunner", + "github.com/ignite-hq/cli/ignite/pkg/cmdrunner/step", + }, }, { name: "some unused tools", From 8e0efd5f4c66a120c7a172db2e5c4aa5f35bc394 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Wed, 17 May 2023 17:47:40 +0200 Subject: [PATCH 11/15] fix double ? in the ask question and improve comments --- ignite/cmd/chain.go | 4 ++-- ignite/pkg/goanalysis/goanalysis.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ignite/cmd/chain.go b/ignite/cmd/chain.go index b8bebc0c66..42674b26dc 100644 --- a/ignite/cmd/chain.go +++ b/ignite/cmd/chain.go @@ -130,7 +130,7 @@ func toolsMigrationPreRunHandler(cmd *cobra.Command, session *cliui.Session) (er session.StopSpinner() if len(missing) > 0 { question := fmt.Sprintf( - "Some required imports are missing in %s file: %s. Would you like to add them?", + "Some required imports are missing in %s file: %s. Would you like to add them", toolsFilename, strings.Join(missing, ", "), ) @@ -141,7 +141,7 @@ func toolsMigrationPreRunHandler(cmd *cobra.Command, session *cliui.Session) (er if len(unused) > 0 { question := fmt.Sprintf( - "File %s contains deprecated imports: %s. Would you like to remove them?", + "File %s contains deprecated imports: %s. Would you like to remove them", toolsFilename, strings.Join(unused, ", "), ) diff --git a/ignite/pkg/goanalysis/goanalysis.go b/ignite/pkg/goanalysis/goanalysis.go index 90e494e98e..6e46e7a4b4 100644 --- a/ignite/pkg/goanalysis/goanalysis.go +++ b/ignite/pkg/goanalysis/goanalysis.go @@ -216,7 +216,7 @@ func FormatImports(f *ast.File) map[string]string { return m } -// UpdateInitImports helper function to remove and add imports to an *ast.File. +// UpdateInitImports helper function to remove and add underscore (init) imports to an *ast.File. func UpdateInitImports(file *ast.File, importsToAdd, importsToRemove []string) ([]byte, error) { // Create a map for faster lookup of items to remove importMap := make(map[string]bool) From 87bc5f83e977acd5e0901c4d2f238025d42412f3 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Wed, 17 May 2023 18:10:35 +0200 Subject: [PATCH 12/15] use io.writer intead return a byte array to goanalysis.UpdateInitImports method --- ignite/cmd/chain.go | 6 +++--- ignite/pkg/cosmosgen/install_test.go | 2 +- ignite/pkg/goanalysis/goanalysis.go | 15 ++++++--------- ignite/pkg/goanalysis/goanalysis_test.go | 6 ++++-- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/ignite/cmd/chain.go b/ignite/cmd/chain.go index 42674b26dc..1fc8ba1f0a 100644 --- a/ignite/cmd/chain.go +++ b/ignite/cmd/chain.go @@ -154,12 +154,12 @@ func toolsMigrationPreRunHandler(cmd *cobra.Command, session *cliui.Session) (er } session.StartSpinner("Migrating tools...") - newTools, err := goanalysis.UpdateInitImports(f, missing, unused) - if err != nil { + var buf bytes.Buffer + if err := goanalysis.UpdateInitImports(f, &buf, missing, unused); err != nil { return err } - return os.WriteFile(toolsFilename, newTools, 0o644) + return os.WriteFile(toolsFilename, buf.Bytes(), 0o644) } func configMigrationPreRunHandler(cmd *cobra.Command, session *cliui.Session) (err error) { diff --git a/ignite/pkg/cosmosgen/install_test.go b/ignite/pkg/cosmosgen/install_test.go index c979bfc4d2..37b9234a50 100644 --- a/ignite/pkg/cosmosgen/install_test.go +++ b/ignite/pkg/cosmosgen/install_test.go @@ -21,7 +21,7 @@ func TestMissingTools(t *testing.T) { want: nil, }, { - name: "some missing tools", + name: "some missing tools", astFile: createASTFileWithImports( "github.com/golang/protobuf/protoc-gen-go", "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway", diff --git a/ignite/pkg/goanalysis/goanalysis.go b/ignite/pkg/goanalysis/goanalysis.go index 6e46e7a4b4..89631f6d3d 100644 --- a/ignite/pkg/goanalysis/goanalysis.go +++ b/ignite/pkg/goanalysis/goanalysis.go @@ -2,13 +2,13 @@ package goanalysis import ( - "bytes" "errors" "fmt" "go/ast" "go/format" "go/parser" "go/token" + "io" "os" "path/filepath" "strconv" @@ -217,13 +217,13 @@ func FormatImports(f *ast.File) map[string]string { } // UpdateInitImports helper function to remove and add underscore (init) imports to an *ast.File. -func UpdateInitImports(file *ast.File, importsToAdd, importsToRemove []string) ([]byte, error) { +func UpdateInitImports(file *ast.File, writer io.Writer, importsToAdd, importsToRemove []string) error { // Create a map for faster lookup of items to remove importMap := make(map[string]bool) for _, astImport := range file.Imports { value, err := strconv.Unquote(astImport.Path.Value) if err != nil { - return nil, err + return err } importMap[value] = true } @@ -251,13 +251,10 @@ func UpdateInitImports(file *ast.File, importsToAdd, importsToRemove []string) ( } } - // Format the modified AST. - var buf bytes.Buffer - fset := token.NewFileSet() - if err := format.Node(&buf, fset, file); err != nil { - return nil, fmt.Errorf("failed to format file: %w", err) + if _, err := writer.Write([]byte(toolsBuildTag)); err != nil { + return fmt.Errorf("failed to write the build tag: %w", err) } - return append([]byte(toolsBuildTag), buf.Bytes()...), nil + return format.Node(writer, token.NewFileSet(), file) } // createUnderscoreImports helper function to create an AST underscore import with given path. diff --git a/ignite/pkg/goanalysis/goanalysis_test.go b/ignite/pkg/goanalysis/goanalysis_test.go index dd1c4cb046..9e42d0cf16 100644 --- a/ignite/pkg/goanalysis/goanalysis_test.go +++ b/ignite/pkg/goanalysis/goanalysis_test.go @@ -1,6 +1,7 @@ package goanalysis_test import ( + "bytes" "errors" "go/ast" "go/parser" @@ -457,7 +458,8 @@ func TestUpdateInitImports(t *testing.T) { } // test method - got, err := goanalysis.UpdateInitImports(file, tt.args.importsToAdd, tt.args.importsToRemove) + var buf bytes.Buffer + err := goanalysis.UpdateInitImports(file, &buf, tt.args.importsToAdd, tt.args.importsToRemove) if tt.err != nil { require.Error(t, err) require.ErrorIs(t, err, tt.err) @@ -465,7 +467,7 @@ func TestUpdateInitImports(t *testing.T) { } require.NoError(t, err) - gotFile, err := parser.ParseFile(token.NewFileSet(), "", got, parser.ParseComments) + gotFile, err := parser.ParseFile(token.NewFileSet(), "", buf.Bytes(), parser.ParseComments) require.NoError(t, err) gotImports := make([]string, 0) From fce6ba9142ecca57d898f88265a37116156c77e5 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Wed, 17 May 2023 18:43:20 +0200 Subject: [PATCH 13/15] fix goanalysis test package name --- ignite/pkg/cosmosgen/install_test.go | 12 +++++++----- ignite/pkg/goanalysis/goanalysis_test.go | 3 +-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/ignite/pkg/cosmosgen/install_test.go b/ignite/pkg/cosmosgen/install_test.go index 37b9234a50..8158c360c2 100644 --- a/ignite/pkg/cosmosgen/install_test.go +++ b/ignite/pkg/cosmosgen/install_test.go @@ -1,4 +1,4 @@ -package cosmosgen +package cosmosgen_test import ( "go/ast" @@ -7,6 +7,8 @@ import ( "testing" "github.com/stretchr/testify/require" + + "github.com/ignite/cli/ignite/pkg/cosmosgen" ) func TestMissingTools(t *testing.T) { @@ -17,7 +19,7 @@ func TestMissingTools(t *testing.T) { }{ { name: "no missing tools", - astFile: createASTFileWithImports(DepTools()...), + astFile: createASTFileWithImports(cosmosgen.DepTools()...), want: nil, }, { @@ -35,12 +37,12 @@ func TestMissingTools(t *testing.T) { { name: "all tools missing", astFile: createASTFileWithImports(), - want: DepTools(), + want: cosmosgen.DepTools(), }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := MissingTools(tt.astFile) + got := cosmosgen.MissingTools(tt.astFile) require.EqualValues(t, tt.want, got) }) } @@ -83,7 +85,7 @@ func TestUnusedTools(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := UnusedTools(tt.astFile) + got := cosmosgen.UnusedTools(tt.astFile) require.EqualValues(t, tt.want, got) }) } diff --git a/ignite/pkg/goanalysis/goanalysis_test.go b/ignite/pkg/goanalysis/goanalysis_test.go index 9e42d0cf16..556ff7fde8 100644 --- a/ignite/pkg/goanalysis/goanalysis_test.go +++ b/ignite/pkg/goanalysis/goanalysis_test.go @@ -12,11 +12,10 @@ import ( "testing" "github.com/stretchr/testify/require" + "golang.org/x/tools/go/ast/astutil" "github.com/ignite/cli/ignite/pkg/goanalysis" "github.com/ignite/cli/ignite/pkg/xast" - - "golang.org/x/tools/go/ast/astutil" ) var MainFile = []byte(`package main`) From 4e09a37052aa16241ad8bfde8bdddb1a9fbabaee Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Fri, 19 May 2023 02:11:47 +0200 Subject: [PATCH 14/15] Update ignite/cmd/chain.go vars MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jerónimo Albi --- ignite/cmd/chain.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/ignite/cmd/chain.go b/ignite/cmd/chain.go index 1fc8ba1f0a..65138199bb 100644 --- a/ignite/cmd/chain.go +++ b/ignite/cmd/chain.go @@ -122,10 +122,8 @@ func toolsMigrationPreRunHandler(cmd *cobra.Command, session *cliui.Session) (er return err } - var ( - missing = cosmosgen.MissingTools(f) - unused = cosmosgen.UnusedTools(f) - ) + missing := cosmosgen.MissingTools(f) + unused := cosmosgen.UnusedTools(f) session.StopSpinner() if len(missing) > 0 { From 0ae283edb35c09a89fbb269e13ae95f436bc1bcb Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Fri, 19 May 2023 02:21:31 +0200 Subject: [PATCH 15/15] run gofmt --- ignite/cmd/chain.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ignite/cmd/chain.go b/ignite/cmd/chain.go index 65138199bb..cff8281e7d 100644 --- a/ignite/cmd/chain.go +++ b/ignite/cmd/chain.go @@ -123,7 +123,7 @@ func toolsMigrationPreRunHandler(cmd *cobra.Command, session *cliui.Session) (er } missing := cosmosgen.MissingTools(f) - unused := cosmosgen.UnusedTools(f) + unused := cosmosgen.UnusedTools(f) session.StopSpinner() if len(missing) > 0 {