summaryrefslogtreecommitdiff
path: root/app/flatpak-builtins-info.c
blob: 0c5c5d9d1d97affcf5d5082a60d8910efc27a457 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * Copyright © 2016 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 <unistd.h>
#include <string.h>

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

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

static gboolean opt_user;
static gboolean opt_system;
static gboolean opt_runtime;
static gboolean opt_app;
static gboolean opt_show_ref;
static gboolean opt_show_commit;
static gboolean opt_show_origin;
static char *opt_arch;

static GOptionEntry options[] = {
  { "arch", 0, 0, G_OPTION_ARG_STRING, &opt_arch, "Arch to use", "ARCH" },
  { "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 },
  { "runtime", 0, 0, G_OPTION_ARG_NONE, &opt_runtime, "List installed runtimes", },
  { "app", 0, 0, G_OPTION_ARG_NONE, &opt_app, "List installed applications", },
  { "show-ref", 'r', 0, G_OPTION_ARG_NONE, &opt_show_ref, "Show ref", },
  { "show-commit", 'c', 0, G_OPTION_ARG_NONE, &opt_show_commit, "Show commit", },
  { "show-origin", 'o', 0, G_OPTION_ARG_NONE, &opt_show_origin, "Show origin", },
  { NULL }
};

gboolean
flatpak_builtin_info (int argc, char **argv, GCancellable *cancellable, GError **error)
{
  g_autoptr(GOptionContext) context = NULL;
  g_autofree char *ref = NULL;
  g_autoptr(FlatpakDir) user_dir = NULL;
  g_autoptr(FlatpakDir) system_dir = NULL;
  FlatpakDir *dir = NULL;
  g_autoptr(GError) lookup_error = NULL;
  g_autoptr(GVariant) deploy_data = NULL;
  const char *name;
  const char *branch = "master";
  const char *commit = NULL;
  const char *origin = NULL;
  gboolean is_app = FALSE;
  gboolean first = TRUE;

  context = g_option_context_new ("NAME [BRANCH] - Get info about installed app and/or runtime");

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

  if (argc < 2)
    return usage_error (context, "NAME must be specified", error);
  name = argv[1];

  if (argc >= 3)
    branch = argv[2];

  if (!opt_app && !opt_runtime)
    opt_app = opt_runtime = TRUE;

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

  if (opt_user)
    {
      user_dir = flatpak_dir_get_user ();

      ref = flatpak_dir_find_installed_ref (user_dir,
                                            name,
                                            branch,
                                            opt_arch,
                                            opt_app, opt_runtime, &is_app,
                                            &lookup_error);
      if (ref)
        dir = user_dir;
    }

  if (ref == NULL && opt_system)
    {
      system_dir = flatpak_dir_get_system ();

      ref = flatpak_dir_find_installed_ref (system_dir,
                                            name,
                                            branch,
                                            opt_arch,
                                            opt_app, opt_runtime, &is_app,
                                            lookup_error == NULL ? &lookup_error : NULL);
      if (ref)
        dir = system_dir;
    }

  if (ref == NULL)
    {
      g_propagate_error (error, g_steal_pointer (&lookup_error));
      return FALSE;
    }

  deploy_data = flatpak_dir_get_deploy_data (dir, ref, cancellable, error);
  if (deploy_data == NULL)
    return FALSE;

  commit = flatpak_deploy_data_get_commit (deploy_data);
  origin = flatpak_deploy_data_get_origin (deploy_data);

  if (!opt_show_ref && !opt_show_origin && !opt_show_commit)
    opt_show_ref = opt_show_origin = opt_show_commit = TRUE;

  if (opt_show_ref)
    {
      if (first)
        first = FALSE;
      else
        g_print (" ");

      g_print ("%s", ref);
    }

  if (opt_show_origin)
    {
      if (first)
        first = FALSE;
      else
        g_print (" ");

      g_print ("%s", origin ? origin : "-");
    }

  if (opt_show_commit)
    {
      if (first)
        first = FALSE;
      else
        g_print (" ");

      g_print ("%s", commit);
    }

  g_print ("\n");

  return TRUE;
}