aboutsummaryrefslogtreecommitdiff
path: root/bdep/config.cxx
blob: 55a1062d07ec486419ed7977cbf16ec4d5a27877 (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
// 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<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<count>;

    // By default the first added configuration is the default.
    //
    if (!def)
      def = (db.query_value<count> () == 0);
    else if (*def)
    {
      using query = bdep::query<configuration>;

      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";
    }

    // 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,
        {} /* packages */});

    try
    {
      db.persist (r);
    }
    catch (const odb::exception&)
    {
      // 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";
      /*            */ dr << ')';
    }

    return r;
  }

  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<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,
                           id,
                           "created");
  }

  int
  cmd_config (const cmd_config_options& o, cli::scanner&)
  {
    //@@ TODO: get subcommand and pass to tracer.
    //@@ TODO: define a CLI options class for subcommands?

    tracer trace ("config");

    //@@ TODO: validate project/config options for subcommands.

    if (o.default_ () && o.no_default ())
      fail << "both --default and --no-default specified";

    for (const string& n: o.config_name ())
      text << n;

    return 0;
  }
}