summaryrefslogtreecommitdiff
path: root/src/ostree/main.c
blob: cf0e89c16a6b390ff4c6d8e810996d8abe6f4fb7 (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
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
 *
 * Copyright (C) 2011 Colin Walters <walters@verbum.org>
 *
 * 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 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, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Colin Walters <walters@verbum.org>
 */

#include "config.h"

#include <gio/gio.h>

#include <string.h>

#include "ot-builtins.h"

static OstreeBuiltin builtins[] = {
  { "checkout", ostree_builtin_checkout, 0 },
  { "diff", ostree_builtin_diff, 0 },
  { "init", ostree_builtin_init, 0 },
  { "commit", ostree_builtin_commit, 0 },
  { "compose", ostree_builtin_compose, 0 },
  { "log", ostree_builtin_log, 0 },
#ifdef HAVE_LIBSOUP_GNOME
  { "pull", ostree_builtin_pull, 0 },
#endif
  { "fsck", ostree_builtin_fsck, 0 },
  { "remote", ostree_builtin_remote, 0 },
  { "rev-parse", ostree_builtin_rev_parse, 0 },
  { "remote", ostree_builtin_remote, 0 },
  { "run-triggers", ostree_builtin_run_triggers, 0 },
  { "show", ostree_builtin_show, 0 },
  { NULL }
};

static int
usage (char **argv, gboolean is_error)
{
  OstreeBuiltin *builtin = builtins;
  void (*print_func) (const gchar *format, ...);

  if (is_error)
    print_func = g_printerr;
  else
    print_func = g_print;

  print_func ("usage: %s --repo=PATH COMMAND [options]\n",
              argv[0]);
  print_func ("Builtin commands:\n");

  while (builtin->name)
    {
      print_func ("  %s\n", builtin->name);
      builtin++;
    }
  return (is_error ? 1 : 0);
}


int
main (int    argc,
      char **argv)
{
  OstreeBuiltin *builtin;
  const char *cmd;
  const char *repo;

  g_type_init ();

  g_set_prgname (argv[0]);

  builtin = builtins;

  if (argc < 3)
    return usage (argv, 1);
  
  if (!g_str_has_prefix (argv[1], "--repo="))
    return usage (argv, 1);
  repo = argv[1] + strlen ("--repo=");

  cmd = argv[2];

  while (builtin->name)
    {
      GError *error = NULL;
      if (strcmp (cmd, builtin->name) == 0)
        {
          int i;
          int tmp_argc;
          char **tmp_argv;

          tmp_argc = argc - 2;
          tmp_argv = g_new0 (char *, tmp_argc + 1);

          tmp_argv[0] = (char*)builtin->name;
          for (i = 0; i < tmp_argc; i++)
            tmp_argv[i+1] = argv[i+3];
          if (!builtin->fn (tmp_argc, tmp_argv, repo, &error))
            {
              g_free (tmp_argv);
              g_printerr ("%s\n", error->message);
              g_clear_error (&error);
              return 1;
            }
          g_free (tmp_argv);
          return 0;
        }
      builtin++;
    }
  
  g_printerr ("Unknown command '%s'\n", cmd);
  return usage (argv, 1);
}