From 0e275b62baaebee8b8048a0a8f1451ffbde1f244 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Fri, 23 Feb 2024 11:46:03 -0300 Subject: [PATCH] feat(cmd): make some `cmd` pkg functions public (#3985) * move `sourceModificationToString` function to `xgenny` and `relativePath` to `xfilepath` * move `newChainWithHomeFlags` to the services package to be reusable * add changelog * rename the method `chain.NewChainWithHomeFlags` to `chain.NewWithHomeFlags` --------- Co-authored-by: Pantani --- changelog.md | 1 + ignite/cmd/chain_build.go | 2 +- ignite/cmd/chain_debug.go | 2 +- ignite/cmd/chain_faucet.go | 2 +- ignite/cmd/chain_init.go | 2 +- ignite/cmd/chain_lint.go | 2 +- ignite/cmd/chain_serve.go | 2 +- ignite/cmd/cmd.go | 72 ---------------- ignite/cmd/generate_composables.go | 2 +- ignite/cmd/generate_go.go | 2 +- ignite/cmd/generate_hooks.go | 2 +- ignite/cmd/generate_openapi.go | 2 +- ignite/cmd/generate_typescript_client.go | 2 +- ignite/cmd/generate_vuex.go | 2 +- ignite/cmd/plugin.go | 3 +- ignite/cmd/scaffold.go | 3 +- ignite/cmd/scaffold_band.go | 3 +- ignite/cmd/scaffold_chain.go | 3 +- ignite/cmd/scaffold_configs.go | 3 +- ignite/cmd/scaffold_message.go | 3 +- ignite/cmd/scaffold_module.go | 3 +- ignite/cmd/scaffold_package.go | 3 +- ignite/cmd/scaffold_params.go | 3 +- ignite/cmd/scaffold_query.go | 3 +- ignite/pkg/xfilepath/xfilepath.go | 13 +++ ignite/pkg/xfilepath/xfilepath_test.go | 53 ++++++++++++ ignite/pkg/xgenny/sourcemodification.go | 47 +++++++++++ ignite/services/chain/chain.go | 102 ++++++++++++++--------- 28 files changed, 210 insertions(+), 132 deletions(-) diff --git a/changelog.md b/changelog.md index c9c7af18fd..c5c30b0595 100644 --- a/changelog.md +++ b/changelog.md @@ -6,6 +6,7 @@ - [#3977](https://github.com/ignite/cli/pull/3977) Add `chain lint` command to lint the chain's codebase using `golangci-lint` - [#3770](https://github.com/ignite/cli/pull/3770) Add `scaffold configs` and `scaffold params` commands +- [#3985](https://github.com/ignite/cli/pull/3985) Make some `cmd` pkg functions public ### Changes diff --git a/ignite/cmd/chain_build.go b/ignite/cmd/chain_build.go index 659d0bb525..5299e915ee 100644 --- a/ignite/cmd/chain_build.go +++ b/ignite/cmd/chain_build.go @@ -123,7 +123,7 @@ func chainBuildHandler(cmd *cobra.Command, _ []string) error { chainOption = append(chainOption, chain.CheckDependencies()) } - c, err := newChainWithHomeFlags(cmd, chainOption...) + c, err := chain.NewWithHomeFlags(cmd, chainOption...) if err != nil { return err } diff --git a/ignite/cmd/chain_debug.go b/ignite/cmd/chain_debug.go index 58ae4a2cc3..dbc46481dd 100644 --- a/ignite/cmd/chain_debug.go +++ b/ignite/cmd/chain_debug.go @@ -105,7 +105,7 @@ func chainDebug(cmd *cobra.Command, session *cliui.Session) error { chainOptions = append(chainOptions, chain.ConfigFile(config)) } - c, err := newChainWithHomeFlags(cmd, chainOptions...) + c, err := chain.NewWithHomeFlags(cmd, chainOptions...) if err != nil { return err } diff --git a/ignite/cmd/chain_faucet.go b/ignite/cmd/chain_faucet.go index 5bd4e06427..1e50122fd3 100644 --- a/ignite/cmd/chain_faucet.go +++ b/ignite/cmd/chain_faucet.go @@ -42,7 +42,7 @@ func chainFaucetHandler(cmd *cobra.Command, args []string) error { chain.CollectEvents(session.EventBus()), } - c, err := newChainWithHomeFlags(cmd, chainOption...) + c, err := chain.NewWithHomeFlags(cmd, chainOption...) if err != nil { return err } diff --git a/ignite/cmd/chain_init.go b/ignite/cmd/chain_init.go index 014a1cfcc4..481146353f 100644 --- a/ignite/cmd/chain_init.go +++ b/ignite/cmd/chain_init.go @@ -111,7 +111,7 @@ func chainInitHandler(cmd *cobra.Command, _ []string) error { chainOption = append(chainOption, chain.CheckDependencies()) } - c, err := newChainWithHomeFlags(cmd, chainOption...) + c, err := chain.NewWithHomeFlags(cmd, chainOption...) if err != nil { return err } diff --git a/ignite/cmd/chain_lint.go b/ignite/cmd/chain_lint.go index 5d23e05087..8123eeeccb 100644 --- a/ignite/cmd/chain_lint.go +++ b/ignite/cmd/chain_lint.go @@ -25,7 +25,7 @@ func NewChainLint() *cobra.Command { chain.CollectEvents(session.EventBus()), } - c, err := newChainWithHomeFlags(cmd, chainOption...) + c, err := chain.NewWithHomeFlags(cmd, chainOption...) if err != nil { return err } diff --git a/ignite/cmd/chain_serve.go b/ignite/cmd/chain_serve.go index c8c842a10d..e6f39660c7 100644 --- a/ignite/cmd/chain_serve.go +++ b/ignite/cmd/chain_serve.go @@ -140,7 +140,7 @@ func chainServe(cmd *cobra.Command, session *cliui.Session) error { } // create the chain - c, err := newChainWithHomeFlags(cmd, chainOption...) + c, err := chain.NewWithHomeFlags(cmd, chainOption...) if err != nil { return err } diff --git a/ignite/cmd/cmd.go b/ignite/cmd/cmd.go index b33d5c985d..bd46a43b2f 100644 --- a/ignite/cmd/cmd.go +++ b/ignite/cmd/cmd.go @@ -6,8 +6,6 @@ import ( "os" "path/filepath" "slices" - "sort" - "strings" "time" "github.com/spf13/cobra" @@ -16,13 +14,10 @@ import ( "github.com/ignite/cli/v28/ignite/config" "github.com/ignite/cli/v28/ignite/pkg/cache" "github.com/ignite/cli/v28/ignite/pkg/cliui" - "github.com/ignite/cli/v28/ignite/pkg/cliui/colors" uilog "github.com/ignite/cli/v28/ignite/pkg/cliui/log" "github.com/ignite/cli/v28/ignite/pkg/errors" "github.com/ignite/cli/v28/ignite/pkg/gitpod" "github.com/ignite/cli/v28/ignite/pkg/goenv" - "github.com/ignite/cli/v28/ignite/pkg/xgenny" - "github.com/ignite/cli/v28/ignite/services/chain" "github.com/ignite/cli/v28/ignite/version" ) @@ -175,60 +170,6 @@ func flagGetClearCache(cmd *cobra.Command) bool { return clearCache } -func newChainWithHomeFlags(cmd *cobra.Command, chainOption ...chain.Option) (*chain.Chain, error) { - // Check if custom home is provided - if home := getHome(cmd); home != "" { - chainOption = append(chainOption, chain.HomePath(home)) - } - - appPath := flagGetPath(cmd) - absPath, err := filepath.Abs(appPath) - if err != nil { - return nil, err - } - - return chain.New(absPath, chainOption...) -} - -var ( - modifyPrefix = colors.Modified("modify ") - createPrefix = colors.Success("create ") - removePrefix = func(s string) string { - return strings.TrimPrefix(strings.TrimPrefix(s, modifyPrefix), createPrefix) - } -) - -func sourceModificationToString(sm xgenny.SourceModification) (string, error) { - // get file names and add prefix - var files []string - for _, modified := range sm.ModifiedFiles() { - // get the relative app path from the current directory - relativePath, err := relativePath(modified) - if err != nil { - return "", err - } - files = append(files, modifyPrefix+relativePath) - } - for _, created := range sm.CreatedFiles() { - // get the relative app path from the current directory - relativePath, err := relativePath(created) - if err != nil { - return "", err - } - files = append(files, createPrefix+relativePath) - } - - // sort filenames without prefix - sort.Slice(files, func(i, j int) bool { - s1 := removePrefix(files[i]) - s2 := removePrefix(files[j]) - - return strings.Compare(s1, s2) == -1 - }) - - return "\n" + strings.Join(files, "\n"), nil -} - func deprecated() []*cobra.Command { return []*cobra.Command{ { @@ -250,19 +191,6 @@ func deprecated() []*cobra.Command { } } -// relativePath return the relative app path from the current directory. -func relativePath(appPath string) (string, error) { - pwd, err := os.Getwd() - if err != nil { - return "", err - } - path, err := filepath.Rel(pwd, appPath) - if err != nil { - return "", err - } - return path, nil -} - func checkNewVersion(ctx context.Context) { if gitpod.IsOnGitpod() { return diff --git a/ignite/cmd/generate_composables.go b/ignite/cmd/generate_composables.go index e5b327decc..649384d9d8 100644 --- a/ignite/cmd/generate_composables.go +++ b/ignite/cmd/generate_composables.go @@ -25,7 +25,7 @@ func generateComposablesHandler(cmd *cobra.Command, _ []string) error { session := cliui.New(cliui.StartSpinnerWithText(statusGenerating)) defer session.End() - c, err := newChainWithHomeFlags( + c, err := chain.NewWithHomeFlags( cmd, chain.WithOutputer(session), chain.CollectEvents(session.EventBus()), diff --git a/ignite/cmd/generate_go.go b/ignite/cmd/generate_go.go index 7b61be77a4..678589ac99 100644 --- a/ignite/cmd/generate_go.go +++ b/ignite/cmd/generate_go.go @@ -24,7 +24,7 @@ func generateGoHandler(cmd *cobra.Command, _ []string) error { session := cliui.New(cliui.StartSpinnerWithText(statusGenerating)) defer session.End() - c, err := newChainWithHomeFlags( + c, err := chain.NewWithHomeFlags( cmd, chain.WithOutputer(session), chain.CollectEvents(session.EventBus()), diff --git a/ignite/cmd/generate_hooks.go b/ignite/cmd/generate_hooks.go index a1eddb0a35..b1506dcae0 100644 --- a/ignite/cmd/generate_hooks.go +++ b/ignite/cmd/generate_hooks.go @@ -25,7 +25,7 @@ func generateHooksHandler(cmd *cobra.Command, _ []string) error { session := cliui.New(cliui.StartSpinnerWithText(statusGenerating)) defer session.End() - c, err := newChainWithHomeFlags( + c, err := chain.NewWithHomeFlags( cmd, chain.WithOutputer(session), chain.CollectEvents(session.EventBus()), diff --git a/ignite/cmd/generate_openapi.go b/ignite/cmd/generate_openapi.go index 4bd830ed85..a3b22ac02f 100644 --- a/ignite/cmd/generate_openapi.go +++ b/ignite/cmd/generate_openapi.go @@ -24,7 +24,7 @@ func generateOpenAPIHandler(cmd *cobra.Command, _ []string) error { session := cliui.New(cliui.StartSpinnerWithText(statusGenerating)) defer session.End() - c, err := newChainWithHomeFlags( + c, err := chain.NewWithHomeFlags( cmd, chain.WithOutputer(session), chain.CollectEvents(session.EventBus()), diff --git a/ignite/cmd/generate_typescript_client.go b/ignite/cmd/generate_typescript_client.go index 00a85cf277..62434eca74 100644 --- a/ignite/cmd/generate_typescript_client.go +++ b/ignite/cmd/generate_typescript_client.go @@ -48,7 +48,7 @@ func generateTSClientHandler(cmd *cobra.Command, _ []string) error { session := cliui.New(cliui.StartSpinnerWithText(statusGenerating)) defer session.End() - c, err := newChainWithHomeFlags( + c, err := chain.NewWithHomeFlags( cmd, chain.WithOutputer(session), chain.CollectEvents(session.EventBus()), diff --git a/ignite/cmd/generate_vuex.go b/ignite/cmd/generate_vuex.go index 5d41e9678f..ea8ca5ecd4 100644 --- a/ignite/cmd/generate_vuex.go +++ b/ignite/cmd/generate_vuex.go @@ -25,7 +25,7 @@ func generateVuexHandler(cmd *cobra.Command, _ []string) error { session := cliui.New(cliui.StartSpinnerWithText(statusGenerating)) defer session.End() - c, err := newChainWithHomeFlags( + c, err := chain.NewWithHomeFlags( cmd, chain.WithOutputer(session), chain.CollectEvents(session.EventBus()), diff --git a/ignite/cmd/plugin.go b/ignite/cmd/plugin.go index a287ed2544..dd25b2535b 100644 --- a/ignite/cmd/plugin.go +++ b/ignite/cmd/plugin.go @@ -18,6 +18,7 @@ import ( "github.com/ignite/cli/v28/ignite/pkg/errors" "github.com/ignite/cli/v28/ignite/pkg/gomodule" "github.com/ignite/cli/v28/ignite/pkg/xgit" + "github.com/ignite/cli/v28/ignite/services/chain" "github.com/ignite/cli/v28/ignite/services/plugin" ) @@ -694,7 +695,7 @@ func printPlugins(ctx context.Context, session *cliui.Session) error { func newAppClientAPI(cmd *cobra.Command) (plugin.ClientAPI, error) { // Get chain when the plugin runs inside an blockchain app - c, err := newChainWithHomeFlags(cmd) + c, err := chain.NewWithHomeFlags(cmd) if err != nil && !errors.Is(err, gomodule.ErrGoModNotFound) { return nil, err } diff --git a/ignite/cmd/scaffold.go b/ignite/cmd/scaffold.go index 703a8056a4..ad0695efd0 100644 --- a/ignite/cmd/scaffold.go +++ b/ignite/cmd/scaffold.go @@ -12,6 +12,7 @@ import ( "github.com/ignite/cli/v28/ignite/pkg/errors" "github.com/ignite/cli/v28/ignite/pkg/gomodulepath" "github.com/ignite/cli/v28/ignite/pkg/placeholder" + "github.com/ignite/cli/v28/ignite/pkg/xgenny" "github.com/ignite/cli/v28/ignite/pkg/xgit" "github.com/ignite/cli/v28/ignite/services/scaffolder" "github.com/ignite/cli/v28/ignite/version" @@ -223,7 +224,7 @@ func scaffoldType( return err } - modificationsStr, err := sourceModificationToString(sm) + modificationsStr, err := xgenny.SourceModificationToString(sm) if err != nil { return err } diff --git a/ignite/cmd/scaffold_band.go b/ignite/cmd/scaffold_band.go index 14840fd8ad..458ad24a25 100644 --- a/ignite/cmd/scaffold_band.go +++ b/ignite/cmd/scaffold_band.go @@ -6,6 +6,7 @@ import ( "github.com/ignite/cli/v28/ignite/pkg/cliui" "github.com/ignite/cli/v28/ignite/pkg/errors" "github.com/ignite/cli/v28/ignite/pkg/placeholder" + "github.com/ignite/cli/v28/ignite/pkg/xgenny" "github.com/ignite/cli/v28/ignite/services/scaffolder" ) @@ -75,7 +76,7 @@ func createBandchainHandler(cmd *cobra.Command, args []string) error { return err } - modificationsStr, err := sourceModificationToString(sm) + modificationsStr, err := xgenny.SourceModificationToString(sm) if err != nil { return err } diff --git a/ignite/cmd/scaffold_chain.go b/ignite/cmd/scaffold_chain.go index aa408bf197..1c44c3d0ae 100644 --- a/ignite/cmd/scaffold_chain.go +++ b/ignite/cmd/scaffold_chain.go @@ -6,6 +6,7 @@ import ( "github.com/ignite/cli/v28/ignite/pkg/cliui" "github.com/ignite/cli/v28/ignite/pkg/errors" "github.com/ignite/cli/v28/ignite/pkg/placeholder" + "github.com/ignite/cli/v28/ignite/pkg/xfilepath" "github.com/ignite/cli/v28/ignite/services/scaffolder" ) @@ -132,7 +133,7 @@ func scaffoldChainHandler(cmd *cobra.Command, args []string) error { return err } - path, err := relativePath(appdir) + path, err := xfilepath.RelativePath(appdir) if err != nil { return err } diff --git a/ignite/cmd/scaffold_configs.go b/ignite/cmd/scaffold_configs.go index 9a7bf29d1d..4f194029f6 100644 --- a/ignite/cmd/scaffold_configs.go +++ b/ignite/cmd/scaffold_configs.go @@ -7,6 +7,7 @@ import ( "github.com/ignite/cli/v28/ignite/pkg/cliui" "github.com/ignite/cli/v28/ignite/pkg/placeholder" + "github.com/ignite/cli/v28/ignite/pkg/xgenny" "github.com/ignite/cli/v28/ignite/services/scaffolder" ) @@ -67,7 +68,7 @@ func scaffoldConfigsHandler(cmd *cobra.Command, args []string) error { return err } - modificationsStr, err := sourceModificationToString(sm) + modificationsStr, err := xgenny.SourceModificationToString(sm) if err != nil { return err } diff --git a/ignite/cmd/scaffold_message.go b/ignite/cmd/scaffold_message.go index cbdd690570..6fa0423984 100644 --- a/ignite/cmd/scaffold_message.go +++ b/ignite/cmd/scaffold_message.go @@ -5,6 +5,7 @@ import ( "github.com/ignite/cli/v28/ignite/pkg/cliui" "github.com/ignite/cli/v28/ignite/pkg/placeholder" + "github.com/ignite/cli/v28/ignite/pkg/xgenny" "github.com/ignite/cli/v28/ignite/services/scaffolder" ) @@ -124,7 +125,7 @@ func messageHandler(cmd *cobra.Command, args []string) error { return err } - modificationsStr, err := sourceModificationToString(sm) + modificationsStr, err := xgenny.SourceModificationToString(sm) if err != nil { return err } diff --git a/ignite/cmd/scaffold_module.go b/ignite/cmd/scaffold_module.go index 05b8a2d2c9..404a56067e 100644 --- a/ignite/cmd/scaffold_module.go +++ b/ignite/cmd/scaffold_module.go @@ -12,6 +12,7 @@ import ( "github.com/ignite/cli/v28/ignite/pkg/errors" "github.com/ignite/cli/v28/ignite/pkg/placeholder" "github.com/ignite/cli/v28/ignite/pkg/validation" + "github.com/ignite/cli/v28/ignite/pkg/xgenny" "github.com/ignite/cli/v28/ignite/services/scaffolder" modulecreate "github.com/ignite/cli/v28/ignite/templates/module/create" ) @@ -195,7 +196,7 @@ func scaffoldModuleHandler(cmd *cobra.Command, args []string) error { return err } } else { - modificationsStr, err := sourceModificationToString(sm) + modificationsStr, err := xgenny.SourceModificationToString(sm) if err != nil { return err } diff --git a/ignite/cmd/scaffold_package.go b/ignite/cmd/scaffold_package.go index 4b4995500c..1eb32a1802 100644 --- a/ignite/cmd/scaffold_package.go +++ b/ignite/cmd/scaffold_package.go @@ -6,6 +6,7 @@ import ( "github.com/ignite/cli/v28/ignite/pkg/cliui" "github.com/ignite/cli/v28/ignite/pkg/errors" "github.com/ignite/cli/v28/ignite/pkg/placeholder" + "github.com/ignite/cli/v28/ignite/pkg/xgenny" "github.com/ignite/cli/v28/ignite/services/scaffolder" ) @@ -77,7 +78,7 @@ func createPacketHandler(cmd *cobra.Command, args []string) error { return err } - modificationsStr, err := sourceModificationToString(sm) + modificationsStr, err := xgenny.SourceModificationToString(sm) if err != nil { return err } diff --git a/ignite/cmd/scaffold_params.go b/ignite/cmd/scaffold_params.go index 57261ca58d..1a3277cecd 100644 --- a/ignite/cmd/scaffold_params.go +++ b/ignite/cmd/scaffold_params.go @@ -7,6 +7,7 @@ import ( "github.com/ignite/cli/v28/ignite/pkg/cliui" "github.com/ignite/cli/v28/ignite/pkg/placeholder" + "github.com/ignite/cli/v28/ignite/pkg/xgenny" "github.com/ignite/cli/v28/ignite/services/scaffolder" ) @@ -69,7 +70,7 @@ func scaffoldParamsHandler(cmd *cobra.Command, args []string) error { return err } - modificationsStr, err := sourceModificationToString(sm) + modificationsStr, err := xgenny.SourceModificationToString(sm) if err != nil { return err } diff --git a/ignite/cmd/scaffold_query.go b/ignite/cmd/scaffold_query.go index 0786b068ea..83afa0c428 100644 --- a/ignite/cmd/scaffold_query.go +++ b/ignite/cmd/scaffold_query.go @@ -7,6 +7,7 @@ import ( "github.com/ignite/cli/v28/ignite/pkg/cliui" "github.com/ignite/cli/v28/ignite/pkg/placeholder" + "github.com/ignite/cli/v28/ignite/pkg/xgenny" "github.com/ignite/cli/v28/ignite/services/scaffolder" ) @@ -75,7 +76,7 @@ func queryHandler(cmd *cobra.Command, args []string) error { return err } - modificationsStr, err := sourceModificationToString(sm) + modificationsStr, err := xgenny.SourceModificationToString(sm) if err != nil { return err } diff --git a/ignite/pkg/xfilepath/xfilepath.go b/ignite/pkg/xfilepath/xfilepath.go index 19dfe14ff2..20ee9f3f2e 100644 --- a/ignite/pkg/xfilepath/xfilepath.go +++ b/ignite/pkg/xfilepath/xfilepath.go @@ -86,3 +86,16 @@ func Mkdir(path PathRetriever) PathRetriever { return p, os.MkdirAll(p, 0o755) } } + +// RelativePath return the relative app path from the current directory. +func RelativePath(appPath string) (string, error) { + pwd, err := os.Getwd() + if err != nil { + return "", err + } + path, err := filepath.Rel(pwd, appPath) + if err != nil { + return "", err + } + return path, nil +} diff --git a/ignite/pkg/xfilepath/xfilepath_test.go b/ignite/pkg/xfilepath/xfilepath_test.go index a76d5f7a03..2355433166 100644 --- a/ignite/pkg/xfilepath/xfilepath_test.go +++ b/ignite/pkg/xfilepath/xfilepath_test.go @@ -99,3 +99,56 @@ func TestMkdir(t *testing.T) { require.Equal(t, newdir, dir) require.DirExists(t, dir) } + +func TestRelativePath(t *testing.T) { + pwd, err := os.Getwd() + require.NoError(t, err) + rootRelative, err := filepath.Rel(pwd, "/") + require.NoError(t, err) + + tests := []struct { + name string + appPath string + want string + err error + }{ + { + name: "same directory", + appPath: filepath.Join(pwd, "file.go"), + want: "file.go", + }, + { + name: "previous directory", + appPath: filepath.Join(filepath.Dir(pwd), "file.go"), + want: "../file.go", + }, + { + name: "root directory", + appPath: "/file.go", + want: filepath.Join(rootRelative, "file.go"), + }, + { + name: "absolute path", + appPath: pwd, + want: ".", + }, + { + name: "NonExistentPath", + appPath: filepath.Join(filepath.Base(pwd), "file.go"), + want: "", + err: errors.Errorf("Rel: can't make xfilepath/file.go relative to %s", pwd), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := xfilepath.RelativePath(tt.appPath) + if tt.err != nil { + require.Error(t, err) + require.Equal(t, tt.err.Error(), err.Error()) + return + } + require.NoError(t, err) + require.Equal(t, tt.want, got) + }) + } +} diff --git a/ignite/pkg/xgenny/sourcemodification.go b/ignite/pkg/xgenny/sourcemodification.go index f74bb90fd0..e48a12f3d5 100644 --- a/ignite/pkg/xgenny/sourcemodification.go +++ b/ignite/pkg/xgenny/sourcemodification.go @@ -1,5 +1,21 @@ package xgenny +import ( + "sort" + "strings" + + "github.com/ignite/cli/v28/ignite/pkg/cliui/colors" + "github.com/ignite/cli/v28/ignite/pkg/xfilepath" +) + +var ( + modifyPrefix = colors.Modified("modify ") + createPrefix = colors.Success("create ") + removePrefix = func(s string) string { + return strings.TrimPrefix(strings.TrimPrefix(s, modifyPrefix), createPrefix) + } +) + // SourceModification describes modified and created files in the source code after a run. type SourceModification struct { modified map[string]struct{} @@ -56,3 +72,34 @@ func (sm *SourceModification) Merge(newSm SourceModification) { sm.AppendModifiedFiles(newSm.ModifiedFiles()...) sm.AppendCreatedFiles(newSm.CreatedFiles()...) } + +func SourceModificationToString(sm SourceModification) (string, error) { + // get file names and add prefix + var files []string + for _, modified := range sm.ModifiedFiles() { + // get the relative app path from the current directory + relativePath, err := xfilepath.RelativePath(modified) + if err != nil { + return "", err + } + files = append(files, modifyPrefix+relativePath) + } + for _, created := range sm.CreatedFiles() { + // get the relative app path from the current directory + relativePath, err := xfilepath.RelativePath(created) + if err != nil { + return "", err + } + files = append(files, createPrefix+relativePath) + } + + // sort filenames without prefix + sort.Slice(files, func(i, j int) bool { + s1 := removePrefix(files[i]) + s2 := removePrefix(files[j]) + + return strings.Compare(s1, s2) == -1 + }) + + return "\n" + strings.Join(files, "\n"), nil +} diff --git a/ignite/services/chain/chain.go b/ignite/services/chain/chain.go index fa21890767..eeae83dcc3 100644 --- a/ignite/services/chain/chain.go +++ b/ignite/services/chain/chain.go @@ -6,6 +6,7 @@ import ( "path/filepath" "github.com/go-git/go-git/v5" + "github.com/spf13/cobra" chainconfig "github.com/ignite/cli/v28/ignite/config/chain" chainconfigv1 "github.com/ignite/cli/v28/ignite/config/chain/v1" @@ -23,6 +24,11 @@ import ( igniteversion "github.com/ignite/cli/v28/ignite/version" ) +const ( + flagPath = "path" + flagHome = "home" +) + var appBackendSourceWatchPaths = []string{ "app", "cmd", @@ -31,57 +37,59 @@ var appBackendSourceWatchPaths = []string{ "third_party", } -type version struct { - tag string - hash string -} +type ( + // Chain provides programmatic access and tools for a Cosmos SDK blockchain. + Chain struct { + // app holds info about blockchain app. + app App -// Chain provides programmatic access and tools for a Cosmos SDK blockchain. -type Chain struct { - // app holds info about blockchain app. - app App + options chainOptions - options chainOptions + Version cosmosver.Version - Version cosmosver.Version + sourceVersion version + serveCancel context.CancelFunc + serveRefresher chan struct{} + served bool - sourceVersion version - serveCancel context.CancelFunc - serveRefresher chan struct{} - served bool + ev events.Bus + logOutputer uilog.Outputer + } - ev events.Bus - logOutputer uilog.Outputer -} + // chainOptions holds user given options that overwrites chain's defaults. + chainOptions struct { + // chainID is the chain's id. + chainID string -// chainOptions holds user given options that overwrites chain's defaults. -type chainOptions struct { - // chainID is the chain's id. - chainID string + // homePath of the chain's config dir. + homePath string - // homePath of the chain's config dir. - homePath string + // keyring backend used by commands if not specified in configuration + keyringBackend chaincmd.KeyringBackend - // keyring backend used by commands if not specified in configuration - keyringBackend chaincmd.KeyringBackend + // checkDependencies checks that cached Go dependencies of the chain have not + // been modified since they were downloaded. + checkDependencies bool - // checkDependencies checks that cached Go dependencies of the chain have not - // been modified since they were downloaded. - checkDependencies bool + // checkCosmosSDKVersion checks that the app was scaffolded with version of + // the Cosmos SDK that is supported by Ignite CLI. + checkCosmosSDKVersion bool - // checkCosmosSDKVersion checks that the app was scaffolded with version of - // the Cosmos SDK that is supported by Ignite CLI. - checkCosmosSDKVersion bool + // printGeneratedPaths prints the output paths of the generated code + printGeneratedPaths bool - // printGeneratedPaths prints the output paths of the generated code - printGeneratedPaths bool + // path of a custom config file + ConfigFile string + } - // path of a custom config file - ConfigFile string -} + version struct { + tag string + hash string + } -// Option configures Chain. -type Option func(*Chain) + // Option configures Chain. + Option func(*Chain) +) // ID replaces chain's id with given id. func ID(id string) Option { @@ -185,6 +193,24 @@ func New(path string, options ...Option) (*Chain, error) { return c, nil } +func NewWithHomeFlags(cmd *cobra.Command, chainOption ...Option) (*Chain, error) { + var ( + home, _ = cmd.Flags().GetString(flagHome) + appPath, _ = cmd.Flags().GetString(flagPath) + ) + + absPath, err := filepath.Abs(appPath) + if err != nil { + return nil, err + } + + // Check if custom home is provided + if home != "" { + chainOption = append(chainOption, HomePath(home)) + } + return New(absPath, chainOption...) +} + func (c *Chain) appVersion() (v version, err error) { ver, err := repoversion.Determine(c.app.Path) if err != nil {