Skip to content

Commit 1150e8d

Browse files
authored
Merge pull request #674 from oasisprotocol/ptrus/feature/rofl-list
Implement ROFL list command
2 parents e9a6062 + 09496d2 commit 1150e8d

File tree

6 files changed

+23640
-0
lines changed

6 files changed

+23640
-0
lines changed

cmd/rofl/list.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
}

cmd/rofl/rofl.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func init() {
2222
Cmd.AddCommand(pushCmd)
2323
Cmd.AddCommand(removeCmd)
2424
Cmd.AddCommand(showCmd)
25+
Cmd.AddCommand(listCmd)
2526
Cmd.AddCommand(trustRootCmd)
2627
Cmd.AddCommand(build.Cmd)
2728
Cmd.AddCommand(identityCmd)

examples/rofl/list-json.in.static

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
oasis rofl list --format json

0 commit comments

Comments
 (0)