aboutsummaryrefslogtreecommitdiff
path: root/bdep/config.cxx
blob: 579753934115e7c611f94af58e158c314ddacd31 (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
// file      : bdep/config.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <bdep/config.hxx>

#include <iostream> // cout

#include <libbutl/json/serializer.hxx>

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

using namespace std;

namespace bdep
{
  template <typename O>
  static void
  print_configuration (O& o,
                       const shared_ptr<configuration>& c,
                       bool flags = true)
  {
    if (c->name)
      o << '@' << *c->name << ' ';

    o << c->path << ' ' << *c->id << ' ' << c->type;

    if (flags)
    {
      char s (' ');
      if (c->default_)  {o << s << "default";           s = ',';}
      if (c->forward)   {o << s << "forwarded";         s = ',';}
      if (c->auto_sync) {o << s << "auto-synchronized"; s = ',';}
    }
  }

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

    // --[no-]auto-sync
    //
    if (o.auto_sync () && o.no_auto_sync ())
      fail << "both --auto-sync and --no-auto-sync specified";

    if (o.existing () && o.wipe ())
      fail << "both --existing|-e and --wipe specified";

    return (o.type_specified () ? "--type|--config-type"  :
            o.default_ ()       ? "--default"             :
            o.no_default ()     ? "--no-default"          :
            o.forward ()        ? "--forward"             :
            o.no_forward ()     ? "--no-forward"          :
            o.auto_sync ()      ? "--auto-sync"           :
            o.no_auto_sync ()   ? "--no-auto-sync"        :
            o.existing ()       ? "--existing|-e"         :
            o.wipe ()           ? "--wipe"                : nullptr);
  }

  void
  cmd_config_validate_add (const configuration_name_options& o,
                           const char* what,
                           optional<string>& name,
                           optional<uint64_t>& id)
  {
    name = nullopt;
    id = nullopt;

    if (size_t n = o.config_name ().size ())
    {
      if (n > 1)
        fail << "multiple configuration names specified for " << what;

      name = o.config_name ()[0].first;
    }

    if (size_t n = o.config_id ().size ())
    {
      if (n > 1)
        fail << "multiple configuration ids specified for " << what;

      id = o.config_id ()[0].first;
    }
  }

  // Translate the configuration directory that is actually a name (@foo or
  // -@foo) to the real directory (prj-foo) and name (@foo).
  //
  static inline void
  translate_path_name (const dir_path& prj,
                       dir_path& path,
                       optional<string>& name)
  {
    if (name || !path.simple ())
      return;

    size_t n;
    {
      const string& s (path.string ());
      if      (s.size () > 1 && s[0] == '@')                n = 1;
      else if (s.size () > 2 && s[0] == '-' && s[1] == '@') n = 2;
      else return;
    }

    string s (move (path).string ()); // Move out.
    s.erase (0, n);                   // Remove leading '@' or '-@'.

    path  = prj;
    path += '-';
    path += s;

    name = move (s);
  }

  // Verify the configuration directory is not inside one of the packages.
  //
  // Note that the new version of our glue buildfile is smart enough to allow
  // creation of configurations inside the project root.
  //
  static void
  verify_configuration_path (const dir_path& cfg,
                             const dir_path& prj,
                             const package_locations& pkgs)
  {
    for (const package_location& p: pkgs)
    {
      dir_path d (prj / p.path); // Should already be normalized.

      if (cfg.sub (d))
        fail << "configuration directory " << cfg << " is inside package "
             << p.name << " (" << d << ")";
    }
  }

  shared_ptr<configuration>
  cmd_config_add (const dir_path&         prj,
                  transaction&            t,
                  const dir_path&         path,
                  const optional<string>& name,
                  string                  type,
                  bool                    default_,
                  bool                    forward,
                  bool                    auto_sync,
                  optional<uint64_t>      id,
                  bool                    dry_run)
  {
    database& db (t.database ());

    //@@ TODO: Maybe redo by querying the conflicting configuration and then
    //         printing its path, like in rename?

    using count = configuration_count;
    using query = bdep::query<count>;

    // See if there is an 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 <<
        info << "use 'bdep config remove --config-id " << *id << "' to remove";

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

    if (db.query_value<count> (query::path == path.string ()) != 0)
      fail << "configuration with directory " << path << " already exists "
           << "in project " << prj <<
        info << "use 'bdep config remove --config " << path << "' to remove";

    if (dry_run)
      return nullptr;

    shared_ptr<configuration> r (
      new configuration {
        id,
        name,
        move (type),
        path,
        path.try_relative (prj),
        default_,
        forward,
        auto_sync,
        {} /* packages */});

    db.persist (r); // Shouldn't fail due to a conflict.

    return r;
  }

  // Quote the string/directory if it contains spaces.
  //
  static inline string
  quote (const string& s)
  {
    return s.find (' ') == string::npos ? s : '"' + s + '"';
  }

  static inline string
  quote (const dir_path& d)
  {
    return quote (d.string ());
  }

  void
  cmd_config_add_print (diag_record& dr,
                        const dir_path& prj,
                        const dir_path& path,
                        const optional<string>& name,
                        bool def,
                        bool fwd,
                        bool asy)
  {
    dr << "bdep config add -d " << quote (prj);

    if (name)
      dr << " @" << *name;

    dr << (def ? " --default" : " --no-default");
    dr << (fwd ? " --forward" : " --no-forward");
    dr << (asy ? "" : " --no-auto-sync");

    dr << ' ' << quote (path);
  }

  shared_ptr<configuration>
  cmd_config_add (const common_options&            co,
                  const configuration_add_options& ao,
                  const dir_path&                  prj,
                  const package_locations&         pkgs,
                  database&                        db,
                  dir_path                         path,
                  optional<string>                 name,
                  optional<string>                 type,
                  optional<uint64_t>               id,
                  const char*                      what,
                  bool                             dry_run)
  {
    translate_path_name (prj, path, name);

    if (name && name->empty ())
      fail << "empty configuration name specified";

    // Note: if dry_run, then configuration may not yet exist.
    //
    if (!dry_run && !exists (path))
      fail << "configuration directory " << path << " does not exist";

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

    verify_configuration_path (path, prj, pkgs);

    // If an existing configuration is being added, then use bpkg-cfg-info to
    // query the configuration type and links, collecting the linked host
    // configuration paths/types.
    //
    vector<pair<dir_path, string>> host_configs;

    if (type)
      ;
    else if (dry_run)
      type = string (); // Not used.
    else
    {
      fdpipe pipe (open_pipe ()); // Text mode seems appropriate.

      process pr (start_bpkg (3,
                              co,
                              pipe /* stdout */,
                              2    /* stderr */,
                              "cfg-info",
                              "--link",
                              "-d", path));

      // Shouldn't throw, unless something is severely damaged.
      //
      pipe.out.close ();

      bool io (false);
      try
      {
        ifdstream is (move (pipe.in), fdstream_mode::skip, ifdstream::badbit);

        // Retrieve the configuration type.
        //
        for (string l; !eof (getline (is, l)); )
        {
          if (l.empty ())
            break;

          // After configuration type is retrieved, continue reading till the
          // end of the configuration information.
          //
          if (l.compare (0, 6, "type: ") == 0)
            type = string (l, 6);
        }

        if (!type || type->empty ())
          fail << "invalid bpkg-cfg-info output: no configuration type";

        // Collect the linked host configurations.
        //
        if (!is.eof ())
        {
          dir_path d;
          string   t;

          auto add_conf = [&d, &t, &host_configs] ()
          {
            if (d.empty ())
              fail << "invalid bpkg-cfg-info output: no linked configuration "
                   << "path";

            if (t.empty ())
              fail << "invalid bpkg-cfg-info output: no linked configuration "
                   << "type";

            if (t == "host" || t == "build2")
              host_configs.emplace_back (move (d), move (t));

            d.clear ();
            t.clear ();
          };

          for (string l; !eof (getline (is, l)); )
          {
            if (l.empty ())
            {
              add_conf ();
            }
            else if (l.compare (0, 6, "path: ") == 0)
            {
              try
              {
                d = dir_path (string (l, 6));
              }
              catch (const invalid_path&)
              {
                fail << "invalid bpkg-cfg-info output line '" << l
                     << "': invalid configuration path";
              }
            }
            else if (l.compare (0, 6, "type: ") == 0)
              t = string (l, 6);
          }

          add_conf (); // Add the remaining config.
        }

        is.close (); // Detect errors.
      }
      catch (const io_error&)
      {
        // Presumably the child process failed and issued diagnostics so let
        // finish_bpkg() try to deal with that first.
        //
        io = true;
      }

      finish_bpkg (co, pr, io);
    }

    transaction t (db.begin ());

    using count = configuration_count;

    optional<bool> def, fwd;
    {
      using query = bdep::query<configuration>;

      // By default the first added for its type configuration is the default.
      //
      if (ao.default_ () || ao.no_default ())
        def = ao.default_ () && !ao.no_default ();

      if (!def)
        def = (db.query_value<count> (query::type == *type) == 0);
      else if (*def)
      {
        if (auto p = db.query_one<configuration> (query::default_ &&
                                                  query::type == *type))
          fail << "configuration " << *p << " of type " << *type
               << " 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 for its configuration type.
      //
      if (ao.forward () || ao.no_forward ())
        fwd = ao.forward () && !ao.no_forward ();

      // Note: there can be multiple forwarded configurations for a type.
      //
      if (!fwd)
        fwd = *def &&
              db.query_value<count> (query::forward &&
                                     query::type == *type) == 0;
    }

    shared_ptr<configuration> r (
      cmd_config_add (prj,
                      t,
                      path,
                      name,
                      move (*type),
                      *def,
                      *fwd,
                      !ao.no_auto_sync (),
                      id,
                      dry_run));

    if (dry_run)
    {
      t.commit ();
      return r;
    }

    // Let's issue a single warning about non-associated host configurations
    // (rather than for each of them) and do it after the configuration is
    // reported as added. So just filter out the associated configurations at
    // this stage.
    //
    for (auto i (host_configs.begin ()); i != host_configs.end (); )
    {
      using query = bdep::query<configuration>;

      shared_ptr<configuration> c (
        db.query_one<configuration> (query::path == i->first.string ()));

      if (c != nullptr)
        i = host_configs.erase (i);
      else
        ++i;
    }

    t.commit ();

    if (verb)
    {
      diag_record dr (text);
      dr << what << " configuration ";
      print_configuration (dr, r);
    }

    if (!host_configs.empty ())
    {
      diag_record dr (warn);
      dr << what << " configuration " << *r << " already linked with host "
         << "configurations";

      for (const pair<dir_path, string>& lc: host_configs)
        dr << info << "configuration of " << lc.second << " type: "
           << lc.first;

      dr << info << "consider adding them to this project if it has "
         << "any build-time dependencies";
    }

    return r;
  }

  // Call bpkg to create the configuration.
  //
  static void
  create_config (const common_options&   co,
                 const dir_path&         path,
                 const optional<string>& name,
                 const string&           type,
                 bool                    existing,
                 bool                    wipe,
                 const strings&          args)
  {
    run_bpkg (2,
              co,
              "create",
              "-d", path,
              (name
               ? strings ({"--name", *name})
               : strings ()),
              "--type", type,
              (existing ? "--existing" : nullptr),
              (wipe     ? "--wipe"     : nullptr),
              "--no-host-config",
              "--no-build2-config",
              args);
  }

  void
  cmd_config_create_print (diag_record& dr,
                           const dir_path& prj,
                           const dir_path& path,
                           const optional<string>& name,
                           const string& type,
                           bool def,
                           bool fwd,
                           bool asy,
                           const strings& args)
  {
    dr << "bdep config create -d " << quote (prj);

    if (name)
      dr << " @" << *name;

    if (type != "target")
      dr << " --type " << type;

    dr << (def ? " --default" : " --no-default");
    dr << (fwd ? " --forward" : " --no-forward");
    dr << (asy ? "" : " --no-auto-sync");

    dr << ' ' << quote (path);

    for (const string& a: args)
      dr << ' ' << quote (a);
  }

  shared_ptr<configuration>
  cmd_config_create (const common_options&   co,
                     const dir_path&         prj,
                     transaction&            t,
                     const dir_path&         path,
                     const optional<string>& name,
                     string                  type,
                     bool                    default_,
                     bool                    forward,
                     bool                    auto_sync,
                     bool                    existing,
                     bool                    wipe,
                     const strings&          args,
                     optional<uint64_t>      id)
  {
    // Check for any path/name/id conflicts before actually creating the
    // configuration. This is especially helpful with --wipe.
    //
    cmd_config_add (prj,
                    t,
                    path,
                    name,
                    type,
                    default_,
                    forward,
                    auto_sync,
                    id,
                    true /* dry_run */);

    create_config (co, path, name, type, existing, wipe, args);

    return cmd_config_add (prj,
                           t,
                           path,
                           name,
                           move (type),
                           default_,
                           forward,
                           auto_sync,
                           id);
  }

  shared_ptr<configuration>
  cmd_config_create (const common_options&            co,
                     const configuration_add_options& ao,
                     const dir_path&                  prj,
                     const package_locations&         pkgs,
                     database&                        db,
                     dir_path                         path,
                     const strings&                   args,
                     optional<string>                 name,
                     string                           type,
                     optional<uint64_t>               id)
  {
    // Similar logic to *_add().
    //
    translate_path_name (prj, path, name);

    normalize (path, "configuration directory");

    verify_configuration_path (path, prj, pkgs);

    // Check for any path/name/id conflicts before actually creating the
    // configuration. This is especially helpful with --wipe.
    //
    const char* what (ao.existing () ? "initialized" : "created");
    cmd_config_add (co,
                    ao,
                    prj,
                    package_locations {}, // Already verified.
                    db,
                    path,
                    name,
                    type,
                    id,
                    what,
                    true /* dry_run */);

    create_config (co, path, name, type, ao.existing (), ao.wipe (), args);

    return cmd_config_add (co,
                           ao,
                           prj,
                           package_locations {},
                           db,
                           move (path),
                           move (name),
                           move (type),
                           id,
                           what);
  }

  void
  cmd_config_link (const common_options& o,
                   const shared_ptr<configuration>& cc,
                   const shared_ptr<configuration>& lc)
  {
    const dir_path& cd (cc->path);
    const dir_path& ld (lc->path);

    // Call bpkg to link the configurations.
    //
    // If possible, rebase the linked configuration directory path relative to
    // the other configuration path. Also use our associated name as the
    // linked configuration name.
    //
    const optional<string>& cn (lc->name);

    run_bpkg (2,
              o,
              "cfg-link",
              ld.try_relative (cd) ? "--relative" : nullptr,
              "-d", cd,
              (cn ? "--name" : nullptr), (cn ? cn->c_str () : nullptr),
              ld);
  }

  static int
  cmd_config_add (const cmd_config_options& o, cli::scanner& args)
  {
    tracer trace ("config_add");

    optional<string> name;
    optional<uint64_t> id;
    cmd_config_validate_add (o, "config add", name, id);

    string arg;
    if (args.more ())
      arg = args.next ();
    else if (name)
    {
      // Reverse into the shortcut form expected by translate_path_name().
      //
      arg = '@' + *name;
      name = nullopt;
    }
    else
      fail << "configuration directory argument expected";

    dir_path path;
    try
    {
      path = dir_path (move (arg));
    }
    catch (const invalid_path&)
    {
      fail << "invalid configuration directory '" << arg << "'";
    }

    dir_path prj (find_project (o));
    database db (open (prj, trace));

    cmd_config_add (o,
                    o,
                    prj,
                    load_packages (prj, true /* allow_empty */),
                    db,
                    move (path),
                    move (name),
                    nullopt,     /* type */
                    move (id));
    return 0;
  }

  static int
  cmd_config_create (const cmd_config_options& o, cli::scanner& args)
  {
    tracer trace ("config_create");

    optional<string> name;
    optional<uint64_t> id;
    cmd_config_validate_add (o, "config create", name, id);

    // Skip `--` which separates the directory argument, if any, as for
    // example in:
    //
    // $ bdep config create -- @gcc cc config.cxx=g++
    //
    bool sep (false);
    if (args.more () && args.peek () == string ("--"))
    {
      sep = true;
      args.next ();
    }

    // Note that the shortcut will only work if there are no cfg-args which
    // is not very likely. Oh, well.
    //
    string arg;
    if (args.more ())
    {
      arg = args.next ();

      // Skip `--` which separates the bpkg options, if any, as for example in:
      //
      // $ bdep config create ../foo-gcc -- -v cc config.cxx=g++
      //
      if (args.more () && args.peek () == string ("--"))
      {
        sep = true;
        args.next ();
      }
    }
    else if (name)
    {
      // Reverse into the shortcut form expected by translate_path_name().
      //
      arg = '@' + *name;
      name = nullopt;
    }
    else
      fail << "configuration directory argument expected";

    dir_path path;
    try
    {
      path = dir_path (move (arg));
    }
    catch (const invalid_path&)
    {
      fail << "invalid configuration directory '" << arg << "'";
    }

    dir_path prj (find_project (o));
    database db (open (prj, trace));

    // Read the configuration arguments.
    //
    // Also make sure that at least one module is specified, unless the `--`
    // separator is specified (in which case we assume that the user knows
    // what they are doing).
    //
    strings cfg_args;
    bool module (false);

    while (args.more ())
    {
      string a (args.next ());

      // Assume the argument is a module unless it is a variable (note that it
      // can't be --existing|-e since no --).
      //
      // Note: the arguments can't be bpkg options if unseparated.
      //
      if (!sep)
      {
        if (a.find ('=') == string::npos)
          module = true;
      }

      cfg_args.push_back (move (a));
    }

    if (!sep && !module)
      fail << "no build system module(s) specified for configuration "
           << "to be created" <<
        info << "for example, for C/C++ configuration, specify 'cc'" <<
        info << "use '--' to create configuration without modules" <<
        info << "for example: bdep config create ... --";

    cmd_config_create (o,
                       o,
                       prj,
                       load_packages (prj, true /* allow_empty */),
                       db,
                       move (path),
                       cfg_args,
                       move (name),
                       o.type (),
                       move (id));
    return 0;
  }

  static int
  cmd_config_link (const cmd_config_options& o, cli::scanner&)
  {
    tracer trace ("config_link");

    // Load project configurations.
    //
    configurations cfgs;
    {
      dir_path prj (find_project (o));
      database db (open (prj, trace));

      transaction t (db.begin ());

      cfgs = find_configurations (o,
                                  prj,
                                  t,
                                  false /* fallback_default */,
                                  true  /* validate         */).first;

      t.commit ();
    }

    if (cfgs.size () != 2)
      fail << "two configurations must be specified for config link";

    cmd_config_link (o, cfgs[0], cfgs[1]);

    if (verb)
      text << "linked configuration " << *cfgs[0] << " (" << cfgs[0]->type
           << ") with configuration " << *cfgs[1] << " (" << cfgs[1]->type
           << ")";

    return 0;
  }

  static int
  cmd_config_unlink (const cmd_config_options& o, cli::scanner&)
  {
    tracer trace ("config_unlink");

    // Load project configurations.
    //
    configurations cfgs;
    {
      dir_path prj (find_project (o));
      database db (open (prj, trace));

      transaction t (db.begin ());

      cfgs = find_configurations (o,
                                  prj,
                                  t,
                                  false /* fallback_default */,
                                  true  /* validate         */).first;

      t.commit ();
    }

    if (cfgs.size () != 2)
      fail << "two configurations must be specified for config unlink";

    // Call bpkg to unlink the configuration.
    //
    run_bpkg (2,
              o,
              "cfg-unlink",
              "-d", cfgs[0]->path,
              cfgs[1]->path);

    if (verb)
      text << "unlinked configuration " << *cfgs[0] << " from configuration "
           << *cfgs[1];

    return 0;
  }

  static void
  cmd_config_list_lines (const configurations& cfgs)
  {
    for (const shared_ptr<configuration>& c: cfgs)
    {
      //@@ TODO: use tabular layout facility when ready.

      print_configuration (cout, c);
      cout << endl;
    }
  }

  static void
  cmd_config_list_json (const configurations& cfgs)
  {
    butl::json::stream_serializer ss (cout);

    ss.begin_array ();

    for (const shared_ptr<configuration>& c: cfgs)
    {
      ss.begin_object ();

      ss.member ("id", *c->id);
      ss.member ("path", c->path.string ());

      if (c->name)
        ss.member ("name", *c->name);

      ss.member ("type", c->type);

      if (c->default_)
        ss.member ("default", c->default_);

      if (c->forward)
        ss.member ("forward", c->forward);

      if (c->auto_sync)
        ss.member ("auto_sync", c->auto_sync);

      if (!c->packages.empty ())
      {
        ss.member_name ("packages", false /* check */);

        ss.begin_array ();

        for (const package_state& s: c->packages)
        {
          ss.begin_object ();
          ss.member ("name", s.name.string (), false /* check */);
          ss.end_object ();
        }

        ss.end_array ();
      }

      ss.end_object ();
    }

    ss.end_array ();

    cout << endl;
  }

  static int
  cmd_config_list (const cmd_config_options& o, cli::scanner&)
  {
    tracer trace ("config_list");

    dir_path prj (find_project (o));
    database db (open (prj, trace));

    transaction t (db.begin ());

    configurations cfgs;
    if (o.config_specified ()    || // Note: handling --all|-a ourselves.
        o.config_id_specified () ||
        o.config_name_specified ())
    {
      cfgs = find_configurations (o,
                                  prj,
                                  t,
                                  false /* fallback_default */,
                                  false /* validate         */).first;
    }
    else
    {
      using query = bdep::query<configuration>;

      // We want to show the default configuration first, then sort them
      // by name, and then by path.
      //
      for (auto c: pointer_result (
             db.query<configuration> ("ORDER BY" +
                                      query::default_ + "DESC," +
                                      query::name     + "IS NULL," +
                                      query::name     + "," +
                                      query::path)))
        cfgs.push_back (move (c));
    }

    t.commit ();

    switch (o.stdout_format ())
    {
    case stdout_format::lines:
      {
        cmd_config_list_lines (cfgs);
        break;
      }
    case stdout_format::json:
      {
        cmd_config_list_json (cfgs);
        break;
      }
    }

    return 0;
  }

  static int
  cmd_config_move (cmd_config_options& o, cli::scanner& args)
  {
    tracer trace ("config_move");

    if (!args.more ())
      fail << "configuration directory argument expected";

    dir_path prj (find_project (o));

    // Similar story to config-add.
    //
    dir_path path;
    optional<dir_path> rel_path;
    {
      const char* a (args.next ());
      try
      {
        path = dir_path (a);

        if (!exists (path))
          fail << "configuration directory " << path << " does not exist";

        normalize (path, "configuration directory");
      }
      catch (const invalid_path&)
      {
        fail << "invalid configuration directory '" << a << "'";
      }

      rel_path = path.try_relative (prj);
    }

    database db (open (prj, trace));

    session ses;
    transaction t (db.begin ());

    configurations cfgs (
      find_configurations (o,
                           prj,
                           t,
                           false /* fallback_default */,
                           false /* validate         */).first);

    if (cfgs.size () > 1)
      fail << "multiple configurations specified for config move";

    const shared_ptr<configuration>& c (cfgs.front ());

    // Check if there is already a configuration with this path.
    //
    using query = bdep::query<configuration>;

    if (auto p = db.query_one<configuration> (query::path == path.string ()))
    {
      // Note that this also covers the case where p == c.
      //
      fail << "configuration " << *p << " already uses directory " << path;
    }

    // Save the old path for diagnostics.
    //
    // @@ We should probably also adjust explicit and implicit links in the
    //    respective bpkg configurations, if/when bpkg provides the required
    //    API.
    //
    c->path.swap (path);
    c->relative_path = move (rel_path);

    db.update (c);
    t.commit ();

    if (verb)
    {
      // Restore the original path so that we can use print_configuration().
      //
      path.swap (c->path);

      {
        diag_record dr (text);
        dr << "moved configuration ";
        print_configuration (dr, c, false /* flags */);
        dr << " to " << path;
      }

      info << "explicit sync command is required for changes to take effect";
    }

    return 0;
  }

  static int
  cmd_config_rename (cmd_config_options& o, cli::scanner& args)
  {
    tracer trace ("config_rename");

    // Let's be nice and allow specifying the new name as noth <name> and
    // @<name>.
    //
    string name;
    if (args.more ())
    {
      name = args.next ();

      if (name.empty ())
        fail << "empty configuration name specified";
    }
    else
    {
      vector<pair<string, size_t>>& ns (o.config_name ());
      size_t n (ns.size ());

      if (n > 1 || (n == 1 && (o.config_specified () ||
                               o.config_id_specified ())))
      {
        name = move (ns.back ().first);
        ns.pop_back ();
      }
      else
        fail << "configuration name argument expected";
    }

    dir_path prj (find_project (o));
    database db (open (prj, trace));

    session ses;
    transaction t (db.begin ());

    configurations cfgs (
      find_configurations (o,
                           prj,
                           t,
                           false /* fallback_default */,
                           false /* validate         */).first);

    if (cfgs.size () > 1)
      fail << "multiple configurations specified for config rename";

    const shared_ptr<configuration>& c (cfgs.front ());

    // Check if this name is already taken.
    //
    using query = bdep::query<configuration>;

    if (auto p = db.query_one<configuration> (query::name == name))
    {
      // Note that this also covers the case where p == c.
      //
      fail << "configuration " << p->path << " is already called " << name;
    }

    // Save the old name for diagnostics.
    //
    if (c->name)
      name.swap (*c->name);
    else
      c->name = move (name);

    db.update (c);
    t.commit ();

    if (verb)
    {
      // Restore the original name so that we can use print_configuration().
      //
      name.swap (*c->name);

      diag_record dr (text);
      dr << "renamed configuration ";
      print_configuration (dr, c, false /* flags */);
      dr << " to @" << name;
    }

    return 0;
  }

  static int
  cmd_config_remove (const cmd_config_options& o, cli::scanner&)
  {
    tracer trace ("config_remove");

    dir_path prj (find_project (o));
    database db (open (prj, trace));

    transaction t (db.begin ());

    configurations cfgs (
      find_configurations (o,
                           prj,
                           t,
                           false /* fallback_default */,
                           false /* validate         */).first);

    for (const shared_ptr<configuration>& c: cfgs)
    {
      if (!c->packages.empty ())
      {
        bool e (exists (c->path));

        fail << "configuration " << *c << " contains initialized packages" <<
          info << "use deinit " << (e ? "" : "--force ") << "command to "
             << "deinitialize packages" <<
          info << "use status command to list initialized packages";
      }

      db.erase (c);
    }

    t.commit ();

    if (verb)
    {
      for (const shared_ptr<configuration>& c: cfgs)
      {
        diag_record dr (text);
        dr << "removed configuration ";
        print_configuration (dr, c, false /* flags */);
      }
    }

    return 0;
  }

  static int
  cmd_config_set (const cmd_config_options& o, cli::scanner&)
  {
    tracer trace ("config_set");

    // Note that these have been validate by cmd_config_validate_add().
    //
    optional<bool> d, f, s;
    if (o.default_  () || o.no_default   ()) d = o.default_  ();
    if (o.forward   () || o.no_forward   ()) f = o.forward   ();
    if (o.auto_sync () || o.no_auto_sync ()) s = o.auto_sync ();

    if (!d && !f && !s)
      fail << "nothing to set";

    dir_path prj (find_project (o));
    database db (open (prj, trace));

    session ses;
    transaction t (db.begin ());

    configurations cfgs (
      find_configurations (o,
                           prj,
                           t,
                           false /* fallback_default */,
                           false /* validate         */).first);

    for (const shared_ptr<configuration>& c: cfgs)
    {
      using query = bdep::query<configuration>;

      // Verify that there is no other default or forwarded configuration with
      // the same package as us.
      //
      auto verify = [&c, &db] (const query& q, const char* what)
      {
        for (const shared_ptr<configuration>& o:
               pointer_result (db.query<configuration> (q)))
        {
          auto i (find_first_of (
                    o->packages.begin (), o->packages.end (),
                    c->packages.begin (), c->packages.end (),
                    [] (const package_state& x, const package_state& y)
                    {
                      return x.name == y.name;
                    }));

          if (i != o->packages.end ())
            fail << "configuration " << *o << " is also " << what << " and "
                 << "also has package " << i->name << " initialized" <<
              info << "while updating configuration " << *c;
        }
      };

      if (d)
      {
        if (*d && !c->default_)
        {
          if (auto p = db.query_one<configuration> (query::default_ &&
                                                    query::type == c->type))
            fail << "configuration " << *p << " of type " << p->type
                 << " is already the default" <<
              info << "while updating configuration " << *c;

          verify (query::default_, "default");
        }

        c->default_ = *d;
      }

      if (f)
      {
        if (*f && !c->forward)
          verify (query::forward, "forwarded");

        c->forward = *f;
      }

      if (s)
        c->auto_sync = *s;

      db.update (c);
    }

    t.commit ();

    if (verb)
    {
      for (const shared_ptr<configuration>& c: cfgs)
      {
        diag_record dr (text);
        dr << "updated configuration ";
        print_configuration (dr, c);
      }

      info << "explicit sync command is required for changes to take effect";
    }

    return 0;
  }

  int
  cmd_config (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 subcommand";

      if (o.type_specified () && !c.create ())
        fail << "--type|--config-type is not valid for this subcommand";

      if (o.existing () && !c.create ())
        fail << "--existing|-e is not valid for this subcommand";

      if (o.wipe () && !c.create ())
        fail << "--wipe is not valid for this subcommand";
    }

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

    // Dispatch to subcommand function.
    //
    if (c.add    ()) return cmd_config_add    (o, scan);
    if (c.create ()) return cmd_config_create (o, scan);
    if (c.link   ()) return cmd_config_link   (o, scan);
    if (c.unlink ()) return cmd_config_unlink (o, scan);
    if (c.list   ()) return cmd_config_list   (o, scan);
    if (c.move   ()) return cmd_config_move   (o, scan);
    if (c.rename ()) return cmd_config_rename (o, scan);
    if (c.remove ()) return cmd_config_remove (o, scan);
    if (c.set    ()) return cmd_config_set    (o, scan);

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

  default_options_files
  options_files (const char*,
                 const cmd_config_options& o,
                 const strings& args)
  {
    // NOTE: remember to update the documentation if changing anything here.

    // bdep.options
    // bdep-config.options
    // bdep-config-<subcmd>.options
    //
    // Note that bdep-config-add.options is loaded for both the add and
    // create subcommands since create is-a add.
    //
    // Also note that these files are loaded by other commands (bdep-init
    // and bdep-new).

    default_options_files r {
      {path ("bdep.options"), path ("bdep-config.options")},
      find_project (o)};

    // Validate the subcommand.
    //
    {
      cli::vector_scanner scan (args);
      parse_command<cmd_config_subcommands> (scan,
                                             "config subcommand",
                                             "bdep help config");
    }

    const string& sc (args[0]);

    auto add = [&r] (const string& n)
    {
      r.files.push_back (path ("bdep-" + n + ".options"));
    };

    if (sc == "create")
      add ("config-add");

    add ("config-" + sc);

    return r;
  }

  cmd_config_options
  merge_options (const default_options<cmd_config_options>& defs,
                 const cmd_config_options& cmd)
  {
    // NOTE: remember to update the documentation if changing anything here.

    return merge_default_options (
      defs,
      cmd,
      [] (const default_options_entry<cmd_config_options>& e,
          const cmd_config_options&)
      {
        const cmd_config_options& o (e.options);

        auto forbid = [&e] (const char* opt, bool specified)
        {
          if (specified)
            fail (e.file) << opt << " in default options file";
        };

        forbid ("--directory|-d", o.directory_specified ());
        forbid ("--wipe",         o.wipe ()); // Dangerous.
      });
  }
}