@@ -49,50 +49,50 @@ static void setopt_nocache(command_t *self) { opt_cache = 0; }
49
49
50
50
static void setopt_json (command_t * self ) { opt_json = 1 ; }
51
51
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
+ } \
65
65
}
66
66
67
67
static 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 ;
96
96
}
97
97
98
98
/*
@@ -126,67 +126,67 @@ static char *wiki_html_cache() {
126
126
static void display_package (const registry_package_ptr_t pkg ,
127
127
cc_color_t fg_color_highlight ,
128
128
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" );
135
135
}
136
136
137
137
static void add_package_to_json (const registry_package_ptr_t pkg ,
138
138
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 );
141
141
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 ));
146
146
147
- json_array_append_value (json_list , json_pkg_root );
147
+ json_array_append_value (json_list , json_pkg_root );
148
148
}
149
149
150
150
int main (int argc , char * argv []) {
151
- opt_color = 1 ;
152
- opt_cache = 1 ;
151
+ opt_color = 1 ;
152
+ opt_cache = 1 ;
153
153
154
- debug_init (& debugger , "clib-search" );
154
+ debug_init (& debugger , "clib-search" );
155
155
156
- clib_cache_init (CLIB_SEARCH_CACHE_TIME );
156
+ clib_cache_init (CLIB_SEARCH_CACHE_TIME );
157
157
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 ...]" ;
161
161
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 );
164
164
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 );
167
167
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 );
170
170
171
- command_parse (& program , argc , argv );
171
+ command_parse (& program , argc , argv );
172
172
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 ]);
175
175
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 ;
179
179
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" );
183
183
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 );
187
187
188
- // TODO, implement caching for the new registries.
189
- /*
188
+ // TODO, implement caching for the new registries.
189
+ /*
190
190
char *html = wiki_html_cache();
191
191
if (NULL == html) {
192
192
command_free(&program);
@@ -198,45 +198,45 @@ int main(int argc, char *argv[]) {
198
198
debug(&debugger, "found %zu packages", pkgs->len);
199
199
*/
200
200
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 );
207
207
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 ;
210
210
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
+ }
215
215
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" );
229
217
218
+ while ((pkg = registry_package_iterator_next (it ))) {
219
+ if (matches (program .argc , program .argv , pkg )) {
230
220
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 );
236
224
}
225
+ } else {
226
+ debug (& debugger , "skipped package %s" , registry_package_get_id (pkg ));
227
+ }
228
+ }
237
229
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 );
239
236
}
240
- command_free (& program );
241
- return 0 ;
237
+
238
+ registry_package_iterator_destroy (it );
239
+ }
240
+ command_free (& program );
241
+ return 0 ;
242
242
}
0 commit comments