@@ -49,50 +49,50 @@ static void setopt_nocache(command_t *self) { opt_cache = 0; }
4949
5050static void setopt_json (command_t * self ) { opt_json = 1 ; }
5151
52- #define COMPARE (v ) \
53- { \
54- if (NULL == v) { \
55- rc = 0; \
56- goto cleanup; \
57- } \
58- case_lower(v); \
59- for (int i = 0; i < count; i++) { \
60- if (strstr(v, args[i])) { \
61- rc = 1; \
62- break; \
63- } \
64- } \
52+ #define COMPARE (v ) \
53+ { \
54+ if (NULL == v) { \
55+ rc = 0; \
56+ goto cleanup; \
57+ } \
58+ case_lower(v); \
59+ for (int i = 0; i < count; i++) { \
60+ if (strstr(v, args[i])) { \
61+ rc = 1; \
62+ break; \
63+ } \
64+ } \
6565 }
6666
6767static int matches (int count , char * args [], registry_package_ptr_t pkg ) {
68- // Display all packages if there's no query
69- if (0 == count )
70- return 1 ;
71-
72- char * description = NULL ;
73- char * name = NULL ;
74- char * repo = NULL ;
75- char * href = NULL ;
76- int rc = 0 ;
77-
78- name = clib_package_parse_name (registry_package_get_id (pkg ));
79- COMPARE (name );
80-
81- description = strdup (registry_package_get_description (pkg ));
82- COMPARE (description );
83-
84- repo = strdup (registry_package_get_id (pkg ));
85- COMPARE (repo );
86-
87- href = strdup (registry_package_get_href (pkg ));
88- COMPARE (href );
89-
90- cleanup :
91- free (description );
92- free (name );
93- free (repo );
94- free (href );
95- return rc ;
68+ // Display all packages if there's no query
69+ if (0 == count )
70+ return 1 ;
71+
72+ char * description = NULL ;
73+ char * name = NULL ;
74+ char * repo = NULL ;
75+ char * href = NULL ;
76+ int rc = 0 ;
77+
78+ name = clib_package_parse_name (registry_package_get_id (pkg ));
79+ COMPARE (name );
80+
81+ description = strdup (registry_package_get_description (pkg ));
82+ COMPARE (description );
83+
84+ repo = strdup (registry_package_get_id (pkg ));
85+ COMPARE (repo );
86+
87+ href = strdup (registry_package_get_href (pkg ));
88+ COMPARE (href );
89+
90+ cleanup :
91+ free (description );
92+ free (name );
93+ free (repo );
94+ free (href );
95+ return rc ;
9696}
9797
9898/*
@@ -126,67 +126,67 @@ static char *wiki_html_cache() {
126126static void display_package (const registry_package_ptr_t pkg ,
127127 cc_color_t fg_color_highlight ,
128128 cc_color_t fg_color_text ) {
129- cc_fprintf (fg_color_highlight , stdout , " %s\n" , registry_package_get_id (pkg ));
130- printf (" url: " );
131- cc_fprintf (fg_color_text , stdout , "%s\n" , registry_package_get_href (pkg ));
132- printf (" desc: " );
133- cc_fprintf (fg_color_text , stdout , "%s\n" , registry_package_get_description (pkg ));
134- printf ("\n" );
129+ cc_fprintf (fg_color_highlight , stdout , " %s\n" , registry_package_get_id (pkg ));
130+ printf (" url: " );
131+ cc_fprintf (fg_color_text , stdout , "%s\n" , registry_package_get_href (pkg ));
132+ printf (" desc: " );
133+ cc_fprintf (fg_color_text , stdout , "%s\n" , registry_package_get_description (pkg ));
134+ printf ("\n" );
135135}
136136
137137static void add_package_to_json (const registry_package_ptr_t pkg ,
138138 JSON_Array * json_list ) {
139- JSON_Value * json_pkg_root = json_value_init_object ();
140- JSON_Object * json_pkg = json_value_get_object (json_pkg_root );
139+ JSON_Value * json_pkg_root = json_value_init_object ();
140+ JSON_Object * json_pkg = json_value_get_object (json_pkg_root );
141141
142- json_object_set_string (json_pkg , "repo" , registry_package_get_id (pkg ));
143- json_object_set_string (json_pkg , "href" , registry_package_get_href (pkg ));
144- json_object_set_string (json_pkg , "description" , registry_package_get_description (pkg ));
145- json_object_set_string (json_pkg , "category" , registry_package_get_category (pkg ));
142+ json_object_set_string (json_pkg , "repo" , registry_package_get_id (pkg ));
143+ json_object_set_string (json_pkg , "href" , registry_package_get_href (pkg ));
144+ json_object_set_string (json_pkg , "description" , registry_package_get_description (pkg ));
145+ json_object_set_string (json_pkg , "category" , registry_package_get_category (pkg ));
146146
147- json_array_append_value (json_list , json_pkg_root );
147+ json_array_append_value (json_list , json_pkg_root );
148148}
149149
150150int main (int argc , char * argv []) {
151- opt_color = 1 ;
152- opt_cache = 1 ;
151+ opt_color = 1 ;
152+ opt_cache = 1 ;
153153
154- debug_init (& debugger , "clib-search" );
154+ debug_init (& debugger , "clib-search" );
155155
156- clib_cache_init (CLIB_SEARCH_CACHE_TIME );
156+ clib_cache_init (CLIB_SEARCH_CACHE_TIME );
157157
158- command_t program ;
159- command_init (& program , "clib-search" , CLIB_VERSION );
160- program .usage = "[options] [query ...]" ;
158+ command_t program ;
159+ command_init (& program , "clib-search" , CLIB_VERSION );
160+ program .usage = "[options] [query ...]" ;
161161
162- command_option (& program , "-n" , "--no-color" , "don't colorize output" ,
163- setopt_nocolor );
162+ command_option (& program , "-n" , "--no-color" , "don't colorize output" ,
163+ setopt_nocolor );
164164
165- command_option (& program , "-c" , "--skip-cache" , "skip the search cache" ,
166- setopt_nocache );
165+ command_option (& program , "-c" , "--skip-cache" , "skip the search cache" ,
166+ setopt_nocache );
167167
168- command_option (& program , "-j" , "--json" , "generate a serialized JSON output" ,
169- setopt_json );
168+ command_option (& program , "-j" , "--json" , "generate a serialized JSON output" ,
169+ setopt_json );
170170
171- command_parse (& program , argc , argv );
171+ command_parse (& program , argc , argv );
172172
173- for (int i = 0 ; i < program .argc ; i ++ )
174- case_lower (program .argv [i ]);
173+ for (int i = 0 ; i < program .argc ; i ++ )
174+ case_lower (program .argv [i ]);
175175
176- // set color theme
177- cc_color_t fg_color_highlight = opt_color ? CC_FG_DARK_CYAN : CC_FG_NONE ;
178- cc_color_t fg_color_text = opt_color ? CC_FG_DARK_GRAY : CC_FG_NONE ;
176+ // set color theme
177+ cc_color_t fg_color_highlight = opt_color ? CC_FG_DARK_CYAN : CC_FG_NONE ;
178+ cc_color_t fg_color_text = opt_color ? CC_FG_DARK_GRAY : CC_FG_NONE ;
179179
180- // We search the local manifest for extra registries.
181- // It is important to give the extra registries preference over the default registry.
182- clib_secrets_t secrets = clib_secrets_load_from_file ("clib_secrets.json" );
180+ // We search the local manifest for extra registries.
181+ // It is important to give the extra registries preference over the default registry.
182+ clib_secrets_t secrets = clib_secrets_load_from_file ("clib_secrets.json" );
183183
184- clib_package_t * package = clib_package_load_local_manifest (0 );
185- registries_t registries = registry_manager_init_registries (package -> registries , secrets );
186- registry_manager_fetch_registries (registries );
184+ clib_package_t * package = clib_package_load_local_manifest (0 );
185+ registries_t registries = registry_manager_init_registries (package -> registries , secrets );
186+ registry_manager_fetch_registries (registries );
187187
188- // TODO, implement caching for the new registries.
189- /*
188+ // TODO, implement caching for the new registries.
189+ /*
190190 char *html = wiki_html_cache();
191191 if (NULL == html) {
192192 command_free(&program);
@@ -198,45 +198,45 @@ int main(int argc, char *argv[]) {
198198 debug(&debugger, "found %zu packages", pkgs->len);
199199 */
200200
201- registry_iterator_t it = registry_iterator_new (registries );
202- registry_ptr_t registry = NULL ;
203- while ((registry = registry_iterator_next (it ))) {
204- printf ("SEARCH: packages from %s\n" , registry_get_url (registry ));
205- registry_package_ptr_t pkg ;
206- registry_package_iterator_t it = registry_package_iterator_new (registry );
201+ registry_iterator_t it = registry_iterator_new (registries );
202+ registry_ptr_t registry = NULL ;
203+ while ((registry = registry_iterator_next (it ))) {
204+ printf ("SEARCH: packages from %s\n" , registry_get_url (registry ));
205+ registry_package_ptr_t pkg ;
206+ registry_package_iterator_t it = registry_package_iterator_new (registry );
207207
208- JSON_Array * json_list = NULL ;
209- JSON_Value * json_list_root = NULL ;
208+ JSON_Array * json_list = NULL ;
209+ JSON_Value * json_list_root = NULL ;
210210
211- if (opt_json ) {
212- json_list_root = json_value_init_array ();
213- json_list = json_value_get_array (json_list_root );
214- }
211+ if (opt_json ) {
212+ json_list_root = json_value_init_array ();
213+ json_list = json_value_get_array (json_list_root );
214+ }
215215
216- printf ("\n" );
217-
218- while ((pkg = registry_package_iterator_next (it ))) {
219- if (matches (program .argc , program .argv , pkg )) {
220- if (opt_json ) {
221- add_package_to_json (pkg , json_list );
222- } else {
223- display_package (pkg , fg_color_highlight , fg_color_text );
224- }
225- } else {
226- debug (& debugger , "skipped package %s" , registry_package_get_id (pkg ));
227- }
228- }
216+ printf ("\n" );
229217
218+ while ((pkg = registry_package_iterator_next (it ))) {
219+ if (matches (program .argc , program .argv , pkg )) {
230220 if (opt_json ) {
231- char * serialized = json_serialize_to_string_pretty (json_list_root );
232- puts (serialized );
233-
234- json_free_serialized_string (serialized );
235- json_value_free (json_list_root );
221+ add_package_to_json (pkg , json_list );
222+ } else {
223+ display_package (pkg , fg_color_highlight , fg_color_text );
236224 }
225+ } else {
226+ debug (& debugger , "skipped package %s" , registry_package_get_id (pkg ));
227+ }
228+ }
237229
238- registry_package_iterator_destroy (it );
230+ if (opt_json ) {
231+ char * serialized = json_serialize_to_string_pretty (json_list_root );
232+ puts (serialized );
233+
234+ json_free_serialized_string (serialized );
235+ json_value_free (json_list_root );
239236 }
240- command_free (& program );
241- return 0 ;
237+
238+ registry_package_iterator_destroy (it );
239+ }
240+ command_free (& program );
241+ return 0 ;
242242}
0 commit comments