timewarrior/src/init.cpp
2016-03-28 22:35:24 -04:00

233 lines
8.2 KiB
C++

////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2015 - 2016, Paul Beckingham, Federico Hernandez.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// http://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////
#include <cmake.h>
#include <Database.h>
#include <Rules.h>
#include <Extensions.h>
#include <Log.h>
//#include <Grammar.h>
//#include <LR0.h>
#include <shared.h>
#include <format.h>
#include <FS.h>
#include <commands.h>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
////////////////////////////////////////////////////////////////////////////////
bool lightweightVersionCheck (const std::vector <std::string>& args)
{
if (args.size () == 2 && args[1] == "--version")
{
std::cout << VERSION << "\n";
return true;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
void initializeDataAndRules (
Database& database,
Rules& rules,
Log& log)
{
// The $TIMEWARRIORDB environment variable overrides the default value of
// ~/.timewarrior‥
Directory dbLocation;
char* override = getenv ("TIMEWARRIORDB");
if (override)
{
log.write ("debug", std::string ("TIMEWARRIORDB ") + override);
dbLocation = Directory (override);
}
else
{
dbLocation = Directory ("~/.timewarrior");
log.write ("debug", std::string ("Using default DB location ") + dbLocation._data);
}
// If dbLocation does not exist, ask whether it should be created.
bool shinyNewDatabase = false;
if (! dbLocation.exists () &&
confirm ("Create new database in " + dbLocation._data + "?"))
{
dbLocation.create (0700);
log.write ("debug", std::string ("Created missing database in ") + dbLocation._data);
shinyNewDatabase = true;
}
// Create extensions subdirectory if necessary.
Directory extensions (dbLocation);
extensions += "extensions";
if (! extensions.exists ())
extensions.create (0700);
// Create data subdirectory if necessary.
Directory data (dbLocation);
data += "data";
if (! data.exists ())
data.create (0700);
// 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);
}
// TODO Load rule grammar.
/*
File ruleFile ("./rule.grammar");
Grammar ruleGrammar;
ruleGrammar.debug (debug);
ruleGrammar.loadFromFile (ruleFile);
// Instantiate the parser.
LR0 ruleParser;
ruleParser.debug (debug);
ruleParser.initialize (ruleGrammar);
*/
// Load the configuration data.
File configFile (dbLocation);
configFile += "timewarrior.cfg";
configFile.create (0600);
rules.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
// for subsequent internal use.
rules.set ("temp.db", dbLocation._data);
log.write ("debug", std::string (" rc.db=") + rules.get ("temp.db"));
// Perhaps some subsequent code would like to know this is a new db and
// possibly a first run.
rules.set ("temp.shiny", (shinyNewDatabase ? 1 : 0));
// Initialize the database (no data read), but files are enumerated.
database.initialize (data._data);
log.write ("debug", database.dump ());
// TODO Give the log file a temp fake name. To be removed.
log.file (dbLocation._data + "/timewarrior.log");
log.write ("debug", rules.dump ());
}
////////////////////////////////////////////////////////////////////////////////
void initializeExtensions (
Rules& rules,
Extensions& extensions,
Log& log)
{
Directory extDir (rules.get ("temp.db"));
extDir += "extensions";
extensions.initialize (extDir._data);
}
////////////////////////////////////////////////////////////////////////////////
int dispatchCommand (
const std::vector <std::string>& args,
Database& database,
Rules& rules,
Extensions& extensions,
Log& log)
{
int status {0};
// Log the command line.
std::string combined;
for (auto& arg : args)
{
if (arg.find (' ') != std::string::npos)
combined += "'" + arg + "' ";
else
combined += arg + " ";
}
log.write ("command", combined);
if (args.size () > 1)
{
std::vector <std::string> allCommands =
{
"clear", "config", "continue", "define", "diagnostics", "export",
"extensions", "gaps", "help", "import", "log", "report", "start", "stop",
"tags", "track", "undo"
};
std::vector <std::string> matches;
autoComplete (args[1], allCommands, matches);
if (matches.size () == 1)
{
// These signatures are æxpected to be all different, therefore no
// command to fn mapping.
if (matches[0] == allCommands[0]) status = CmdClear ( );
else if (matches[0] == allCommands[1]) status = CmdConfig ( );
else if (matches[0] == allCommands[2]) status = CmdContinue ( rules, database, log);
else if (matches[0] == allCommands[3]) status = CmdDefine (args, rules, database, log);
else if (matches[0] == allCommands[4]) status = CmdDiagnostics ( rules, database, extensions, log);
else if (matches[0] == allCommands[5]) status = CmdExport ( database );
else if (matches[0] == allCommands[6]) status = CmdExtensions ( rules, extensions );
else if (matches[0] == allCommands[7]) status = CmdGaps ( );
else if (matches[0] == allCommands[8]) status = CmdHelp (args, log);
else if (matches[0] == allCommands[9]) status = CmdImport ( );
else if (matches[0] == allCommands[10]) status = CmdLog (args, log);
else if (matches[0] == allCommands[11]) status = CmdReport (args, rules, database, extensions, log);
else if (matches[0] == allCommands[12]) status = CmdStart (args, database, log);
else if (matches[0] == allCommands[13]) status = CmdStop (args, database, log);
else if (matches[0] == allCommands[14]) status = CmdTags ( rules, database, log);
else if (matches[0] == allCommands[15]) status = CmdTrack ( );
else if (matches[0] == allCommands[16]) status = CmdUndo ( );
}
else if (matches.size () == 0)
{
CmdHelpUsage ();
}
else
{
std::cout << "timew: '" << args[1] << "' is not a command. See 'timew help'.\n"
<< "\n"
<< "Did you mean one of these?\n";
for (const auto& command : matches)
std::cout << " " << command << "\n";
}
}
else if (args.size () == 1)
{
status = CmdDefault (rules, database);
}
return status;
}
////////////////////////////////////////////////////////////////////////////////