Skip to content

Commit

Permalink
feat(cmd): make some cmd pkg functions public (#3985)
Browse files Browse the repository at this point in the history
* 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 <Pantani>
  • Loading branch information
Pantani authored Feb 23, 2024
1 parent 2de08af commit a27cd72
Show file tree
Hide file tree
Showing 28 changed files with 210 additions and 132 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/chain_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/chain_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/chain_faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/chain_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/chain_lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/chain_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
72 changes: 0 additions & 72 deletions ignite/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"os"
"path/filepath"
"slices"
"sort"
"strings"
"time"

"github.com/spf13/cobra"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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{
{
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/generate_composables.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/generate_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/generate_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/generate_openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/generate_typescript_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/generate_vuex.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
3 changes: 2 additions & 1 deletion ignite/cmd/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion ignite/cmd/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -223,7 +224,7 @@ func scaffoldType(
return err
}

modificationsStr, err := sourceModificationToString(sm)
modificationsStr, err := xgenny.SourceModificationToString(sm)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion ignite/cmd/scaffold_band.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion ignite/cmd/scaffold_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion ignite/cmd/scaffold_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion ignite/cmd/scaffold_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion ignite/cmd/scaffold_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion ignite/cmd/scaffold_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion ignite/cmd/scaffold_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion ignite/cmd/scaffold_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down
Loading

0 comments on commit a27cd72

Please sign in to comment.