|
| 1 | +package ignitecmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "sort" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | + |
| 9 | + "github.com/ignite/cli/v29/ignite/pkg/cliui" |
| 10 | + "github.com/ignite/cli/v29/ignite/pkg/cosmosanalysis/app" |
| 11 | + "github.com/ignite/cli/v29/ignite/pkg/gomodule" |
| 12 | + "github.com/ignite/cli/v29/ignite/services/chain" |
| 13 | +) |
| 14 | + |
| 15 | +func NewChainModulesList() *cobra.Command { |
| 16 | + c := &cobra.Command{ |
| 17 | + Use: "list", |
| 18 | + Short: "List all Cosmos SDK modules in the app", |
| 19 | + Long: "The list command lists all modules in the app.", |
| 20 | + Args: cobra.NoArgs, |
| 21 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 22 | + session := cliui.New(cliui.StartSpinner()) |
| 23 | + defer session.End() |
| 24 | + |
| 25 | + chainOption := []chain.Option{ |
| 26 | + chain.WithOutputer(session), |
| 27 | + chain.CollectEvents(session.EventBus()), |
| 28 | + } |
| 29 | + |
| 30 | + c, err := chain.NewWithHomeFlags(cmd, chainOption...) |
| 31 | + if err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + |
| 35 | + modules, err := app.FindRegisteredModules(c.AppPath()) |
| 36 | + if err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + |
| 40 | + if len(modules) == 0 { |
| 41 | + session.Println("no modules found") |
| 42 | + return nil |
| 43 | + } |
| 44 | + |
| 45 | + modFile, err := gomodule.ParseAt(c.AppPath()) |
| 46 | + if err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + |
| 50 | + deps, err := gomodule.ResolveDependencies(modFile, false) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + depMap := make(map[string]string) |
| 56 | + for _, dep := range deps { |
| 57 | + depMap[dep.Path] = dep.Version |
| 58 | + } |
| 59 | + |
| 60 | + // create a map of replaced modules for easy lookup |
| 61 | + // check the original required modules, not the resolved ones |
| 62 | + replacedMap := make(map[string]bool) |
| 63 | + for _, replace := range modFile.Replace { |
| 64 | + replacedMap[replace.Old.Path] = true |
| 65 | + } |
| 66 | + |
| 67 | + // get the app's module path to identify app modules |
| 68 | + appModulePath := modFile.Module.Mod.Path |
| 69 | + |
| 70 | + var entries [][]string |
| 71 | + for _, m := range modules { |
| 72 | + ver := depMap[m] |
| 73 | + modName := m |
| 74 | + |
| 75 | + switch { |
| 76 | + case strings.HasPrefix(m, appModulePath+"/"): |
| 77 | + ver = "main" |
| 78 | + case strings.HasPrefix(m, cosmosSDKModulePrefix+"/"): |
| 79 | + ver = depMap[cosmosSDKModulePrefix] |
| 80 | + modName = strings.TrimPrefix(m, cosmosSDKModulePrefix+"/") |
| 81 | + case strings.Contains(m, ibcModulePrefix+"/v"): |
| 82 | + modName, ver = getIBCVersion(m, depMap) |
| 83 | + case isModuleReplaced(m, replacedMap): |
| 84 | + ver = "locally replaced" |
| 85 | + } |
| 86 | + |
| 87 | + if ver == "" { |
| 88 | + ver = findBestMatchingVersion(m, depMap) |
| 89 | + if ver == "" { |
| 90 | + ver = "-" |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + entries = append(entries, []string{modName, ver}) |
| 95 | + } |
| 96 | + |
| 97 | + session.StopSpinner() |
| 98 | + |
| 99 | + // Sort entries by module name |
| 100 | + sort.SliceStable(entries, func(i, j int) bool { |
| 101 | + return entries[i][0] < entries[j][0] |
| 102 | + }) |
| 103 | + |
| 104 | + header := []string{"module", "version"} |
| 105 | + return session.PrintTable(header, entries...) |
| 106 | + }, |
| 107 | + } |
| 108 | + |
| 109 | + return c |
| 110 | +} |
| 111 | + |
| 112 | +const ( |
| 113 | + cosmosSDKModulePrefix = "github.com/cosmos/cosmos-sdk" |
| 114 | + ibcModulePrefix = "github.com/cosmos/ibc-go" |
| 115 | +) |
| 116 | + |
| 117 | +// isModuleReplaced checks if a module path (or its parent paths) is in the replaced map. |
| 118 | +func isModuleReplaced(modulePath string, replacedMap map[string]bool) bool { |
| 119 | + checkPath := modulePath |
| 120 | + for checkPath != "" && checkPath != "." { |
| 121 | + if replacedMap[checkPath] { |
| 122 | + return true |
| 123 | + } |
| 124 | + // check parent path |
| 125 | + if idx := strings.LastIndex(checkPath, "/"); idx > 0 { |
| 126 | + checkPath = checkPath[:idx] |
| 127 | + } else { |
| 128 | + break |
| 129 | + } |
| 130 | + } |
| 131 | + return false |
| 132 | +} |
| 133 | + |
| 134 | +// for a given module path by checking progressively shorter paths. |
| 135 | +func findBestMatchingVersion(modulePath string, depMap map[string]string) string { |
| 136 | + checkPath := modulePath |
| 137 | + for checkPath != "" && checkPath != "." { |
| 138 | + if version, exists := depMap[checkPath]; exists { |
| 139 | + return version |
| 140 | + } |
| 141 | + // check parent path |
| 142 | + if idx := strings.LastIndex(checkPath, "/"); idx > 0 { |
| 143 | + checkPath = checkPath[:idx] |
| 144 | + } else { |
| 145 | + break |
| 146 | + } |
| 147 | + } |
| 148 | + return "" |
| 149 | +} |
| 150 | + |
| 151 | +// getIBCVersion tries to extract the ibc-go version from the module path or dependencies. |
| 152 | +func getIBCVersion(modulePath string, depMap map[string]string) (string, string) { |
| 153 | + // find the root ibc-go module path (with major version) |
| 154 | + parts := strings.Split(modulePath, "/") |
| 155 | + for i := range parts { |
| 156 | + if parts[i] == "ibc-go" && i+1 < len(parts) && strings.HasPrefix(parts[i+1], "v") { |
| 157 | + root := strings.Join(parts[:i+2], "/") |
| 158 | + ver := depMap[root] |
| 159 | + // clean module name after root |
| 160 | + mod := strings.TrimPrefix(modulePath, root+"/") |
| 161 | + return mod, ver |
| 162 | + } |
| 163 | + } |
| 164 | + return modulePath, "" |
| 165 | +} |
0 commit comments