aboutsummaryrefslogtreecommitdiff
path: root/bdep/config.cxx
blob: 0dcea4838bd8dd7a7de5c837555974b7a5843c2e (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
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
// file      : bdep/config.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <bdep/config.hxx>

#include <bdep/database.hxx>
#include <bdep/project-odb.hxx>
#include <bdep/diagnostics.hxx>

using namespace std;

namespace bdep
{
  shared_ptr<configuration>
  cmd_config_add (const dir_path&    prj,
                  database&          db,
                  dir_path           path,
                  optional<string>   name,
                  optional<bool>     def,
                  optional<bool>     fwd,
                  optional<uint64_t> id,
                  const char*        what)
  {
    if (!exists (path))
      fail << "configuration directory " << path << " does not exist";

    transaction t (db.begin ());

    using count = configuration_count;

    {
      using query = bdep::query<configuration>;

      // By default the first added configuration is the default.
      //
      if (!def)
        def = (db.query_value<count> () == 0);
      else if (*def)
      {
        if (auto p = db.query_one<configuration> (query::default_))
          fail << "configuration " << *p << " is already the default" <<
            info << "use 'bdep config set --no-default' to clear";
      }

      // By default the default configuration is forwarded unless another
      // is already forwarded.
      //
      if (!fwd)
        fwd = *def && db.query_one<configuration> (query::forward) == nullptr;
      else if (*fwd)
      {
        if (auto p = db.query_one<configuration> (query::forward))
          fail << "configuration " << *p << " is already forwarded" <<
            info << "use 'bdep config set --no-forward' to clear";
      }
    }

    // Make sure the configuration path is absolute and normalized. Also
    // derive relative to project directory path if possible.
    //
    path.complete ();
    path.normalize ();

    optional<dir_path> rel_path;
    try {rel_path = path.relative (prj);} catch (const invalid_path&) {}

    shared_ptr<configuration> r (
      new configuration {
        id,
        name,
        path,
        move (rel_path),
        *def,
        *fwd,
        {} /* packages */});

    try
    {
      db.persist (r);
    }
    catch (const odb::exception&)
    {
      using query = bdep::query<count>;

      // See if this is id, name, or path conflict.
      //
      if (id && db.query_value<count> (query::id == *id) != 0)
        fail << "configuration with id " << *id << " already exists "
             << "in project " << prj;

      if (name && db.query_value<count> (query::name == *name) != 0)
        fail << "configuration with name '" << *name << "' already exists "
             << "in project " << prj;

      if (db.query_value<count> (query::path == path.string ()) != 0)
        fail << "configuration with path " << path << " already exists "
             << "in project " << prj;

      // Hm, what could that be?
      //
      throw;
    }

    t.commit ();

    if (verb)
    {
      diag_record dr (text);
      /*            */ dr << what << " configuration ";
      if (r->name)     dr << '@' << *r->name << ' ';
      /*            */ dr << r->path << " (" << *r->id;
      if (r->default_) dr << ", default";
      if (r->forward)  dr << ", forwarded";
      /*            */ dr << ')';
    }

    return r;
  }

  static int
  cmd_config_add (const cmd_config_options&, cli::scanner&)
  {
    fail << "@@ TODO" << endf;
  }

  shared_ptr<configuration>
  cmd_config_create (const common_options& co,
                     const dir_path&       prj,
                     database&             db,
                     dir_path              path,
                     cli::scanner&         cfg_args,
                     optional<string>      name,
                     optional<bool>        def,
                     optional<bool>        fwd,
                     optional<uint64_t>    id)
  {
    // Call bpkg to create the configuration.
    //
    {
      strings args;
      while (cfg_args.more ())
        args.push_back (cfg_args.next ());

      run_bpkg (co, "create", "-d", path, args);
    }

    return cmd_config_add (prj,
                           db,
                           move (path),
                           move (name),
                           def,
                           fwd,
                           id,
                           "created");
  }

  static int
  cmd_config_create (const cmd_config_options&, cli::scanner&)
  {
    fail << "@@ TODO" << endf;
  }

  static int
  cmd_config_remove (const cmd_config_options&, cli::scanner&)
  {
    fail << "@@ TODO" << endf;
  }

  static int
  cmd_config_rename (const cmd_config_options&, cli::scanner&)
  {
    fail << "@@ TODO" << endf;
  }

  static int
  cmd_config_set (const cmd_config_options&, cli::scanner&)
  {
    fail << "@@ TODO" << endf;
  }

  const char*
  cmd_config_validate_add (const configuration_add_options& o)
  {
    // --[no-]default
    //
    if (o.default_ () && o.no_default ())
      fail << "both --default and --no-default specified";

    // --[no-]forward
    //
    if (o.forward () && o.no_forward ())
      fail << "both --forward and --no-forward specified";

    return (o.default_ ()   ? "--default"    :
            o.forward ()    ? "--forward"    :
            o.no_default () ? "--no-default" :
            o.no_forward () ? "--no-forward" : nullptr);
  }

  int
  cmd_config (const cmd_config_options& o, cli::scanner& scan)
  {
    tracer trace ("config");

    cmd_config_subcommands c (
      parse_command<cmd_config_subcommands> (scan,
                                             "config subcommand",
                                             "bdep help config"));

    // Validate options/subcommands.
    //
    if (const char* n = cmd_config_validate_add (o))
    {
      if (!(c.add () || c.create () || c.set ()))
        fail << n << " not valid for this command";
    }

    // --all
    //
    if (o.all ())
    {
      if (!c.remove ())
        fail << "--all not valid for this command";
    }

    // Dispatch to subcommand function.
    //
    if (c.add    ()) return cmd_config_add    (o, scan);
    if (c.create ()) return cmd_config_create (o, scan);
    if (c.remove ()) return cmd_config_remove (o, scan);
    if (c.rename ()) return cmd_config_rename (o, scan);
    if (c.set    ()) return cmd_config_set    (o, scan);

    assert (false); // Unhandled (new) subcommand.
    return 1;
  }
}