From 8de3e6d91c0ac7c9bcc85468619d0fa024ba4008 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Thu, 3 Mar 2016 15:07:46 -0500 Subject: [PATCH] init: Creates database - Creates ~/.timewarrior if necessary. - Creates ~/.timewarrior/timewarrior.cfg if necessary. - Loads ~/.timewarrior/timewarrior.cfg. --- src/init.cpp | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index ceaacfb1..8def0727 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -32,6 +32,8 @@ //#include //#include #include +#include +#include #include #include #include @@ -69,9 +71,29 @@ void initializeData (Configuration& configuration, Database& database) } // TODO If dbLocation does not exist, ask whether it should be created. - // TODO IF dbLocation exists, but is not readable/writable/executable, error. + bool shinyNewDatabase = false; + if (! dbLocation.exists () /* && + confirm ("Create Timewarrior database in " + dbLocation._data + "?")*/) + { + dbLocation.create (0700); + std::cout << "# Created missing database in " << dbLocation._data << "\n"; + shinyNewDatabase = true; + } - // TODO Load the configuration data. + // If dbLocation exists, but is not readable/writable/executable, error. + if (dbLocation.exists () && + (! dbLocation.readable () || + ! dbLocation.writable () || + ! dbLocation.executable ())) + { + throw format ("Database is not readable at '{1}'", dbLocation._data); + } + + // Load the configuration data. + File configFile (dbLocation); + configFile += "timewarrior.cfg"; + configFile.create (0600); + configuration.load (configFile._data); // This value is not written out to disk, as there would be no point. Having // located the config file, the 'db' location is already known. This is just @@ -79,6 +101,9 @@ void initializeData (Configuration& configuration, Database& database) configuration.set ("db", dbLocation._data); std::cout << "# rc.db=" << configuration.get ("db") << "\n"; + // Perhaps some later code would like to know this is a new db. + configuration.set ("shiny", 1); + // TODO Init database (no data read). std::cout << "# Configuration\n";