}
-static void
-error (string m)
+class KDMCLIError : public std::runtime_error
{
- cerr << program_name << ": " << m << "\n";
- exit (EXIT_FAILURE);
-}
+public:
+ KDMCLIError (std::string message)
+ : std::runtime_error (String::compose("%1: %2", program_name, message).c_str())
+ {}
+};
static boost::posix_time::ptime
string const unit (unit_buf);
if (N == 0) {
- cerr << "Could not understand duration \"" << d << "\"\n";
- exit (EXIT_FAILURE);
+ throw KDMCLIError (String::compose("could not understand duration \"%1\"", d));
}
if (unit == "year" || unit == "years") {
return boost::posix_time::time_duration (N, 0, 0, 0);
}
- cerr << "Could not understand duration \"" << d << "\"\n";
- exit (EXIT_FAILURE);
+ throw KDMCLIError (String::compose("could not understand duration \"%1\"", d));
}
}
if (i == cinemas.end ()) {
- cerr << program_name << ": could not find cinema \"" << cinema_name << "\"\n";
- exit (EXIT_FAILURE);
+ throw KDMCLIError (String::compose("could not find cinema \"%1\"", cinema_name));
}
return *i;
cout << "Read film " << film->name () << "\n";
}
} catch (std::exception& e) {
- cerr << program_name << ": error reading film `" << film_dir.string() << "' (" << e.what() << ")\n";
- exit (EXIT_FAILURE);
+ throw KDMCLIError (String::compose("error reading film \"%1\" (%2)", film_dir.string(), e.what()));
}
/* XXX: allow specification of this */
vector<CPLSummary> cpls = film->cpls ();
if (cpls.empty ()) {
- error ("no CPLs found in film");
+ throw KDMCLIError ("no CPLs found in film");
} else if (cpls.size() > 1) {
- error ("more than one CPL found in film");
+ throw KDMCLIError ("more than one CPL found in film");
}
auto cpl = cpls.front().cpl_file;
send_emails ({kdms}, container_name_format, filename_format, film->dcp_name());
}
} catch (FileError& e) {
- cerr << program_name << ": " << e.what() << " (" << e.file().string() << ")\n";
- exit (EXIT_FAILURE);
- } catch (KDMError& e) {
- cerr << program_name << ": " << e.what() << "\n";
- exit (EXIT_FAILURE);
- } catch (runtime_error& e) {
- cerr << program_name << ": " << e.what() << "\n";
- exit (EXIT_FAILURE);
+ throw KDMCLIError (String::compose("%1 (%2)", e.what(), e.file().string()));
}
}
}
}
- return optional<dcp::EncryptedKDM>();
+ return {};
}
/* Signer for new KDM */
auto signer = Config::instance()->signer_chain ();
if (!signer->valid ()) {
- error ("signing certificate chain is invalid.");
+ throw KDMCLIError ("signing certificate chain is invalid.");
}
/* Make a new empty KDM and add the keys from the DKDM to it */
send_emails ({kdms}, container_name_format, filename_format, dkdm.annotation_text().get_value_or(""));
}
} catch (FileError& e) {
- cerr << program_name << ": " << e.what() << " (" << e.file().string() << ")\n";
- exit (EXIT_FAILURE);
- } catch (KDMError& e) {
- cerr << program_name << ": " << e.what() << "\n";
- exit (EXIT_FAILURE);
- } catch (NetworkError& e) {
- cerr << program_name << ": " << e.what() << "\n";
- exit (EXIT_FAILURE);
+ throw KDMCLIError (String::compose("%1 (%2)", e.what(), e.file().string()));
}
}
}
-int
+optional<string>
kdm_cli (int argc, char* argv[])
+try
{
boost::filesystem::path output = ".";
auto container_name_format = Config::instance()->kdm_container_name_format();
} else if (string(optarg) == "dci-specific") {
formulation = dcp::Formulation::DCI_SPECIFIC;
} else {
- error ("unrecognised KDM formulation " + string (optarg));
+ throw KDMCLIError ("unrecognised KDM formulation " + string (optarg));
}
break;
case 'p':
}
if (!duration_string && !valid_to) {
- error ("you must specify a --valid-duration or --valid-to");
+ throw KDMCLIError ("you must specify a --valid-duration or --valid-to");
}
if (!valid_from) {
- error ("you must specify --valid-from");
+ throw KDMCLIError ("you must specify --valid-from");
}
if (optind >= argc) {
if (screens.empty()) {
if (!cinema_name) {
- error ("you must specify either a cinema or one or more screens using certificate files");
+ throw KDMCLIError ("you must specify either a cinema or one or more screens using certificate files");
}
screens = find_cinema (*cinema_name)->screens ();
}
if (!dkdm) {
- error ("could not find film or CPL ID corresponding to " + thing);
+ throw KDMCLIError ("could not find film or CPL ID corresponding to " + thing);
}
from_dkdm (
);
}
- return 0;
+ return {};
+} catch (std::exception& e) {
+ return string(e.what());
}
+