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
|
/* foundry-command-line.c
*
* 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
*/
#include "config.h"
#include <glib/gstdio.h>
#include <glib/gi18n-lib.h>
#include <json-glib/json-glib.h>
#include "foundry-cli-command-private.h"
#include "foundry-cli-command-tree.h"
#include "foundry-command-line-private.h"
#include "foundry-command-line-input-private.h"
#include "foundry-command-line-local-private.h"
#include "foundry-command-line-remote-private.h"
#include "foundry-ipc.h"
#include "foundry-init-private.h"
#include "foundry-tty-auth-provider.h"
#include "foundry-util-private.h"
/**
* FoundryCommandLine:
*
* Abstract base class for command line interface implementations.
*
* FoundryCommandLine provides the core interface for command line operations including
* command parsing, execution, and communication with remote services. Concrete
* implementations handle different command line modes and provide unified access
* to Foundry functionality from the terminal.
*/
G_DEFINE_ABSTRACT_TYPE (FoundryCommandLine, foundry_command_line, G_TYPE_OBJECT)
G_DEFINE_QUARK (foundry_command_line_error, foundry_command_line_error)
G_DEFINE_ENUM_TYPE (FoundryObjectSerializerFormat, foundry_object_serializer_format,
G_DEFINE_ENUM_VALUE (FOUNDRY_OBJECT_SERIALIZER_FORMAT_TEXT, "text"),
G_DEFINE_ENUM_VALUE (FOUNDRY_OBJECT_SERIALIZER_FORMAT_JSON, "json"))
static DexCancellable *
foundry_command_line_dup_cancellable (FoundryCommandLine *self)
{
g_assert (FOUNDRY_IS_COMMAND_LINE (self));
if (FOUNDRY_COMMAND_LINE_GET_CLASS (self)->dup_cancellable)
return FOUNDRY_COMMAND_LINE_GET_CLASS (self)->dup_cancellable (self);
return dex_cancellable_new ();
}
static DexFuture *
foundry_command_line_real_run (FoundryCommandLine *self,
const char * const *argv)
{
g_autoptr(FoundryCliOptions) options = NULL;
g_autoptr(DexCancellable) cancellable = NULL;
g_autoptr(GOptionContext) context = NULL;
g_autoptr(GError) error = NULL;
g_autofree GType *types = NULL;
g_autofree char *foundry_dir = NULL;
g_autofree char *command_name = NULL;
const FoundryCliCommand *command;
FoundryCliCommandTree *tree;
g_auto(GStrv) args = NULL;
int argc;
const GOptionEntry global_entries[] = {
{ "foundry-dir", 0, 0, G_OPTION_ARG_FILENAME, &foundry_dir, N_("Set the path to the .foundry dir"), N_("DIR") },
{ 0 }
};
g_assert (FOUNDRY_IS_COMMAND_LINE (self));
g_assert (argv != NULL);
if (argv[0] == NULL || argv[1] == NULL)
{
foundry_command_line_help (self);
return dex_future_new_for_int (EXIT_FAILURE);
}
cancellable = foundry_command_line_dup_cancellable (self);
args = g_strdupv ((char **)argv);
argc = g_strv_length (args);
context = g_option_context_new (NULL);
g_option_context_add_main_entries (context, global_entries, GETTEXT_PACKAGE);
g_option_context_set_ignore_unknown_options (context, TRUE);
g_option_context_set_help_enabled (context, FALSE);
g_option_context_set_strict_posix (context, TRUE);
if (!g_option_context_parse (context, &argc, &args, &error))
{
foundry_command_line_printerr (self, "%s: %s\n", _("error"), error->message);
return dex_future_new_for_int (EXIT_FAILURE);
}
if (args[0] == NULL || args[1] == NULL)
{
foundry_command_line_help (self);
return dex_future_new_for_int (EXIT_FAILURE);
}
command_name = g_strdup (args[1]);
if (g_str_equal (command_name, "help") || g_str_equal (command_name, "--help"))
{
foundry_command_line_help (self);
return dex_future_new_for_int (EXIT_SUCCESS);
}
/* Be sure our plugins have loaded */
_foundry_init_plugins ();
tree = foundry_cli_command_tree_get_default ();
if (!(command = foundry_cli_command_tree_lookup (tree, &args, &options, &error)))
{
foundry_command_line_printerr (self, "%s: %s\n", _("error"), error->message);
return dex_future_new_for_int (EXIT_FAILURE);
}
if (foundry_dir != NULL)
foundry_cli_options_set_string (options, "foundry-dir", foundry_dir);
return foundry_cli_command_run (command,
self,
(const char * const *)args,
options,
cancellable);
}
static void
foundry_command_line_class_init (FoundryCommandLineClass *klass)
{
klass->run = foundry_command_line_real_run;
g_dbus_error_register_error (FOUNDRY_COMMAND_LINE_ERROR,
FOUNDRY_COMMAND_LINE_ERROR_RUN_LOCAL,
"org.gnome.foundry.CommandLine.Error.RunLocal");
}
static void
foundry_command_line_init (FoundryCommandLine *self)
{
}
FoundryCommandLine *
foundry_command_line_new (void)
{
return foundry_command_line_local_new ();
}
void
foundry_command_line_print (FoundryCommandLine *self,
const char *format,
...)
{
g_autofree char *message = NULL;
va_list args;
g_return_if_fail (FOUNDRY_IS_COMMAND_LINE (self));
g_return_if_fail (format != NULL);
va_start (args, format);
message = g_strdup_vprintf (format, args);
va_end (args);
FOUNDRY_COMMAND_LINE_GET_CLASS (self)->print (self, message);
}
void
foundry_command_line_printerr (FoundryCommandLine *self,
const char *format,
...)
{
g_autofree char *message = NULL;
va_list args;
g_return_if_fail (FOUNDRY_IS_COMMAND_LINE (self));
g_return_if_fail (format != NULL);
va_start (args, format);
message = g_strdup_vprintf (format, args);
va_end (args);
FOUNDRY_COMMAND_LINE_GET_CLASS (self)->printerr (self, message);
}
gboolean
foundry_command_line_isatty (FoundryCommandLine *self)
{
g_return_val_if_fail (FOUNDRY_IS_COMMAND_LINE (self), FALSE);
return FOUNDRY_COMMAND_LINE_GET_CLASS (self)->isatty (self);
}
void
foundry_command_line_help (FoundryCommandLine *self)
{
g_return_if_fail (FOUNDRY_IS_COMMAND_LINE (self));
foundry_command_line_print (self, "%s\n", _("Usage:"));
foundry_command_line_print (self, " foundry [OPTIONS…] COMMAND\n");
foundry_command_line_print (self, "\n");
foundry_command_line_print (self, "%s:\n", _("Commands"));
foundry_command_line_print (self, " init %s\n", _("Initialize a new project in current directory"));
//foundry_command_line_print (self, " create %s\n", _("Create a new project from template"));
foundry_command_line_print (self, "\n");
foundry_command_line_print (self, "%s:\n", _("Environment Commands"));
foundry_command_line_print (self, " enter %s\n", _("Enter environment of project in current directory"));
foundry_command_line_print (self, " devenv %s\n", _("Enter build environment for project"));
foundry_command_line_print (self, "\n");
foundry_command_line_print (self, "%s:\n", _("Build Commands"));
foundry_command_line_print (self, " build %s\n", _("Build the current build pipeline"));
foundry_command_line_print (self, " rebuild %s\n", _("Wipe and rebuild the current build pipeline"));
foundry_command_line_print (self, " clean %s\n", _("Clean the current build pipeline"));
foundry_command_line_print (self, " install %s\n", _("Run the install target for the project"));
foundry_command_line_print (self, " deploy %s\n", _("Deploy the project to the active device"));
foundry_command_line_print (self, " export %s\n", _("Export project artifacts"));
foundry_command_line_print (self, "\n");
foundry_command_line_print (self, "%s:\n", _("Run Commands"));
foundry_command_line_print (self, " run %s\n", _("Run a project command"));
//foundry_command_line_print (self, " debug %s\n", _("Run a project commmnd in the debugger"));
//foundry_command_line_print (self, " profile %s\n", _("Run a project commmnd in the profiler"));
//foundry_command_line_print (self, " valgrind %s\n", _("Run a project commmnd under valgrind"));
//foundry_command_line_print (self, " test %s\n", _("Run unit tests"));
foundry_command_line_print (self, "\n");
//foundry_command_line_print (self, "%s:\n", _("User Commands"));
//foundry_command_line_print (self, " command list %s\n", _("List registered commands"));
//foundry_command_line_print (self, " command add %s\n", _("Add a new user command or group"));
//foundry_command_line_print (self, " command remove %s\n", _("Remove a user command or group"));
//foundry_command_line_print (self, "\n");
foundry_command_line_print (self, "%s:\n", _("File/Directory Commands"));
//foundry_command_line_print (self, " search %s\n", _("Search the project"));
//foundry_command_line_print (self, " replace %s\n", _("Search and replace within the project"));
//foundry_command_line_print (self, " index %s\n", _("Update source code indexes"));
//foundry_command_line_print (self, " format %s\n", _("Reformat a file"));
//foundry_command_line_print (self, " symbols %s\n", _("List symbols within a file"));
foundry_command_line_print (self, " diagnose %s\n", _("List diagnostics within a file or files"));
foundry_command_line_print (self, " show %s\n", _("Open a file or files in file browser"));
foundry_command_line_print (self, "\n");
foundry_command_line_print (self, "%s:\n", _("Documentation Commands"));
foundry_command_line_print (self, " doc bundle list %s\n", _("List available documentation bundles"));
foundry_command_line_print (self, " doc bundle install %s\n", _("Install a specific documentation bundle"));
foundry_command_line_print (self, " doc query %s\n", _("Search for documentation"));
foundry_command_line_print (self, "\n");
foundry_command_line_print (self, "%s:\n", _("Configuration Commands"));
foundry_command_line_print (self, " config list %s\n", _("List available configurations"));
foundry_command_line_print (self, " config switch %s\n", _("Change the active configuration"));
foundry_command_line_print (self, "\n");
foundry_command_line_print (self, "%s:\n", _("SDK Commands"));
foundry_command_line_print (self, " sdk list %s\n", _("List available SDKs"));
foundry_command_line_print (self, " sdk install %s\n", _("Install a specific SDK"));
foundry_command_line_print (self, " sdk switch %s\n", _("Change the active SDK for the build pipeline"));
foundry_command_line_print (self, "\n");
foundry_command_line_print (self, "%s:\n", _("Pipeline Commands"));
foundry_command_line_print (self, " pipeline info %s\n", _("Show information about the pipeline"));
foundry_command_line_print (self, " pipeline build %s\n", _("Build the current pipeline"));
foundry_command_line_print (self, " pipeline rebuild %s\n", _("Rebuild the current pipeline"));
foundry_command_line_print (self, " pipeline clean %s\n", _("Clean the current pipeline"));
foundry_command_line_print (self, " pipeline purge %s\n", _("Delete contents related to build"));
foundry_command_line_print (self, " pipeline configure %s\n", _("Run pipeline through configure phase"));
//foundry_command_line_print (self, " pipeline insert %s\n", _("Insert a command into the pipeline"));
foundry_command_line_print (self, "\n");
foundry_command_line_print (self, "%s:\n", _("Device Commands"));
foundry_command_line_print (self, " device list %s\n", _("List available devices"));
foundry_command_line_print (self, " device switch %s\n", _("Switch the current target device"));
//foundry_command_line_print (self, " device pair %s\n", _("Pair a device"));
//foundry_command_line_print (self, " device shell %s\n", _("Open a shell on a device"));
foundry_command_line_print (self, "\n");
foundry_command_line_print (self, "%s:\n", _("LSP Commands"));
foundry_command_line_print (self, " lsp list %s\n", _("List available language server plugins"));
foundry_command_line_print (self, " lsp run %s\n", _("Run a language server for specific language"));
foundry_command_line_print (self, " lsp prefer %s\n", _("Set preferred LSP for language"));
foundry_command_line_print (self, "\n");
foundry_command_line_print (self, "Examples:\n");
foundry_command_line_print (self, " # Enter project directory\n");
foundry_command_line_print (self, " cd ~/Projects/gnome-builder\n");
foundry_command_line_print (self, "\n");
foundry_command_line_print (self, " # Run once to setup foundry\n");
foundry_command_line_print (self, " foundry init\n");
foundry_command_line_print (self, "\n");
foundry_command_line_print (self, " # Build the project\n");
foundry_command_line_print (self, " foundry build\n");
foundry_command_line_print (self, "\n");
foundry_command_line_print (self, " # Run the project\n");
foundry_command_line_print (self, " foundry run\n");
foundry_command_line_print (self, "\n");
foundry_command_line_print (self, " # Enter the build environment\n");
foundry_command_line_print (self, " foundry devenv\n");
foundry_command_line_print (self, "\n");
foundry_command_line_print (self, " # Start a shell in 'Run' environment\n");
foundry_command_line_print (self, " foundry run -- bash\n");
foundry_command_line_print (self, "\n");
foundry_command_line_print (self, " # Start a language server on stdin/out for python\n");
foundry_command_line_print (self, " foundry lsp run python3\n");
foundry_command_line_print (self, "\n");
foundry_command_line_print (self, " # Entry persistent foundry IDE environment\n");
foundry_command_line_print (self, " # where commands will run in parent process.\n");
foundry_command_line_print (self, " foundry enter\n");
foundry_command_line_print (self, "\n");
}
/**
* foundry_command_line_run:
* @self: a #FoundryCommandLine
*
* Runs the command line.
*
* Returns: (transfer full): a #DexFuture that resolves to an int
*/
DexFuture *
foundry_command_line_run (FoundryCommandLine *self,
const char * const *argv)
{
g_return_val_if_fail (FOUNDRY_IS_COMMAND_LINE (self), NULL);
g_return_val_if_fail (argv != NULL, NULL);
return FOUNDRY_COMMAND_LINE_GET_CLASS (self)->run (self, argv);
}
char *
foundry_command_line_get_directory (FoundryCommandLine *self)
{
g_return_val_if_fail (FOUNDRY_IS_COMMAND_LINE (self), NULL);
return FOUNDRY_COMMAND_LINE_GET_CLASS (self)->get_directory (self);
}
/**
* foundry_command_line_get_environ:
* @self: a #FoundryCommandLine
*
* Returns: (transfer full): the environment of the command line
*/
char **
foundry_command_line_get_environ (FoundryCommandLine *self)
{
g_return_val_if_fail (FOUNDRY_IS_COMMAND_LINE (self), NULL);
return FOUNDRY_COMMAND_LINE_GET_CLASS (self)->get_environ (self);
}
gboolean
foundry_command_line_is_remote (FoundryCommandLine *self)
{
g_return_val_if_fail (FOUNDRY_IS_COMMAND_LINE (self), FALSE);
return FOUNDRY_IS_COMMAND_LINE_REMOTE (self);
}
DexFuture *
foundry_command_line_open (FoundryCommandLine *self,
int fd_number)
{
g_return_val_if_fail (FOUNDRY_IS_COMMAND_LINE (self), NULL);
if (fd_number < 0)
return dex_future_new_reject (G_FILE_ERROR,
G_FILE_ERROR_BADF,
"Invalid fd number");
return FOUNDRY_COMMAND_LINE_GET_CLASS (self)->open (self, fd_number);
}
const char *
foundry_command_line_getenv (FoundryCommandLine *self,
const char *name)
{
g_return_val_if_fail (FOUNDRY_IS_COMMAND_LINE (self), NULL);
return FOUNDRY_COMMAND_LINE_GET_CLASS (self)->getenv (self, name);
}
int
foundry_command_line_get_stdin (FoundryCommandLine *self)
{
g_return_val_if_fail (FOUNDRY_IS_COMMAND_LINE (self), -1);
return FOUNDRY_COMMAND_LINE_GET_CLASS (self)->get_stdin (self);
}
int
foundry_command_line_get_stdout (FoundryCommandLine *self)
{
g_return_val_if_fail (FOUNDRY_IS_COMMAND_LINE (self), -1);
return FOUNDRY_COMMAND_LINE_GET_CLASS (self)->get_stdout (self);
}
int
foundry_command_line_get_stderr (FoundryCommandLine *self)
{
g_return_val_if_fail (FOUNDRY_IS_COMMAND_LINE (self), -1);
return FOUNDRY_COMMAND_LINE_GET_CLASS (self)->get_stderr (self);
}
void
foundry_command_line_print_object (FoundryCommandLine *self,
GObject *object,
const FoundryObjectSerializerEntry *entries,
FoundryObjectSerializerFormat format)
{
g_autoptr(GListStore) store = NULL;
g_return_if_fail (FOUNDRY_IS_COMMAND_LINE (self));
g_return_if_fail (G_IS_OBJECT (object));
store = g_list_store_new (G_OBJECT_TYPE (object));
g_list_store_append (store, object);
foundry_command_line_print_list (self, G_LIST_MODEL (store), entries, format, G_TYPE_INVALID);
}
typedef struct _Column
{
const char *title;
GParamSpec *pspec;
gsize longest;
guint is_boolean : 1;
guint is_number : 1;
guint is_enum : 1;
guint is_flags : 1;
guint is_strv : 1;
guint is_datetime : 1;
} Column;
static void
foundry_command_line_print_sized (FoundryCommandLine *self,
gsize size,
const char *message,
gboolean bold)
{
gsize len = 0;
if (message != NULL)
{
len = strlen (message);
if (bold && foundry_command_line_isatty (self))
foundry_command_line_print (self, "\e[1m%s\e[22m", message);
else
foundry_command_line_print (self, "%s", message);
}
for (; len < size; len++)
foundry_command_line_print (self, " ");
}
static gboolean
is_number_type (GType type)
{
switch ((int) type)
{
case G_TYPE_UINT:
case G_TYPE_UINT64:
case G_TYPE_INT:
case G_TYPE_INT64:
case G_TYPE_LONG:
case G_TYPE_ULONG:
case G_TYPE_DOUBLE:
case G_TYPE_FLOAT:
return TRUE;
default:
return FALSE;
}
}
void
foundry_command_line_print_list (FoundryCommandLine *self,
GListModel *model,
const FoundryObjectSerializerEntry *entries,
FoundryObjectSerializerFormat format,
GType expected_type)
{
g_autofree Column *columns = NULL;
g_autoptr(GTypeClass) klass = NULL;
g_autoptr(GStringChunk) chunk = NULL;
g_autoptr(GPtrArray) strings = NULL;
GType item_type;
guint n_items;
guint n_columns;
g_assert (FOUNDRY_IS_COMMAND_LINE (self));
g_assert (!model || G_IS_LIST_MODEL (model));
g_assert (entries != NULL);
if (model == NULL)
return;
chunk = g_string_chunk_new (4096);
strings = g_ptr_array_new ();
if (expected_type != G_TYPE_INVALID)
item_type = expected_type;
else
item_type = g_list_model_get_item_type (model);
klass = g_type_class_ref (item_type);
for (n_columns = 0; entries[n_columns].property; n_columns++);
columns = g_new0 (Column, n_columns);
for (guint c = 0; c < n_columns; c++)
{
columns[c].title = entries[c].heading;
columns[c].longest = strlen (entries[c].heading);
columns[c].pspec = g_object_class_find_property (G_OBJECT_CLASS (klass), entries[c].property);
if (columns[c].pspec == NULL)
{
g_critical ("Object type %s does not have property '%s'",
g_type_name (item_type),
entries[c].property);
return;
}
columns[c].is_datetime = g_type_is_a (columns[c].pspec->value_type, G_TYPE_DATE_TIME);
columns[c].is_enum = G_TYPE_IS_ENUM (columns[c].pspec->value_type);
columns[c].is_flags = G_TYPE_IS_FLAGS (columns[c].pspec->value_type);
columns[c].is_strv = columns[c].pspec->value_type == G_TYPE_STRV;
columns[c].is_boolean = columns[c].pspec->value_type == G_TYPE_BOOLEAN;
columns[c].is_number = is_number_type (columns[c].pspec->value_type);
}
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);
for (guint ii = 0; entries[ii].property; ii++)
{
Column *column = &columns[ii];
g_auto(GValue) value = G_VALUE_INIT;
g_autofree char *tmpstr = NULL;
const char *str;
if (column->is_boolean)
{
g_value_init (&value, G_TYPE_BOOLEAN);
g_object_get_property (object, entries[ii].property, &value);
if (format == FOUNDRY_OBJECT_SERIALIZER_FORMAT_JSON)
str = g_value_get_boolean (&value) ? "true" : "false";
else
{
if (g_value_get_boolean (&value))
str = g_string_chunk_insert_const (chunk, _("Yes"));
else
str = g_string_chunk_insert_const (chunk, _("No"));
}
}
else if (column->is_datetime)
{
GDateTime *dt;
g_value_init (&value, G_TYPE_DATE_TIME);
g_object_get_property (object, entries[ii].property, &value);
if ((dt = g_value_get_boxed (&value)))
{
if (format == FOUNDRY_OBJECT_SERIALIZER_FORMAT_JSON)
tmpstr = g_date_time_format_iso8601 (dt);
else
tmpstr = g_date_time_format (dt, "%x %X");
}
str = tmpstr;
}
else if (column->is_enum)
{
g_autoptr(GEnumClass) enum_class = g_type_class_ref (column->pspec->value_type);
GEnumValue *v;
g_value_init (&value, column->pspec->value_type);
g_object_get_property (object, entries[ii].property, &value);
if ((v = g_enum_get_value (enum_class, g_value_get_enum (&value))))
str = g_intern_string (v->value_nick);
else
str = NULL;
}
else if (column->is_flags)
{
g_autoptr(GFlagsClass) flags_class = g_type_class_ref (column->pspec->value_type);
g_autoptr(GString) gstr = g_string_new (NULL);
GFlagsValue *v;
guint flags;
g_value_init (&value, column->pspec->value_type);
g_object_get_property (object, entries[ii].property, &value);
flags = g_value_get_flags (&value);
while ((v = g_flags_get_first_value (flags_class, flags)))
{
if (gstr->len > 0)
g_string_append_c (gstr, '|');
g_string_append (gstr, v->value_nick);
flags &= ~v->value;
if (flags == 0)
break;
}
g_value_unset (&value);
g_value_init (&value, G_TYPE_STRING);
g_value_take_string (&value, g_string_free (g_steal_pointer (&gstr), FALSE));
str = g_value_get_string (&value);
}
else if (column->is_strv)
{
g_auto(GStrv) strv = NULL;
g_autoptr(GString) gstr = g_string_new (NULL);
g_value_init (&value, G_TYPE_STRV);
g_object_get_property (object, entries[ii].property, &value);
if ((strv = g_value_dup_boxed (&value)))
{
if (format == FOUNDRY_OBJECT_SERIALIZER_FORMAT_JSON)
g_string_append (gstr, "[");
for (guint z = 0; strv[z]; z++)
{
g_autofree char *escaped = g_strescape (strv[z], NULL);
if (format == FOUNDRY_OBJECT_SERIALIZER_FORMAT_JSON)
g_string_append_printf (gstr, "\"%s\"", escaped);
else
g_string_append (gstr, escaped);
if (strv[z+1] != NULL)
g_string_append (gstr, ", ");
}
if (format == FOUNDRY_OBJECT_SERIALIZER_FORMAT_JSON)
g_string_append (gstr, "]");
}
g_value_unset (&value);
g_value_init (&value, G_TYPE_STRING);
g_value_take_string (&value, g_string_free (g_steal_pointer (&gstr), FALSE));
str = g_value_get_string (&value);
}
else
{
g_value_init (&value, G_TYPE_STRING);
g_object_get_property (object, entries[ii].property, &value);
str = g_value_get_string (&value);
}
if (str != NULL)
{
if (column->is_strv)
{
column->longest = MAX (column->longest, strlen (str));
str = g_string_chunk_insert_const (chunk, str);
}
else
{
g_autofree char *escaped = g_strescape (str, NULL);
column->longest = MAX (column->longest, strlen (escaped));
str = g_string_chunk_insert_const (chunk, escaped);
}
}
g_ptr_array_add (strings, (char *)str);
}
}
if (format == FOUNDRY_OBJECT_SERIALIZER_FORMAT_TEXT)
{
for (guint c = 0; c < n_columns; c++)
{
const Column *column = &columns[c];
guint len = c + 1 == n_columns ? strlen (column->title) : column->longest;
if (c > 0)
foundry_command_line_print (self, " ");
foundry_command_line_print_sized (self, len, column->title, TRUE);
}
foundry_command_line_print (self, "\n");
g_assert (strings->len % n_columns == 0);
for (guint i = 0; i < n_items; i++)
{
for (guint ii = 0; ii < n_columns; ii++)
{
const Column *column = &columns[ii];
const char *str = strings->pdata[i * n_columns + ii];
if (ii > 0)
foundry_command_line_print (self, " ");
foundry_command_line_print_sized (self, column->longest, str, FALSE);
}
foundry_command_line_print (self, "\n");
}
}
else if (format == FOUNDRY_OBJECT_SERIALIZER_FORMAT_JSON)
{
foundry_command_line_print (self, "[");
for (guint i = 0; i < n_items; i++)
{
if (i > 0)
foundry_command_line_print (self, ", ");
foundry_command_line_print (self, "{");
for (guint ii = 0; ii < n_columns; ii++)
{
const Column *column = &columns[ii];
const char *str = strings->pdata[i * n_columns + ii];
if (ii > 0)
foundry_command_line_print (self, ", ");
foundry_command_line_print (self, "\"%s\": ", column->pspec->name);
if (column->is_boolean || column->is_strv || column->is_number)
foundry_command_line_print (self, "%s", str);
else if (str == NULL)
foundry_command_line_print (self, "null");
else
foundry_command_line_print (self, "\"%s\"", str);
}
foundry_command_line_print (self, "}");
}
foundry_command_line_print (self, "]\n");
}
}
FoundryObjectSerializerFormat
foundry_object_serializer_format_parse (const char *string)
{
static GEnumClass *klass;
GEnumValue *value;
if (string == NULL)
return FOUNDRY_OBJECT_SERIALIZER_FORMAT_TEXT;
if G_UNLIKELY (klass == NULL)
klass = g_type_class_ref (FOUNDRY_TYPE_OBJECT_SERIALIZER_FORMAT);
if ((value = g_enum_get_value_by_nick (klass, string)))
return value->value;
return FOUNDRY_OBJECT_SERIALIZER_FORMAT_TEXT;
}
void
foundry_command_line_clear_progress (FoundryCommandLine *self)
{
g_return_if_fail (FOUNDRY_IS_COMMAND_LINE (self));
if (foundry_command_line_isatty (self))
foundry_command_line_print (self, "\033]9;4;0\e\\");
}
void
foundry_command_line_set_progress (FoundryCommandLine *self,
guint percent)
{
g_return_if_fail (FOUNDRY_IS_COMMAND_LINE (self));
if (foundry_command_line_isatty (self))
foundry_command_line_print (self, "\033]9;4;1;%d\e\\", MIN (percent, 100));
}
void
foundry_command_line_clear_line (FoundryCommandLine *self)
{
g_return_if_fail (FOUNDRY_IS_COMMAND_LINE (self));
if (foundry_command_line_isatty (self))
foundry_command_line_print (self, "\033[2K\r");
else
foundry_command_line_print (self, "\n");
}
void
foundry_command_line_set_title (FoundryCommandLine *self,
const char *title)
{
g_autofree char *escaped = NULL;
g_autofree char *command = NULL;
gsize len;
g_return_if_fail (FOUNDRY_IS_COMMAND_LINE (self));
if (title == NULL)
escaped = g_strdup ("");
else
escaped = g_strescape (title, NULL);
command = g_strdup_printf ("\e]0;%s\e\\", escaped);
len = strlen (command);
if (len != write (foundry_command_line_get_stdout (self), command, len))
{
/* Do Nothing */
}
}
/**
* foundry_command_line_dup_auth_provider:
* @self: a [class@Foundry.CommandLine]
*
* Gets an auth provider for this command line client if possible.
*
* Currrently, this only returns an auth provider if the command line
* client provides a TTY.
*
* Returns: (transfer full) (nullable):
*/
FoundryAuthProvider *
foundry_command_line_dup_auth_provider (FoundryCommandLine *self)
{
g_return_val_if_fail (FOUNDRY_IS_COMMAND_LINE (self), NULL);
if (foundry_command_line_isatty (self))
return foundry_tty_auth_provider_new (foundry_command_line_get_stdin (self));
return NULL;
}
/**
* foundry_command_line_build_file_for_arg:
* @self: a [class@Foundry.CommandLine]
*
* Returns: (transfer full):
*/
GFile *
foundry_command_line_build_file_for_arg (FoundryCommandLine *self,
const char *arg)
{
g_return_val_if_fail (FOUNDRY_IS_COMMAND_LINE (self), NULL);
g_return_val_if_fail (arg != NULL, NULL);
if (g_path_is_absolute (arg))
return g_file_new_for_path (arg);
return g_file_new_build_filename (foundry_command_line_get_directory (self), arg, NULL);
}
/**
* foundry_command_line_request_input:
* @self: a [class@Foundry.CommandLine]
* @input: a [class@Foundry.Input]
*
* Queries the user for the information requested in @input.
*
* Returns: (transfer full): a [class@Dex.Future] that resolves to
* any value or rejects with error.
*/
DexFuture *
foundry_command_line_request_input (FoundryCommandLine *self,
FoundryInput *input)
{
int pty_fd;
dex_return_error_if_fail (FOUNDRY_IS_COMMAND_LINE (self));
dex_return_error_if_fail (FOUNDRY_IS_INPUT (input));
pty_fd = foundry_command_line_get_stdout (self);
return foundry_command_line_input (pty_fd, input);
}
|