Add add-dkdm command to add DKDMs to DCP-o-matic's internal list from the CLI.
authorCarl Hetherington <cth@carlh.net>
Fri, 22 Mar 2024 19:39:38 +0000 (20:39 +0100)
committerCarl Hetherington <cth@carlh.net>
Mon, 1 Apr 2024 22:52:27 +0000 (00:52 +0200)
src/lib/kdm_cli.cc
test/kdm_cli_test.cc

index f63fd2a550d30275c1a3ed16e468ddf79efb12d2..651ba8e2616dc364478055cb73ab0d206f247a79 100644 (file)
@@ -62,6 +62,7 @@ help (std::function<void (string)> out)
        out ("create          create KDMs; default if no other command is specified");
        out ("list-cinemas    list known cinemas from DCP-o-matic settings");
        out ("list-dkdm-cpls  list CPLs for which DCP-o-matic has DKDMs");
+       out ("add-dkdm        add DKDM to DCP-o-matic's list");
        out ("  -h, --help                               show this help");
        out ("  -o, --output <path>                      output file or directory");
        out ("  -K, --filename-format <format>           filename format for KDMs");
@@ -594,7 +595,8 @@ try
        vector<string> commands = {
                "create",
                "list-cinemas",
-               "list-dkdm-cpls"
+               "list-dkdm-cpls",
+               "add-dkdm"
        };
 
        if (optind < argc - 1) {
@@ -603,7 +605,7 @@ try
        } else if (optind < argc) {
                /* Look for a valid command, hoping that it's not the name of the KDM / CPL / whatever */
                if (std::find(commands.begin(), commands.end(), argv[optind]) != commands.end()) {
-                       command = argv[optind];
+                       command = argv[optind++];
                }
        }
 
@@ -638,6 +640,17 @@ try
                return {};
        }
 
+       if (optind >= argc) {
+               throw KDMCLIError("no film, CPL ID or DKDM specified");
+       }
+
+       if (command == "add-dkdm") {
+               auto dkdms = Config::instance()->dkdms();
+               dkdms->add(make_shared<DKDM>(dcp::EncryptedKDM(dcp::file_to_string(argv[optind]))));
+               Config::instance()->write_config();
+               return {};
+       }
+
        if (!duration_string && !valid_to) {
                throw KDMCLIError ("you must specify a --valid-duration or --valid-to");
        }
@@ -646,10 +659,6 @@ try
                throw KDMCLIError ("you must specify --valid-from");
        }
 
-       if (optind >= argc) {
-               throw KDMCLIError ("no film, CPL ID or DKDM specified");
-       }
-
        if (screens.empty()) {
                if (!cinema_name) {
                        throw KDMCLIError ("you must specify either a cinema or one or more screens using certificate files");
index 057d7be9398e84f4be5509c0318a751b8c88fd73..303f2b0d4d470b8d01cc35ef930e7f608f755a8d 100644 (file)
@@ -23,6 +23,7 @@
 #include "lib/config.h"
 #include "lib/content_factory.h"
 #include "lib/cross.h"
+#include "lib/dkdm_wrapper.h"
 #include "lib/film.h"
 #include "lib/kdm_cli.h"
 #include "lib/screen.h"
@@ -34,6 +35,7 @@
 #include <iostream>
 
 
+using std::dynamic_pointer_cast;
 using std::string;
 using std::vector;
 using boost::optional;
@@ -331,3 +333,29 @@ BOOST_AUTO_TEST_CASE(kdm_cli_time)
        BOOST_CHECK(boost::filesystem::exists(kdm_filename));
 }
 
+
+BOOST_AUTO_TEST_CASE(kdm_cli_add_dkdm)
+{
+       ConfigRestorer cr;
+
+       setup_test_config();
+
+       BOOST_CHECK_EQUAL(Config::instance()->dkdms()->children().size(), 0U);
+
+       vector<string> args = {
+               "kdm_cli",
+               "add-dkdm",
+               "test/data/dkdm.xml"
+       };
+
+       vector<string> output;
+       auto error = run(args, output);
+       BOOST_CHECK(!error);
+
+       auto dkdms = Config::instance()->dkdms()->children();
+       BOOST_CHECK_EQUAL(dkdms.size(), 1U);
+       auto dkdm = dynamic_pointer_cast<DKDM>(dkdms.front());
+       BOOST_CHECK(dkdm);
+       BOOST_CHECK_EQUAL(dkdm->dkdm().as_xml(), dcp::file_to_string("test/data/dkdm.xml"));
+}
+