99
1010 "github.com/MakeNowJust/heredoc"
1111 "github.com/odpf/salt/printer"
12+ "github.com/odpf/salt/term"
1213 stencilv1beta1 "github.com/odpf/stencil/proto/odpf/stencil/v1beta1"
1314 "github.com/spf13/cobra"
1415)
@@ -26,83 +27,87 @@ func SearchCmd(cdk *CDK) *cobra.Command {
2627 Long : "Search your queries on schemas" ,
2728 Args : cobra .ExactArgs (1 ),
2829 Example : heredoc .Doc (`
29- $ stencil search <query> --namespace=<namespace> --schema=<schema> --version=<version> --history=<history>
30+ $ stencil search email
31+ $ stencil search email -s human
32+ $ stencil search name -n odpf -s person -v 2
33+ $ stencil search address -n odpf -s person -h true
3034 ` ),
3135 Annotations : map [string ]string {
3236 "group" : "core" ,
3337 "client" : "true" ,
3438 },
35- RunE : func (cmd * cobra.Command , args []string ) error {
36- s := printer .Spin ("" )
37- defer s .Stop ()
38-
39- client , cancel , err := createClient (cmd , cdk )
40- if err != nil {
41- return err
42- }
43- defer cancel ()
44-
45- query := args [0 ]
46- req .Query = query
39+ }
4740
48- if len (schemaID ) > 0 && len (namespaceID ) == 0 {
49- s .Stop ()
50- fmt .Println ("Namespace ID not specified for" , schemaID )
51- return nil
52- }
53- req .NamespaceId = namespaceID
54- req .SchemaId = schemaID
55-
56- if versionID != 0 {
57- req .Version = & stencilv1beta1.SearchRequest_VersionId {
58- VersionId : versionID ,
59- }
60- } else if history {
61- req .Version = & stencilv1beta1.SearchRequest_History {
62- History : history ,
63- }
64- }
41+ cmd .RunE = func (cmd * cobra.Command , args []string ) error {
42+ s := printer .Spin ("" )
43+ defer s .Stop ()
6544
66- res , err := client .Search (context .Background (), & req )
67- if err != nil {
68- return err
69- }
45+ client , cancel , err := createClient (cmd , cdk )
46+ if err != nil {
47+ return err
48+ }
49+ defer cancel ()
7050
71- hits := res .GetHits ()
51+ query := args [0 ]
52+ req .Query = query
7253
73- report := [][]string {}
74- total := 0
54+ if len (schemaID ) > 0 && len (namespaceID ) == 0 {
7555 s .Stop ()
56+ fmt .Println ("Namespace ID not specified for" , schemaID )
57+ return nil
58+ }
59+ req .NamespaceId = namespaceID
60+ req .SchemaId = schemaID
7661
77- if len ( hits ) = = 0 {
78- fmt . Printf ( "No results found" )
79- return nil
62+ if versionID ! = 0 {
63+ req . Version = & stencilv1beta1. SearchRequest_VersionId {
64+ VersionId : versionID ,
8065 }
81-
82- fmt .Printf (" \n Found results across %d schema(s)/version(s) \n \n " , len (hits ))
83-
84- report = append (report , []string {"TYPES" , "NAMESPACE" , "SCHEMA" , "VERSION" , "FIELDS" })
85- for _ , h := range hits {
86- m := groupByType (h .GetFields ())
87- for t , f := range m {
88- report = append (report , []string {
89- t ,
90- h .GetNamespaceId (),
91- h .GetSchemaId (),
92- strconv .Itoa (int (h .GetVersionId ())),
93- strings .Join (f , ", " ),
94- })
95-
96- total ++
97- }
98- report = append (report , []string {"" , "" , "" , "" , "" })
66+ } else if history {
67+ req .Version = & stencilv1beta1.SearchRequest_History {
68+ History : history ,
9969 }
100- printer . Table ( os . Stdout , report )
70+ }
10171
102- fmt .Println ("TOTAL: " , total )
72+ res , err := client .Search (context .Background (), & req )
73+ if err != nil {
74+ return err
75+ }
76+
77+ hits := res .GetHits ()
78+
79+ report := [][]string {}
80+ s .Stop ()
10381
82+ if len (hits ) == 0 {
83+ fmt .Println ("No results found" )
10484 return nil
105- },
85+ }
86+
87+ var total = 0
88+ report = append (report , []string {
89+ term .Bold ("FIELD" ),
90+ term .Bold ("TYPE" ),
91+ term .Bold ("SCHEMA" ),
92+ term .Bold ("VERSION" ),
93+ term .Bold ("NAMESPACE" ),
94+ })
95+ for _ , h := range hits {
96+ fields := h .GetFields ()
97+ for _ , field := range fields {
98+ report = append (report , []string {
99+ field [strings .LastIndex (field , "." )+ 1 :],
100+ field [:strings .LastIndex (field , "." )],
101+ h .GetSchemaId (),
102+ strconv .Itoa (int (h .GetVersionId ())),
103+ h .GetNamespaceId (),
104+ })
105+ total ++
106+ }
107+ }
108+ fmt .Printf (" \n Found %d results across %d schema(s)/version(s) \n \n " , total , len (hits ))
109+ printer .Table (os .Stdout , report )
110+ return nil
106111 }
107112
108113 cmd .Flags ().StringVarP (& namespaceID , "namespace" , "n" , "" , "parent namespace ID" )
@@ -112,17 +117,3 @@ func SearchCmd(cdk *CDK) *cobra.Command {
112117
113118 return cmd
114119}
115-
116- func groupByType (fields []string ) map [string ][]string {
117- m := make (map [string ][]string )
118- for _ , field := range fields {
119- f := field [strings .LastIndex (field , "." )+ 1 :]
120- t := field [:strings .LastIndex (field , "." )]
121- if m [t ] != nil {
122- m [t ] = append (m [t ], f )
123- } else {
124- m [t ] = []string {f }
125- }
126- }
127- return m
128- }
0 commit comments