/
query.go
1202 lines (1078 loc) · 39.3 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package cli
import (
"context"
"errors"
"fmt"
"regexp"
"strings"
"github.com/google/uuid"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/types/bech32"
"github.com/cosmos/cosmos-sdk/version"
"github.com/provenance-io/provenance/x/metadata/types"
)
// cmdStart is the first part of the command that gets you to one of the ones in here.
var cmdStart = fmt.Sprintf("%s query metadata", version.AppName)
// These vars are tied to flags that are added to many commands in here.
var (
includeScope bool
includeSessions bool
includeRecords bool
includeContractSpecs bool
includeRecordSpecs bool
excludeIDInfo bool
includeRequest bool
)
const all = "all"
// GetQueryCmd returns the top-level command for marker CLI queries.
func GetQueryCmd() *cobra.Command {
queryCmd := &cobra.Command{
Use: types.ModuleName,
Aliases: []string{"md"},
Short: "Querying commands for the metadata module",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
queryCmd.AddCommand(
GetMetadataParamsCmd(),
GetMetadataByIDCmd(),
GetMetadataGetAllCmd(),
GetMetadataScopeCmd(),
GetMetadataSessionCmd(),
GetMetadataRecordCmd(),
GetMetadataScopeSpecCmd(),
GetMetadataContractSpecCmd(),
GetMetadataRecordSpecCmd(),
GetOwnershipCmd(),
GetValueOwnershipCmd(),
GetOSLocatorCmd(),
GetAccountDataCmd(),
GetCmdNetAssetValuesQuery(),
)
return queryCmd
}
// ------------ Individual Commands ------------
// GetMetadataParamsCmd returns the command handler for metadata parameter querying.
func GetMetadataParamsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "params [locator]",
Aliases: []string{"p"},
Short: "Query the current metadata parameters",
Args: cobra.MaximumNArgs(1),
Example: fmt.Sprintf("%s params", cmdStart),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return outputParams(cmd)
}
arg0 := strings.TrimSpace(args[0])
if arg0 == "locator" {
return outputOSLocatorParams(cmd)
}
return fmt.Errorf("unknown argument: %s", arg0)
},
}
addIncludeRequestFlag(cmd)
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetMetadataByIDCmd returns the command handler for querying metadata for anything from an id.
func GetMetadataByIDCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "get <id> [<id2> ...]",
Aliases: []string{"g"},
Short: "Get metadata by address(es)",
Args: cobra.MinimumNArgs(1),
Example: fmt.Sprintf(`%[1]s get scope1qzge0zaztu65tx5x5llv5xc9ztsqxlkwel
%[1]s get session1qxge0zaztu65tx5x5llv5xc9zts9sqlch3sxwn44j50jzgt8rshvqyfrjcr
%[1]s get record1q2ge0zaztu65tx5x5llv5xc9ztsw42dq2jdvmdazuwzcaddhh8gmu3mcze3
%[1]s get scopespec1qnwg86nsatx5pl56muw0v9ytlz3qu3jx6m
%[1]s get contractspec1q000d0q2e8w5say53afqdesxp2zqzkr4fn
%[1]s get recspec1qh00d0q2e8w5say53afqdesxp2zw42dq2jdvmdazuwzcaddhh8gmuqhez44
%[1]s get scope1qzge0zaztu65tx5x5llv5xc9ztsqxlkwel record1q2ge0zaztu65tx5x5llv5xc9ztsw42dq2jdvmdazuwzcaddhh8gmu3mcze3`,
cmdStart),
RunE: func(cmd *cobra.Command, args []string) error {
for i, addr := range args {
if _, err := types.MetadataAddressFromBech32(addr); err != nil {
return fmt.Errorf("invalid metadata address %d, %q: %w", i+1, addr, err)
}
}
return outputGetByAddr(cmd, args)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetMetadataGetAllCmd returns the command handler for querying metadata for all entries of a type.
func GetMetadataGetAllCmd() *cobra.Command {
nonLetterRegex := regexp.MustCompile("[^[:alpha:]]+")
cmd := &cobra.Command{
Use: "all {scopes|sessions|records|scopespecs|contractspecs|recordspecs|locators}",
Aliases: []string{"a"},
Short: "Get all entries of a certain type",
Long: fmt.Sprintf(`%[1]s all scopes - gets all scopes.
%[1]s all sessions - gets all sessions.
%[1]s all records - gets all records.
%[1]s all scopespecs - gets all scope specifications.
%[1]s all contractspecs - gets all contract specifications.
%[1]s all recordspecs - gets all record specifications.
%[1]s all locators - gets all object store locators.`, cmdStart),
Example: fmt.Sprintf(`%[1]s all scopes
%[1]s all sessions
%[1]s all records
%[1]s all scopespecs
%[1]s all contractspecs
%[1]s all recordspecs
%[1]s all locators`, cmdStart),
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// Smash all the args together. We only want a single "word" anyway.
input := strings.ToLower(trimSpaceAndJoin(args, ""))
// Get rid of non-letters
// This simplifies the switch below, e.g. record-specs becomes recordspecs.
input = nonLetterRegex.ReplaceAllString(input, "")
// Make sure it ends with an "s"
// This simplifies the switch below, e.g. "scope" becomes "scopes"
if input[len(input)-1:] != "s" {
input += "s"
}
switch input {
case "scopes":
return outputScopesAll(cmd)
case "sessions", "sess":
return outputSessionsAll(cmd)
case "records", "recs":
return outputRecordsAll(cmd)
case "scopespecs", "scopespecifications":
return outputScopeSpecsAll(cmd)
case "contractspecs", "cspecs", "contractspecifications":
return outputContractSpecsAll(cmd)
case "recordspecs", "recspecs", "recordspecifications", "recspecifications":
return outputRecordSpecsAll(cmd)
case "locators", "locs":
return outputOSLocatorsAll(cmd)
}
return fmt.Errorf("unknown entry type: %s", input)
},
}
addExcludeIDInfoFlag(cmd)
addIncludeRequestFlag(cmd)
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "entries")
return cmd
}
// GetMetadataScopeCmd returns the command handler for metadata scope querying.
func GetMetadataScopeCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "scope {scope_id|scope_uuid|session_id|record_id|\"all\"}",
Aliases: []string{"sc", "scopes"},
Short: "Query the current metadata for a scope",
Long: fmt.Sprintf(`%[1]s scope {scope_id} - gets the scope with the given id.
%[1]s scope {scope_uuid} - gets the scope with the given uuid.
%[1]s scope {session_id} - gets the scope containing the given session.
%[1]s scope {record_id} - gets the scope containing the given record.
%[1]s scope all - gets all scopes.`, cmdStart),
Args: cobra.ExactArgs(1),
Example: fmt.Sprintf(`%[1]s scope scope1qzge0zaztu65tx5x5llv5xc9ztsqxlkwel
%[1]s scope 91978ba2-5f35-459a-86a7-feca1b0512e0
%[1]s scope session1qxge0zaztu65tx5x5llv5xc9zts9sqlch3sxwn44j50jzgt8rshvqyfrjcr
%[1]s scope record1q2ge0zaztu65tx5x5llv5xc9ztsw42dq2jdvmdazuwzcaddhh8gmu3mcze3
%[1]s scope all`, cmdStart),
RunE: func(cmd *cobra.Command, args []string) error {
arg0 := strings.TrimSpace(args[0])
if arg0 == all {
return outputScopesAll(cmd)
}
id, idErr := types.MetadataAddressFromBech32(arg0)
if idErr == nil {
switch {
case id.IsScopeAddress():
return outputScope(cmd, id.String(), "", "")
case id.IsSessionAddress():
return outputScope(cmd, "", id.String(), "")
case id.IsRecordAddress():
return outputScope(cmd, "", "", id.String())
}
}
return outputScope(cmd, arg0, "", "")
},
}
addIncludeSessionsFlag(cmd)
addIncludeRecordsFlag(cmd)
addExcludeIDInfoFlag(cmd)
addIncludeRequestFlag(cmd)
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "scopes (all)")
return cmd
}
// GetMetadataSessionCmd returns the command handler for metadata session querying.
func GetMetadataSessionCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "session {session_id|{scope_id|scope_uuid} [session_uuid|record_name]|record_id|\"all\"}",
Aliases: []string{"se", "sessions"},
Short: "Query the current metadata for sessions",
Long: fmt.Sprintf(`%[1]s session {session_id} - gets the session with the given id.
%[1]s session {scope_id} - gets the list of sessions associated with a scope.
%[1]s session {scope_id} {session_uuid} - gets a session with the given scope and session uuid.
%[1]s session {scope_id} {record_name} - gets the session in the given scope containing the given record.
%[1]s session {scope_uuid} - gets the list of sessions associated with a scope.
%[1]s session {scope_uuid} {session_uuid} - gets a session with the given scope uuid and session uuid.
%[1]s session {scope_uuid} {record_name} - gets the session in the given scope containing the given record.
%[1]s session {record_id} - gets the session containing the given record.
%[1]s session all - gets all sessions.`, cmdStart),
Args: cobra.RangeArgs(1, 2),
Example: fmt.Sprintf(`%[1]s session session1qxge0zaztu65tx5x5llv5xc9zts9sqlch3sxwn44j50jzgt8rshvqyfrjcr
%[1]s session scope1qzge0zaztu65tx5x5llv5xc9ztsqxlkwel
%[1]s session scope1qzge0zaztu65tx5x5llv5xc9ztsqxlkwel 5803f8bc-6067-4eb5-951f-2121671c2ec0
%[1]s session scope1qzge0zaztu65tx5x5llv5xc9ztsqxlkwel recordname
%[1]s session 91978ba2-5f35-459a-86a7-feca1b0512e0
%[1]s session 91978ba2-5f35-459a-86a7-feca1b0512e0 5803f8bc-6067-4eb5-951f-2121671c2ec0
%[1]s session 91978ba2-5f35-459a-86a7-feca1b0512e0 recordname
%[1]s session record1q2ge0zaztu65tx5x5llv5xc9ztsw42dq2jdvmdazuwzcaddhh8gmu3mcze3
%[1]s session all`, cmdStart),
RunE: func(cmd *cobra.Command, args []string) error {
arg0 := strings.TrimSpace(args[0])
if arg0 == all {
return outputSessionsAll(cmd)
}
if len(args) == 2 {
arg1 := strings.TrimSpace(args[1])
if sessionUUID, err := uuid.Parse(arg1); err == nil {
return outputSessions(cmd, arg0, sessionUUID.String(), "", "")
}
return outputSessions(cmd, arg0, "", "", arg1)
}
id, idErr := types.MetadataAddressFromBech32(arg0)
if idErr == nil {
switch {
case id.IsScopeAddress():
return outputSessions(cmd, id.String(), "", "", "")
case id.IsSessionAddress():
return outputSessions(cmd, "", id.String(), "", "")
case id.IsRecordAddress():
return outputSessions(cmd, "", "", id.String(), "")
}
}
return outputSessions(cmd, arg0, "", "", "")
},
}
addIncludeScopeFlag(cmd)
addIncludeRecordsFlag(cmd)
addExcludeIDInfoFlag(cmd)
addIncludeRequestFlag(cmd)
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "sessions (all)")
return cmd
}
// GetMetadataRecordCmd returns the command handler for metadata record querying.
func GetMetadataRecordCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "record {record_id|{session_id|scope_id|scope_uuid} [record_name]|\"all\"}",
Aliases: []string{"r", "records"},
Short: "Query the current metadata for records",
Long: fmt.Sprintf(`%[1]s record {record_id} - gets the record with the given id.
%[1]s record {session_id} - gets the list of records associated with a session id.
%[1]s record {session_id} {record_name} - gets the record with the given name the scope containing that session.
%[1]s record {scope_id} - gets the list of records associated with a scope id.
%[1]s record {scope_id} {record_name} - gets the record with the given name from the given scope.
%[1]s record {scope_uuid} - gets the list of records associated with a scope uuid.
%[1]s record {scope_uuid} {record_name} - gets the record with the given name from the given scope.
%[1]s record all - all records.`, cmdStart),
Args: cobra.MinimumNArgs(1),
Example: fmt.Sprintf(`%[1]s record record1q2ge0zaztu65tx5x5llv5xc9ztsw42dq2jdvmdazuwzcaddhh8gmu3mcze3
%[1]s record session1qxge0zaztu65tx5x5llv5xc9zts9sqlch3sxwn44j50jzgt8rshvqyfrjcr
%[1]s record session1qxge0zaztu65tx5x5llv5xc9zts9sqlch3sxwn44j50jzgt8rshvqyfrjcr recordname
%[1]s record scope1qzge0zaztu65tx5x5llv5xc9ztsqxlkwel
%[1]s record scope1qzge0zaztu65tx5x5llv5xc9ztsqxlkwel recordname
%[1]s record 91978ba2-5f35-459a-86a7-feca1b0512e0
%[1]s record 91978ba2-5f35-459a-86a7-feca1b0512e0 recordname
%[1]s record all`, cmdStart),
RunE: func(cmd *cobra.Command, args []string) error {
arg0 := strings.TrimSpace(args[0])
if arg0 == all {
return outputRecordsAll(cmd)
}
name := ""
if len(args) > 1 {
name = trimSpaceAndJoin(args[1:], " ")
}
id, idErr := types.MetadataAddressFromBech32(arg0)
if idErr == nil {
switch {
case id.IsRecordAddress():
return outputRecords(cmd, id.String(), "", "", name)
case id.IsScopeAddress():
return outputRecords(cmd, "", id.String(), "", name)
case id.IsSessionAddress():
return outputRecords(cmd, "", "", id.String(), name)
}
}
return outputRecords(cmd, "", arg0, "", name)
},
}
addIncludeScopeFlag(cmd)
addIncludeSessionsFlag(cmd)
addExcludeIDInfoFlag(cmd)
addIncludeRequestFlag(cmd)
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "records (all)")
return cmd
}
// GetMetadataScopeSpecCmd returns the command handler for metadata scope specification querying.
func GetMetadataScopeSpecCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "scopespec {scope_spec_id|scope_spec_uuid|\"all\"}",
Aliases: []string{"ss", "scopespecification", "scopespecs", "scopespecifications"},
Short: "Query the current metadata for a scope specification",
Long: fmt.Sprintf(`%[1]s scopespec {scope_spec_id} - gets the scope specification for that a given id.
%[1]s scopespec {scope_spec_uuid} - gets the scope specification for a given uuid.
%[1]s scopespec all - gets all the scope specifications.`, cmdStart),
Example: fmt.Sprintf(`%[1]s scopespec scopespec1qnwg86nsatx5pl56muw0v9ytlz3qu3jx6m
%[1]s scopespec dc83ea70-eacd-40fe-9adf-1cf6148bf8a2
%[1]s scopespec all`, cmdStart),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
arg0 := strings.TrimSpace(args[0])
if arg0 == all {
return outputScopeSpecsAll(cmd)
}
return outputScopeSpec(cmd, arg0)
},
}
addIncludeContractSpecsFlag(cmd)
addIncludeRecordSpecsFlag(cmd)
addExcludeIDInfoFlag(cmd)
addIncludeRequestFlag(cmd)
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "scope specifications (all)")
return cmd
}
// GetMetadataContractSpecCmd returns the command handler for metadata contract specification querying.
func GetMetadataContractSpecCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "contractspec {contract_spec_id|contract_spec_uuid|record_spec_id|\"all\"}",
Aliases: []string{"cs", "contractspecification", "contractspecs", "contractspecifications"},
Short: "Query the current metadata for a contract specification",
Long: fmt.Sprintf(`%[1]s contractspec {contract_spec_id} - gets the contract specification for that a given id.
%[1]s contractspec {contract_spec_uuid} - gets the contract specification for a given uuid.
%[1]s contractspec {record_spec_id} - gets the contract specification associated with that record spec id.
%[1]s contractspec all - gets all the contract specifications.`, cmdStart),
Args: cobra.ExactArgs(1),
Example: fmt.Sprintf(`%[1]s contractspec contractspec1q000d0q2e8w5say53afqdesxp2zqzkr4fn
%[1]s contractspec def6bc0a-c9dd-4874-948f-5206e6060a84
%[1]s contractspec recspec1qh00d0q2e8w5say53afqdesxp2zw42dq2jdvmdazuwzcaddhh8gmuqhez44
%[1]s contractspec all`, cmdStart),
RunE: func(cmd *cobra.Command, args []string) error {
arg0 := strings.TrimSpace(args[0])
if arg0 == all {
return outputContractSpecsAll(cmd)
}
return outputContractSpec(cmd, arg0)
},
}
addIncludeRecordSpecsFlag(cmd)
addExcludeIDInfoFlag(cmd)
addIncludeRequestFlag(cmd)
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "contract specifications (all)")
return cmd
}
// GetMetadataRecordSpecCmd returns the command handler for metadata record specification querying.
func GetMetadataRecordSpecCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "recordspec {rec_spec_id|{contract_spec_id|contract_spec_uuid} [record_name]|\"all\"}",
Aliases: []string{"rs", "recordspecs", "recspec", "recspecs", "recordspecification", "recordspecifications"},
Short: "Query the current metadata for record specifications",
Long: fmt.Sprintf(`%[1]s recordspec {rec_spec_id} - gets the record specification for a given id.
%[1]s recordspec {contract_spec_id} - gets the list of record specifications for the given contract specification.
%[1]s recordspec {contract_spec_id} {record_name} - gets the record specification for a given contract specification and record name.
%[1]s recordspec {contract_spec_uuid} - gets the list of record specifications for the given contract specification.
%[1]s recordspec {contract_spec_uuid} {record_name} - gets the record specification for a given contract specification and record name.
%[1]s recordspec all - gets all the record specifications`, cmdStart),
Args: cobra.MinimumNArgs(1),
Example: fmt.Sprintf(`%[1]s recordspec recspec1qh00d0q2e8w5say53afqdesxp2zw42dq2jdvmdazuwzcaddhh8gmuqhez44
%[1]s recordspec contractspec1q000d0q2e8w5say53afqdesxp2zqzkr4fn
%[1]s recordspec contractspec1q000d0q2e8w5say53afqdesxp2zqzkr4fn recordname
%[1]s recordspec def6bc0a-c9dd-4874-948f-5206e6060a84
%[1]s recordspec def6bc0a-c9dd-4874-948f-5206e6060a84 recordname
%[1]s recordspec all`, cmdStart),
RunE: func(cmd *cobra.Command, args []string) error {
arg0 := strings.TrimSpace(args[0])
if arg0 == all {
return outputRecordSpecsAll(cmd)
}
name := ""
if len(args) > 1 {
name = trimSpaceAndJoin(args[1:], " ")
}
if len(name) == 0 {
return outputRecordSpecsForContractSpec(cmd, arg0)
}
return outputRecordSpec(cmd, arg0, name)
},
}
addExcludeIDInfoFlag(cmd)
addIncludeRequestFlag(cmd)
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "record specifications (all)")
return cmd
}
// GetOwnershipCmd returns the command handler for metadata entry querying by owner address
func GetOwnershipCmd() *cobra.Command {
// Note: Once we get queries for ownership of things other than scopes,
// we can add a 2nd argument to this command to restrict the search to one specific type.
// E.g. Use: "owner {address} [scope|session|record|scopespec|contractspec|recordspec]"
cmd := &cobra.Command{
Use: "owner address",
Aliases: []string{"o", "ownership"},
Short: "Query the current metadata for entries owned by an address",
Long: fmt.Sprintf(`%[1]s owner {address} - gets a list of scope uuids owned by the provided address.`, cmdStart),
Args: cobra.ExactArgs(1),
Example: fmt.Sprintf(`%[1]s owner pb1sh49f6ze3vn7cdl2amh2gnc70z5mten3dpvr42`, cmdStart),
RunE: func(cmd *cobra.Command, args []string) error {
address := strings.TrimSpace(args[0])
if len(address) == 0 {
return fmt.Errorf("empty address")
}
return outputOwnership(cmd, address)
},
}
addIncludeRequestFlag(cmd)
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "scopes")
return cmd
}
// GetValueOwnershipCmd returns the command handler for metadata scope querying by owner address
func GetValueOwnershipCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "valueowner address",
Aliases: []string{"vo", "valueownership"},
Short: "Query the current metadata for scopes with the provided address as the value owner",
Long: fmt.Sprintf(`%[1]s valueowner {address} - gets a list of scope uuids value-owned by the provided address.`, cmdStart),
Args: cobra.ExactArgs(1),
Example: fmt.Sprintf(`%[1]s valueowner pb1sh49f6ze3vn7cdl2amh2gnc70z5mten3dpvr42`, cmdStart),
RunE: func(cmd *cobra.Command, args []string) error {
address := strings.TrimSpace(args[0])
if len(address) == 0 {
return fmt.Errorf("empty address")
}
return outputValueOwnership(cmd, address)
},
}
addIncludeRequestFlag(cmd)
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "scopes")
return cmd
}
// GetOSLocatorCmd returns the command handler for metadata object store locator querying.
func GetOSLocatorCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "locator {owner|scope_id|scope_uuid|uri|\"params\"|\"all\"}",
Aliases: []string{"l", "locators"},
Short: "Query the current metadata for object store locators",
Long: fmt.Sprintf(`%[1]s locator {owner} - gets the object store locator for that owner.
%[1]s locator {scope_id} - gets object store locators for all the owners of that scope.
%[1]s locator {scope_uuid} - gets object store locators for all the owners of that scope.
%[1]s locator {uri} - gets object store locators with that uri.
%[1]s locator params - gets the object store locator params.
%[1]s locator all - gets all object store locators.`, cmdStart),
Args: cobra.ExactArgs(1),
Example: fmt.Sprintf(`%[1]s locator pb1sh49f6ze3vn7cdl2amh2gnc70z5mten3dpvr42
%[1]s locator scope1qzge0zaztu65tx5x5llv5xc9ztsqxlkwel
%[1]s locator 91978ba2-5f35-459a-86a7-feca1b0512e0
%[1]s locator https://provenance.io/
%[1]s locator params
%[1]s locator all`, cmdStart),
RunE: func(cmd *cobra.Command, args []string) error {
arg0 := strings.TrimSpace(args[0])
// First check if it's just the string "params".
if arg0 == "params" {
return outputOSLocatorParams(cmd)
}
// And then look for "all".
if arg0 == all {
return outputOSLocatorsAll(cmd)
}
// Now look to see if it's a metadata address.
_, idErr := types.MetadataAddressFromBech32(arg0)
if idErr == nil {
return outputOSLocatorsByScope(cmd, arg0)
}
// Okay... maybe check for a generic bech32.
_, _, bech32Err := bech32.DecodeAndConvert(arg0)
if bech32Err == nil {
return outputOSLocator(cmd, arg0)
}
// Well maybe a UUID?
_, uuidErr := uuid.Parse(arg0)
if uuidErr == nil {
return outputOSLocatorsByScope(cmd, arg0)
}
// Fine... let's just hope it's a URI.
return outputOSLocatorsByURI(cmd, arg0)
},
}
addIncludeRequestFlag(cmd)
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "locators (all)")
return cmd
}
// GetAccountDataCmd is the CLI command for querying account data for metadata.
func GetAccountDataCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "account-data <address>",
Short: "Get the account data for a metadata address",
Aliases: []string{"accountdata", "ad"},
Example: fmt.Sprintf(`$ %s account-data scope1qzge0zaztu65tx5x5llv5xc9ztsqxlkwel`, cmdStart),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
req := &types.AccountDataRequest{}
req.MetadataAddr, err = types.MetadataAddressFromBech32(args[0])
if err != nil {
return fmt.Errorf("invalid metadata address %q: %w", args[0], err)
}
resp, err := queryClient.AccountData(cmd.Context(), req)
if err != nil {
return fmt.Errorf("failed to query account data for metadata address %q: %w", args[0], err)
}
return clientCtx.PrintProto(resp)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// ------------ private funcs for actually querying and outputting ------------
// outputParams calls the Params query and outputs the response.
func outputParams(cmd *cobra.Command) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.Params(cmd.Context(), &types.QueryParamsRequest{IncludeRequest: includeRequest})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
}
// outputGetByAddr calls the GetByAddr query and outputs the response.
func outputGetByAddr(cmd *cobra.Command, addrs []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.GetByAddr(
cmd.Context(),
&types.GetByAddrRequest{Addrs: addrs},
)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
}
// outputScope calls the Scope query and outputs the response.
func outputScope(cmd *cobra.Command, scopeID string, sessionAddr string, recordAddr string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
req := types.ScopeRequest{
ScopeId: scopeID,
SessionAddr: sessionAddr,
RecordAddr: recordAddr,
IncludeSessions: includeSessions,
IncludeRecords: includeRecords,
ExcludeIdInfo: excludeIDInfo,
IncludeRequest: includeRequest,
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.Scope(cmd.Context(), &req)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
}
// outputScopesAll calls the ScopesAllRequest query and outputs the response.
func outputScopesAll(cmd *cobra.Command) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
pageReq, e := client.ReadPageRequestWithPageKeyDecoded(cmd.Flags())
if e != nil {
return e
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.ScopesAll(
cmd.Context(),
&types.ScopesAllRequest{
ExcludeIdInfo: excludeIDInfo,
IncludeRequest: includeRequest,
Pagination: pageReq,
},
)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
}
// outputSessions calls the Sessions query and outputs the response.
func outputSessions(cmd *cobra.Command, scopeID, sessionID, recordID, recordName string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
req := types.SessionsRequest{
ScopeId: scopeID,
SessionId: sessionID,
RecordAddr: recordID,
RecordName: recordName,
IncludeScope: includeScope,
IncludeRecords: includeRecords,
ExcludeIdInfo: excludeIDInfo,
IncludeRequest: includeRequest,
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.Sessions(cmd.Context(), &req)
if err != nil {
return err
}
if res == nil || res.Sessions == nil || len(res.Sessions) == 0 {
return errors.New("no sessions found")
}
return clientCtx.PrintProto(res)
}
// outputSessionsAll calls the SessionsAll query and outputs the response.
func outputSessionsAll(cmd *cobra.Command) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
pageReq, e := client.ReadPageRequestWithPageKeyDecoded(cmd.Flags())
if e != nil {
return e
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.SessionsAll(
cmd.Context(),
&types.SessionsAllRequest{
ExcludeIdInfo: excludeIDInfo,
IncludeRequest: includeRequest,
Pagination: pageReq,
},
)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
}
// outputRecords calls the Records query and outputs the response.
func outputRecords(cmd *cobra.Command, recordAddr string, scopeID string, sessionID string, name string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
req := types.RecordsRequest{
RecordAddr: recordAddr,
ScopeId: scopeID,
SessionId: sessionID,
Name: name,
IncludeScope: includeScope,
IncludeSessions: includeSessions,
ExcludeIdInfo: excludeIDInfo,
IncludeRequest: includeRequest,
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.Records(cmd.Context(), &req)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
}
// outputRecordsAll calls the RecordsAll query and outputs the response.
func outputRecordsAll(cmd *cobra.Command) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
pageReq, e := client.ReadPageRequestWithPageKeyDecoded(cmd.Flags())
if e != nil {
return e
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.RecordsAll(
cmd.Context(),
&types.RecordsAllRequest{
ExcludeIdInfo: excludeIDInfo,
IncludeRequest: includeRequest,
Pagination: pageReq,
},
)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
}
// outputOwnership calls the Ownership query and outputs the response.
func outputOwnership(cmd *cobra.Command, address string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
pageReq, e := client.ReadPageRequestWithPageKeyDecoded(cmd.Flags())
if e != nil {
return e
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.Ownership(
cmd.Context(),
&types.OwnershipRequest{Address: address, IncludeRequest: includeRequest, Pagination: pageReq},
)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
}
// outputValueOwnership calls the ValueOwnership query and outputs the response.
func outputValueOwnership(cmd *cobra.Command, address string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
pageReq, e := client.ReadPageRequestWithPageKeyDecoded(cmd.Flags())
if e != nil {
return e
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.ValueOwnership(
cmd.Context(),
&types.ValueOwnershipRequest{Address: address, IncludeRequest: includeRequest, Pagination: pageReq},
)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
}
// outputScopeSpec calls the ScopeSpecification query and outputs the response.
func outputScopeSpec(cmd *cobra.Command, specificationID string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
req := types.ScopeSpecificationRequest{
SpecificationId: specificationID,
IncludeContractSpecs: includeContractSpecs,
IncludeRecordSpecs: includeRecordSpecs,
ExcludeIdInfo: excludeIDInfo,
IncludeRequest: includeRequest,
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.ScopeSpecification(cmd.Context(), &req)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
}
// outputScopeSpecsAll calls the ScopeSpecificationsAll query and outputs the response.
func outputScopeSpecsAll(cmd *cobra.Command) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
pageReq, e := client.ReadPageRequestWithPageKeyDecoded(cmd.Flags())
if e != nil {
return e
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.ScopeSpecificationsAll(
cmd.Context(),
&types.ScopeSpecificationsAllRequest{
ExcludeIdInfo: excludeIDInfo,
IncludeRequest: includeRequest,
Pagination: pageReq,
},
)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
}
// outputContractSpec calls the ContractSpecification query and outputs the response.
func outputContractSpec(cmd *cobra.Command, specificationID string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
req := types.ContractSpecificationRequest{
SpecificationId: specificationID,
IncludeRecordSpecs: includeRecordSpecs,
ExcludeIdInfo: excludeIDInfo,
IncludeRequest: includeRequest,
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.ContractSpecification(cmd.Context(), &req)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
}
// outputContractSpecsAll calls the ContractSpecificationsAll query and outputs the response.
func outputContractSpecsAll(cmd *cobra.Command) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
pageReq, e := client.ReadPageRequestWithPageKeyDecoded(cmd.Flags())
if e != nil {
return e
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.ContractSpecificationsAll(
cmd.Context(),
&types.ContractSpecificationsAllRequest{
ExcludeIdInfo: excludeIDInfo,
IncludeRequest: includeRequest,
Pagination: pageReq,
},
)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
}
// outputRecordSpec calls the RecordSpecification query and outputs the response.
func outputRecordSpec(cmd *cobra.Command, specificationID string, name string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
req := types.RecordSpecificationRequest{
SpecificationId: specificationID,
Name: name,
ExcludeIdInfo: excludeIDInfo,
IncludeRequest: includeRequest,
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.RecordSpecification(cmd.Context(), &req)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
}
// outputRecordSpecsForContractSpec calls the RecordSpecificationsForContractSpecification query and outputs the response.
func outputRecordSpecsForContractSpec(cmd *cobra.Command, specificationID string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
req := types.RecordSpecificationsForContractSpecificationRequest{
SpecificationId: specificationID,
ExcludeIdInfo: excludeIDInfo,
IncludeRequest: includeRequest,
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.RecordSpecificationsForContractSpecification(cmd.Context(), &req)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
}
// outputRecordSpecsAll calls the RecordSpecificationsAll query and outputs the response.
func outputRecordSpecsAll(cmd *cobra.Command) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
pageReq, e := client.ReadPageRequestWithPageKeyDecoded(cmd.Flags())
if e != nil {
return e
}
queryClient := types.NewQueryClient(clientCtx)