|
| 1 | +package rofl |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "sort" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + |
| 10 | + "github.com/oasisprotocol/oasis-sdk/client-sdk/go/client" |
| 11 | + "github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection" |
| 12 | + "github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/rofl" |
| 13 | + |
| 14 | + "github.com/oasisprotocol/cli/cmd/common" |
| 15 | + cliConfig "github.com/oasisprotocol/cli/config" |
| 16 | + "github.com/oasisprotocol/cli/table" |
| 17 | +) |
| 18 | + |
| 19 | +var listCmd = &cobra.Command{ |
| 20 | + Use: "list", |
| 21 | + Aliases: []string{"ls"}, |
| 22 | + Short: "List ROFL applications", |
| 23 | + Long: `List all ROFL applications registered on the selected paratime. |
| 24 | +
|
| 25 | +This command queries on-chain ROFL application data and displays application IDs, |
| 26 | +names, and admin addresses. |
| 27 | +
|
| 28 | +Use --format json for machine-readable output.`, |
| 29 | + Args: cobra.NoArgs, |
| 30 | + Run: func(_ *cobra.Command, _ []string) { |
| 31 | + cfg := cliConfig.Global() |
| 32 | + npa := common.GetNPASelection(cfg) |
| 33 | + npa.MustHaveParaTime() |
| 34 | + |
| 35 | + ctx := context.Background() |
| 36 | + conn, err := connection.Connect(ctx, npa.Network) |
| 37 | + cobra.CheckErr(err) |
| 38 | + |
| 39 | + // Query all ROFL apps from the chain. |
| 40 | + apps, err := conn.Runtime(npa.ParaTime).ROFL.Apps(ctx, client.RoundLatest) |
| 41 | + if err != nil { |
| 42 | + cobra.CheckErr(fmt.Errorf("failed to query ROFL apps: %w", err)) |
| 43 | + } |
| 44 | + |
| 45 | + // Sort apps by ID for consistent output. |
| 46 | + sort.Slice(apps, func(i, j int) bool { |
| 47 | + return apps[i].ID.String() < apps[j].ID.String() |
| 48 | + }) |
| 49 | + |
| 50 | + // Output format handling. |
| 51 | + if common.OutputFormat() == common.FormatJSON { |
| 52 | + outputAppsJSON(apps) |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + if len(apps) == 0 { |
| 57 | + fmt.Println("No ROFL applications found.") |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + outputAppsText(apps) |
| 62 | + }, |
| 63 | +} |
| 64 | + |
| 65 | +// outputAppsJSON returns apps in JSON format. |
| 66 | +func outputAppsJSON(apps []*rofl.AppConfig) { |
| 67 | + output := make([]common.JSONPrettyPrintRoflAppConfig, 0, len(apps)) |
| 68 | + for _, app := range apps { |
| 69 | + output = append(output, common.JSONPrettyPrintRoflAppConfig(*app)) |
| 70 | + } |
| 71 | + |
| 72 | + data, err := common.PrettyJSONMarshal(output) |
| 73 | + cobra.CheckErr(err) |
| 74 | + fmt.Println(string(data)) |
| 75 | +} |
| 76 | + |
| 77 | +// outputAppsText returns apps in human-readable table format. |
| 78 | +func outputAppsText(apps []*rofl.AppConfig) { |
| 79 | + tbl := table.New() |
| 80 | + tbl.Header("App ID", "Name", "Version", "Admin") |
| 81 | + |
| 82 | + rows := make([][]string, 0, len(apps)) |
| 83 | + for _, app := range apps { |
| 84 | + // Extract name from metadata. |
| 85 | + name := app.Metadata["net.oasis.rofl.name"] |
| 86 | + |
| 87 | + // Extract version from metadata. |
| 88 | + version := app.Metadata["net.oasis.rofl.version"] |
| 89 | + |
| 90 | + // Format admin address. |
| 91 | + var admin string |
| 92 | + if app.Admin != nil { |
| 93 | + admin = app.Admin.String() |
| 94 | + } |
| 95 | + |
| 96 | + rows = append(rows, []string{ |
| 97 | + app.ID.String(), |
| 98 | + name, |
| 99 | + version, |
| 100 | + admin, |
| 101 | + }) |
| 102 | + } |
| 103 | + |
| 104 | + cobra.CheckErr(tbl.Bulk(rows)) |
| 105 | + cobra.CheckErr(tbl.Render()) |
| 106 | +} |
| 107 | + |
| 108 | +func init() { |
| 109 | + common.AddSelectorNPFlags(listCmd) |
| 110 | + listCmd.Flags().AddFlagSet(common.FormatFlag) |
| 111 | +} |
0 commit comments