File: foundry-cli-builtin-private.h

package info (click to toggle)
foundry 1.1~beta-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,552 kB
  • sloc: ansic: 167,487; xml: 417; makefile: 21; sh: 19; javascript: 10
file content (348 lines) | stat: -rw-r--r-- 14,615 bytes parent folder | download | duplicates (2)
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
/* foundry-cli-builtin-private.h
 *
 * Copyright 2024 Christian Hergert <chergert@redhat.com>
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#pragma once

#include "foundry-cli-command.h"
#include "foundry-cli-command-tree.h"
#include "foundry-command-line.h"
#include "foundry-types.h"
#include "foundry-util.h"

G_BEGIN_DECLS

void foundry_cli_builtin_build                     (FoundryCliCommandTree *tree);
#ifdef FOUNDRY_FEATURE_GIT
void foundry_cli_builtin_clone                     (FoundryCliCommandTree *tree);
#endif
void foundry_cli_builtin_config_list               (FoundryCliCommandTree *tree);
void foundry_cli_builtin_config_switch             (FoundryCliCommandTree *tree);
void foundry_cli_builtin_dependencies_list         (FoundryCliCommandTree *tree);
void foundry_cli_builtin_dependencies_update       (FoundryCliCommandTree *tree);
void foundry_cli_builtin_deploy                    (FoundryCliCommandTree *tree);
void foundry_cli_builtin_devenv                    (FoundryCliCommandTree *tree);
void foundry_cli_builtin_device_list               (FoundryCliCommandTree *tree);
void foundry_cli_builtin_device_switch             (FoundryCliCommandTree *tree);
void foundry_cli_builtin_diagnose                  (FoundryCliCommandTree *tree);
#ifdef FOUNDRY_FEATURE_DOCS
void foundry_cli_builtin_doc_bundle_install        (FoundryCliCommandTree *tree);
void foundry_cli_builtin_doc_bundle_list           (FoundryCliCommandTree *tree);
void foundry_cli_builtin_doc_query                 (FoundryCliCommandTree *tree);
#endif
void foundry_cli_builtin_enter                     (FoundryCliCommandTree *tree);
#ifdef FOUNDRY_FEATURE_FORGE
void foundry_cli_builtin_forge_list                (FoundryCliCommandTree *tree);
void foundry_cli_builtin_forge_issues_list         (FoundryCliCommandTree *tree);
void foundry_cli_builtin_forge_merge_requests_list (FoundryCliCommandTree *tree);
void foundry_cli_builtin_forge_project             (FoundryCliCommandTree *tree);
void foundry_cli_builtin_forge_switch              (FoundryCliCommandTree *tree);
void foundry_cli_builtin_forge_user                (FoundryCliCommandTree *tree);
#endif
void foundry_cli_builtin_guess_language            (FoundryCliCommandTree *tree);
void foundry_cli_builtin_init                      (FoundryCliCommandTree *tree);
#ifdef FOUNDRY_FEATURE_LLM
void foundry_cli_builtin_llm_list_models           (FoundryCliCommandTree *tree);
void foundry_cli_builtin_llm_list_resources        (FoundryCliCommandTree *tree);
void foundry_cli_builtin_llm_list_tools            (FoundryCliCommandTree *tree);
void foundry_cli_builtin_llm_complete              (FoundryCliCommandTree *tree);
#endif
#ifdef FOUNDRY_FEATURE_LSP
void foundry_cli_builtin_lsp_list                  (FoundryCliCommandTree *tree);
void foundry_cli_builtin_lsp_prefer                (FoundryCliCommandTree *tree);
void foundry_cli_builtin_lsp_run                   (FoundryCliCommandTree *tree);
#endif
#ifdef FOUNDRY_FEATURE_MCP
void foundry_cli_builtin_mcp                       (FoundryCliCommandTree *tree);
#endif
void foundry_cli_builtin_mdoc                      (FoundryCliCommandTree *tree);
void foundry_cli_builtin_pipeline_flags            (FoundryCliCommandTree *tree);
void foundry_cli_builtin_pipeline_info             (FoundryCliCommandTree *tree);
void foundry_cli_builtin_pipeline_invalidate       (FoundryCliCommandTree *tree);
void foundry_cli_builtin_pipeline_link             (FoundryCliCommandTree *tree);
void foundry_cli_builtin_pipeline_unlink           (FoundryCliCommandTree *tree);
void foundry_cli_builtin_pipeline_targets          (FoundryCliCommandTree *tree);
void foundry_cli_builtin_pipeline_which            (FoundryCliCommandTree *tree);
void foundry_cli_builtin_run                       (FoundryCliCommandTree *tree);
void foundry_cli_builtin_sdk_install               (FoundryCliCommandTree *tree);
void foundry_cli_builtin_sdk_list                  (FoundryCliCommandTree *tree);
void foundry_cli_builtin_sdk_shell                 (FoundryCliCommandTree *tree);
void foundry_cli_builtin_sdk_switch                (FoundryCliCommandTree *tree);
void foundry_cli_builtin_sdk_which                 (FoundryCliCommandTree *tree);
void foundry_cli_builtin_search                    (FoundryCliCommandTree *tree);
#ifdef FOUNDRY_FEATURE_TEXT
void foundry_cli_builtin_find_symbol_at            (FoundryCliCommandTree *tree);
void foundry_cli_builtin_grep                      (FoundryCliCommandTree *tree);
void foundry_cli_builtin_symbol_tree               (FoundryCliCommandTree *tree);
#endif
#ifdef FOUNDRY_FEATURE_TEXT
# ifdef HAVE_PLUGIN_CTAGS
void foundry_cli_builtin_ctags                    (FoundryCliCommandTree *tree);
# endif
#endif
void foundry_cli_builtin_secret_check_expires_at   (FoundryCliCommandTree *tree);
void foundry_cli_builtin_secret_get_api_key        (FoundryCliCommandTree *tree);
void foundry_cli_builtin_secret_rotate             (FoundryCliCommandTree *tree);
void foundry_cli_builtin_secret_set_api_key        (FoundryCliCommandTree *tree);
void foundry_cli_builtin_settings_get              (FoundryCliCommandTree *tree);
void foundry_cli_builtin_settings_set              (FoundryCliCommandTree *tree);
void foundry_cli_builtin_shell                     (FoundryCliCommandTree *tree);
void foundry_cli_builtin_show                      (FoundryCliCommandTree *tree);
#ifdef FOUNDRY_FEATURE_TEMPLATES
void foundry_cli_builtin_template_create           (FoundryCliCommandTree *tree);
void foundry_cli_builtin_template_list             (FoundryCliCommandTree *tree);
#endif
void foundry_cli_builtin_test_list                 (FoundryCliCommandTree *tree);
void foundry_cli_builtin_test_run                  (FoundryCliCommandTree *tree);
#ifdef FOUNDRY_FEATURE_VCS
void foundry_cli_builtin_vcs_blame                 (FoundryCliCommandTree *tree);
void foundry_cli_builtin_vcs_fetch                 (FoundryCliCommandTree *tree);
void foundry_cli_builtin_vcs_ignored               (FoundryCliCommandTree *tree);
void foundry_cli_builtin_vcs_list                  (FoundryCliCommandTree *tree);
void foundry_cli_builtin_vcs_list_branches         (FoundryCliCommandTree *tree);
void foundry_cli_builtin_vcs_list_files            (FoundryCliCommandTree *tree);
void foundry_cli_builtin_vcs_list_remotes          (FoundryCliCommandTree *tree);
void foundry_cli_builtin_vcs_list_tags             (FoundryCliCommandTree *tree);
void foundry_cli_builtin_vcs_log                   (FoundryCliCommandTree *tree);
void foundry_cli_builtin_vcs_switch                (FoundryCliCommandTree *tree);
#endif

static inline void
_foundry_cli_builtin_register (FoundryCliCommandTree *tree)
{
    foundry_cli_command_tree_register (tree,
                                       FOUNDRY_STRV_INIT ("foundry"),
                                       &(FoundryCliCommand) {
                                         .options = (GOptionEntry[]) {
                                           { "shared", 0, 0, G_OPTION_ARG_NONE },
                                           {0}
                                         },
                                       });

  foundry_cli_builtin_build (tree);
#ifdef FOUNDRY_FEATURE_GIT
  foundry_cli_builtin_clone (tree);
#endif
  foundry_cli_builtin_config_list (tree);
  foundry_cli_builtin_config_switch (tree);
  foundry_cli_builtin_dependencies_list (tree);
  foundry_cli_builtin_dependencies_update (tree);
  foundry_cli_builtin_deploy (tree);
  foundry_cli_builtin_devenv (tree);
  foundry_cli_builtin_device_list (tree);
  foundry_cli_builtin_device_switch (tree);
  foundry_cli_builtin_diagnose (tree);
#ifdef FOUNDRY_FEATURE_DOCS
  foundry_cli_builtin_doc_bundle_install (tree);
  foundry_cli_builtin_doc_bundle_list (tree);
  foundry_cli_builtin_doc_query (tree);
#endif
  foundry_cli_builtin_enter (tree);
#ifdef FOUNDRY_FEATURE_FORGE
  foundry_cli_builtin_forge_list (tree);
  foundry_cli_builtin_forge_issues_list (tree);
  foundry_cli_builtin_forge_merge_requests_list (tree);
  foundry_cli_builtin_forge_project (tree);
  foundry_cli_builtin_forge_switch (tree);
  foundry_cli_builtin_forge_user (tree);
#endif
#ifdef FOUNDRY_FEATURE_TEXT
  foundry_cli_builtin_find_symbol_at (tree);
  foundry_cli_builtin_grep (tree);
  foundry_cli_builtin_symbol_tree (tree);
#endif
  foundry_cli_builtin_guess_language (tree);
  foundry_cli_builtin_init (tree);
#ifdef FOUNDRY_FEATURE_LLM
  foundry_cli_builtin_llm_list_models (tree);
  foundry_cli_builtin_llm_list_resources (tree);
  foundry_cli_builtin_llm_list_tools (tree);
  foundry_cli_builtin_llm_complete (tree);
#endif
#ifdef FOUNDRY_FEATURE_LSP
  foundry_cli_builtin_lsp_list (tree);
  foundry_cli_builtin_lsp_prefer (tree);
  foundry_cli_builtin_lsp_run (tree);
#endif
#ifdef FOUNDRY_FEATURE_MCP
  foundry_cli_builtin_mcp (tree);
#endif
  foundry_cli_builtin_mdoc (tree);
  foundry_cli_builtin_pipeline_flags (tree);
  foundry_cli_builtin_pipeline_info (tree);
  foundry_cli_builtin_pipeline_invalidate (tree);
  foundry_cli_builtin_pipeline_link (tree);
  foundry_cli_builtin_pipeline_unlink (tree);
  foundry_cli_builtin_pipeline_targets (tree);
  foundry_cli_builtin_pipeline_which (tree);
  foundry_cli_builtin_run (tree);
  foundry_cli_builtin_sdk_install (tree);
  foundry_cli_builtin_sdk_list (tree);
  foundry_cli_builtin_sdk_shell (tree);
  foundry_cli_builtin_sdk_switch (tree);
  foundry_cli_builtin_sdk_which (tree);
  foundry_cli_builtin_search (tree);
#ifdef FOUNDRY_FEATURE_TEXT
# ifdef HAVE_PLUGIN_CTAGS
  foundry_cli_builtin_ctags (tree);
# endif
#endif
  foundry_cli_builtin_secret_check_expires_at (tree);
  foundry_cli_builtin_secret_get_api_key (tree);
  foundry_cli_builtin_secret_rotate (tree);
  foundry_cli_builtin_secret_set_api_key (tree);
  foundry_cli_builtin_settings_get (tree);
  foundry_cli_builtin_settings_set (tree);
  foundry_cli_builtin_shell (tree);
  foundry_cli_builtin_show (tree);
#ifdef FOUNDRY_FEATURE_TEMPLATES
  foundry_cli_builtin_template_create (tree);
  foundry_cli_builtin_template_list (tree);
#endif
  foundry_cli_builtin_test_list (tree);
  foundry_cli_builtin_test_run (tree);
#ifdef FOUNDRY_FEATURE_VCS
  foundry_cli_builtin_vcs_blame (tree);
  foundry_cli_builtin_vcs_fetch (tree);
  foundry_cli_builtin_vcs_ignored (tree);
  foundry_cli_builtin_vcs_list (tree);
  foundry_cli_builtin_vcs_list_branches (tree);
  foundry_cli_builtin_vcs_list_files (tree);
  foundry_cli_builtin_vcs_list_remotes (tree);
  foundry_cli_builtin_vcs_list_tags (tree);
  foundry_cli_builtin_vcs_log (tree);
  foundry_cli_builtin_vcs_switch (tree);
#endif
}

static inline gboolean
_foundry_cli_builtin_should_complete_id (const char * const *argv,
                                         const char         *current)
{
  if (g_strv_length ((char **)argv) > 2 ||
      (g_strv_length ((char **)argv) == 2 && foundry_str_empty0 (current)))
    return FALSE;

  return TRUE;
}

static inline char **
foundry_cli_builtin_complete_model (GListModel         *model,
                                    const char * const *argv,
                                    const char         *current,
                                    const char         *keyword_property)
{
  g_autoptr(FoundryContext) context = NULL;
  g_autoptr(GStrvBuilder) builder = NULL;
  g_autoptr(GError) error = NULL;
  guint n_items;

  g_assert (!model || G_IS_LIST_MODEL (model));
  g_assert (keyword_property != NULL);

  if (!_foundry_cli_builtin_should_complete_id (argv, current))
    return NULL;

  if (model == NULL)
    return NULL;

  builder = g_strv_builder_new ();
  n_items = g_list_model_get_n_items (model);

  for (guint i = 0; i < n_items; i++)
    {
      g_autoptr(GObject) object = g_list_model_get_item (model, i);
      g_autofree char *id = NULL;
      g_autofree char *spaced = NULL;

      g_object_get (object,
                    keyword_property, &id,
                    NULL);

      if (id == NULL)
        continue;

      spaced = g_strdup_printf ("%s ", id);

      if (current == NULL ||
          g_str_has_prefix (spaced, current))
        g_strv_builder_add (builder, spaced);
    }

  return g_strv_builder_end (builder);
}

static inline char **
foundry_cli_builtin_complete_list_model (FoundryCliOptions  *options,
                                         FoundryCommandLine *command_line,
                                         const char * const *argv,
                                         const char         *current,
                                         const char         *service_property,
                                         const char         *keyword_property)
{
  g_autoptr(FoundryContext) context = NULL;
  g_autoptr(GStrvBuilder) builder = NULL;
  g_autoptr(GError) error = NULL;

  g_assert (service_property != NULL);
  g_assert (keyword_property != NULL);

  if (!_foundry_cli_builtin_should_complete_id (argv, current))
    return NULL;

  builder = g_strv_builder_new ();

  if ((context = dex_await_object (foundry_cli_options_load_context (options, command_line), &error)))
    {
      g_autoptr(GListModel) model = NULL;
      guint n_items;

      g_object_get (context,
                    service_property, &model,
                    NULL);

      g_assert (G_IS_LIST_MODEL (model));

      n_items = g_list_model_get_n_items (model);

      for (guint i = 0; i < n_items; i++)
        {
          g_autoptr(GObject) object = g_list_model_get_item (model, i);
          g_autofree char *id = NULL;
          g_autofree char *spaced = NULL;

          g_object_get (object,
                        keyword_property, &id,
                        NULL);

          if (id == NULL)
            continue;

          spaced = g_strdup_printf ("%s ", id);

          if (current == NULL ||
              g_str_has_prefix (spaced, current))
            g_strv_builder_add (builder, spaced);
        }
    }

  return g_strv_builder_end (builder);
}

G_END_DECLS