summaryrefslogtreecommitdiff
path: root/app/flatpak-builtins-list-remotes.c
blob: 70c9afbbeb0ba8c54f72f1e9e7a5e6b8fe2da1e9 (plain)
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
/*
 * Copyright © 2014 Red Hat, Inc
 *
 * This program 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 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 Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *       Alexander Larsson <alexl@redhat.com>
 */

#include "config.h"

#include <locale.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "libgsystem.h"
#include "libglnx/libglnx.h"

#include "flatpak-builtins.h"
#include "flatpak-utils.h"

static gboolean opt_show_details;
static gboolean opt_user;
static gboolean opt_system;
static gboolean opt_show_disabled;

static GOptionEntry options[] = {
  { "user", 0, 0, G_OPTION_ARG_NONE, &opt_user, "Show user installations", NULL },
  { "system", 0, 0, G_OPTION_ARG_NONE, &opt_system, "Show system-wide installations", NULL },
  { "show-details", 'd', 0, G_OPTION_ARG_NONE, &opt_show_details, "Show remote details", NULL },
  { "show-disabled", 0, 0, G_OPTION_ARG_NONE, &opt_show_disabled, "Show disabled remotes", NULL },
  { NULL }
};

gboolean
flatpak_builtin_list_remotes (int argc, char **argv, GCancellable *cancellable, GError **error)
{
  g_autoptr(GOptionContext) context = NULL;
  g_autoptr(FlatpakDir) user_dir = NULL;
  g_autoptr(FlatpakDir) system_dir = NULL;
  FlatpakDir *dirs[2] = { 0 };
  guint i = 0, n_dirs = 0, j;
  FlatpakTablePrinter *printer;

  context = g_option_context_new (" - List remote repositories");

  if (!flatpak_option_context_parse (context, options, &argc, &argv, FLATPAK_BUILTIN_FLAG_NO_DIR, NULL, cancellable, error))
    return FALSE;

  if (!opt_user && !opt_system)
    opt_system = TRUE;

  if (opt_user)
    {
      user_dir = flatpak_dir_get_user ();
      dirs[n_dirs++] = user_dir;
    }

  if (opt_system)
    {
      system_dir = flatpak_dir_get_system ();
      dirs[n_dirs++] = system_dir;
    }

  printer = flatpak_table_printer_new ();

  for (j = 0; j < n_dirs; j++)
    {
      FlatpakDir *dir = dirs[j];
      g_auto(GStrv) remotes = NULL;

      remotes = flatpak_dir_list_remotes (dir, cancellable, error);
      if (remotes == NULL)
        return FALSE;

      for (i = 0; remotes[i] != NULL; i++)
        {
          char *remote_name = remotes[i];
          gboolean disabled;

          disabled = flatpak_dir_get_remote_disabled (dir, remote_name);
          if (disabled && !opt_show_disabled)
            continue;

          if (opt_show_details)
            {
              g_autofree char *remote_url = NULL;
              g_autofree char *title = NULL;
              int prio;
              g_autofree char *prio_as_string = NULL;
              gboolean gpg_verify = TRUE;

              flatpak_table_printer_add_column (printer, remote_name);

              title = flatpak_dir_get_remote_title (dir, remote_name);
              if (title)
                flatpak_table_printer_add_column (printer, title);
              else
                flatpak_table_printer_add_column (printer, "-");

              ostree_repo_remote_get_url (flatpak_dir_get_repo (dir), remote_name, &remote_url, NULL);

              flatpak_table_printer_add_column (printer, remote_url);

              prio = flatpak_dir_get_remote_prio (dir, remote_name);
              prio_as_string = g_strdup_printf ("%d", prio);
              flatpak_table_printer_add_column (printer, prio_as_string);

              flatpak_table_printer_add_column (printer, ""); /* Options */

              ostree_repo_remote_get_gpg_verify (flatpak_dir_get_repo (dir), remote_name,
                                                 &gpg_verify, NULL);
              if (!gpg_verify)
                flatpak_table_printer_append_with_comma (printer, "no-gpg-verify");
              if (disabled)
                flatpak_table_printer_append_with_comma (printer, "disabled");

              if (flatpak_dir_get_remote_noenumerate (dir, remote_name))
                flatpak_table_printer_append_with_comma (printer, "no-enumerate");

              if (opt_user && opt_system)
                flatpak_table_printer_append_with_comma (printer, dir == user_dir ? "user" : "system");
            }
          else
            {
              flatpak_table_printer_add_column (printer, remote_name);
            }

          flatpak_table_printer_finish_row (printer);
        }
    }

  flatpak_table_printer_print (printer);
  flatpak_table_printer_free (printer);

  return TRUE;
}