Skip to content

Commit 63c74e7

Browse files
Merge pull request #69 from Chia-Network/show-my-mirrors
Add command show-my-mirrors
2 parents f3f450d + 540f40e commit 63c74e7

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed

cmd/datalayer/showmymirrors.go

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package datalayer
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/chia-network/go-chia-libs/pkg/rpc"
8+
"github.com/chia-network/go-chia-libs/pkg/types"
9+
"github.com/chia-network/go-modules/pkg/slogs"
10+
"github.com/spf13/cobra"
11+
"github.com/spf13/viper"
12+
)
13+
14+
// showMyMirrorsCmd shows all mirrors owned by the user across all or specific stores
15+
var showMyMirrorsCmd = &cobra.Command{
16+
Use: "show-my-mirrors",
17+
Short: "Shows all mirrors owned by the user across all or specific stores",
18+
Example: `chia-tools data show-my-mirrors
19+
chia-tools data show-my-mirrors --id abcd1234`,
20+
Run: func(cmd *cobra.Command, args []string) {
21+
client, err := rpc.NewClient(rpc.ConnectionModeHTTP, rpc.WithAutoConfig())
22+
if err != nil {
23+
slogs.Logr.Fatal("error creating chia RPC client", "error", err)
24+
}
25+
26+
subID := viper.GetString("show-mirrors-id")
27+
if subID != "" {
28+
// Show mirrors for specific store
29+
mirrors, _, err := client.DataLayerService.GetMirrors(&rpc.DatalayerGetMirrorsOptions{
30+
ID: subID,
31+
})
32+
if err != nil {
33+
slogs.Logr.Fatal("error fetching mirrors for subscription", "store", subID, "error", err)
34+
}
35+
36+
var ownedMirrors []types.DatalayerMirror
37+
for _, mirror := range mirrors.Mirrors {
38+
if mirror.Ours {
39+
ownedMirrors = append(ownedMirrors, mirror)
40+
}
41+
}
42+
43+
if len(ownedMirrors) == 0 {
44+
fmt.Println("No owned mirrors found for store")
45+
return
46+
}
47+
48+
// Create output structure
49+
output := struct {
50+
Subscriptions []struct {
51+
StoreID string `json:"store_id"`
52+
Mirrors []types.DatalayerMirror `json:"mirrors"`
53+
} `json:"subscriptions"`
54+
}{
55+
Subscriptions: []struct {
56+
StoreID string `json:"store_id"`
57+
Mirrors []types.DatalayerMirror `json:"mirrors"`
58+
}{
59+
{
60+
StoreID: subID,
61+
Mirrors: ownedMirrors,
62+
},
63+
},
64+
}
65+
66+
// Convert to JSON with nice formatting
67+
jsonOutput, err := json.MarshalIndent(output, "", " ")
68+
if err != nil {
69+
slogs.Logr.Fatal("error marshaling mirrors to JSON", "error", err)
70+
}
71+
72+
fmt.Println(string(jsonOutput))
73+
return
74+
}
75+
76+
// Show mirrors for all stores
77+
subscriptions, _, err := client.DataLayerService.GetSubscriptions(&rpc.DatalayerGetSubscriptionsOptions{})
78+
if err != nil {
79+
slogs.Logr.Fatal("error getting list of datalayer subscriptions", "error", err)
80+
}
81+
82+
// Create output structure
83+
output := struct {
84+
Subscriptions []struct {
85+
StoreID string `json:"store_id"`
86+
Mirrors []types.DatalayerMirror `json:"mirrors"`
87+
} `json:"subscriptions"`
88+
}{}
89+
90+
foundAnyMirrors := false
91+
for _, subscription := range subscriptions.StoreIDs {
92+
mirrors, _, err := client.DataLayerService.GetMirrors(&rpc.DatalayerGetMirrorsOptions{
93+
ID: subscription,
94+
})
95+
if err != nil {
96+
slogs.Logr.Fatal("error fetching mirrors for subscription", "store", subscription, "error", err)
97+
}
98+
99+
var ownedMirrors []types.DatalayerMirror
100+
for _, mirror := range mirrors.Mirrors {
101+
if mirror.Ours {
102+
ownedMirrors = append(ownedMirrors, mirror)
103+
}
104+
}
105+
106+
if len(ownedMirrors) > 0 {
107+
foundAnyMirrors = true
108+
output.Subscriptions = append(output.Subscriptions, struct {
109+
StoreID string `json:"store_id"`
110+
Mirrors []types.DatalayerMirror `json:"mirrors"`
111+
}{
112+
StoreID: subscription,
113+
Mirrors: ownedMirrors,
114+
})
115+
}
116+
}
117+
118+
if !foundAnyMirrors {
119+
fmt.Println("No owned mirrors found for any store")
120+
return
121+
}
122+
123+
// Convert to JSON with nice formatting
124+
jsonOutput, err := json.MarshalIndent(output, "", " ")
125+
if err != nil {
126+
slogs.Logr.Fatal("error marshaling mirrors to JSON", "error", err)
127+
}
128+
129+
fmt.Println(string(jsonOutput))
130+
},
131+
}
132+
133+
func init() {
134+
showMyMirrorsCmd.PersistentFlags().String("id", "", "The subscription ID to show mirrors for")
135+
136+
cobra.CheckErr(viper.BindPFlag("show-mirrors-id", showMyMirrorsCmd.PersistentFlags().Lookup("id")))
137+
138+
datalayerCmd.AddCommand(showMyMirrorsCmd)
139+
}

0 commit comments

Comments
 (0)