From af90a14cb5a0a27545dcfa43a1569eead9210a02 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Mon, 30 May 2011 14:37:39 -0400 Subject: [PATCH] Commands - push - Migrated handlePush to CmdPush. --- src/Cmd.cpp | 5 -- src/Context.cpp | 1 - src/command.cpp | 57 ++------------------ src/commands/CMakeLists.txt | 1 + src/commands/CmdHelp.cpp | 4 -- src/commands/CmdPush.cpp | 102 ++++++++++++++++++++++++++++++++++++ src/commands/CmdPush.h | 42 +++++++++++++++ src/commands/Command.cpp | 2 + src/main.h | 1 - 9 files changed, 150 insertions(+), 65 deletions(-) create mode 100644 src/commands/CmdPush.cpp create mode 100644 src/commands/CmdPush.h diff --git a/src/Cmd.cpp b/src/Cmd.cpp index 3e9675df8..b31d4d479 100644 --- a/src/Cmd.cpp +++ b/src/Cmd.cpp @@ -130,7 +130,6 @@ void Cmd::load () if (commands.size () == 0) { commands.push_back ("merge"); - commands.push_back ("push"); // Now load the custom reports. std::vector all; @@ -187,10 +186,6 @@ void Cmd::allCommands (std::vector & all) const // Commands that do not directly modify the data files. bool Cmd::isReadOnlyCommand () { - if (command == "push" || - validCustom (command)) - return true; - return false; } diff --git a/src/Context.cpp b/src/Context.cpp index 81f76c3b9..7519d842b 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -251,7 +251,6 @@ int Context::dispatch (std::string &out) // TODO Chain-of-command pattern dispatch. if (cmd.command == "merge") { tdb.gc (); handleMerge (out); } - else if (cmd.command == "push") { handlePush (out); } else if (cmd.command == "" && sequence.size ()) { rc = handleModify (out); } diff --git a/src/command.cpp b/src/command.cpp index a15018d9b..f116519bc 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -95,9 +95,10 @@ void handleMerge (std::string&) if ( ((sAutopush == "ask") && (confirm ("Would you like to push the merged changes to \'" + uri.data + "\'?")) ) || (bAutopush) ) { + context.task.set ("description", uri.data); + std::string out; - context.task.set ("description", uri.data); - handlePush (out); + context.commands["push"]->execute ("", out); } } else @@ -106,58 +107,6 @@ void handleMerge (std::string&) "'merge.default.uri' entry in your .taskrc file."); } -//////////////////////////////////////////////////////////////////////////////// -// Transfers the local data (from rc.location.data) to the remote path. Because -// this is potentially on another machine, no checking can be performed. -void handlePush (std::string&) -{ - std::string file = trim (context.task.get ("description")); - - Uri uri (file, "push"); - uri.parse (); - - if (uri.data.length ()) - { - Directory location (context.config.get ("data.location")); - - Transport* transport; - if ((transport = Transport::getTransport (uri)) != NULL ) - { - transport->send (location.data + "/{pending,undo,completed}.data"); - delete transport; - } - else - { - // Verify that files are not being copied from rc.data.location to the - // same place. - if (Directory (uri.path) == Directory (context.config.get ("data.location"))) - throw std::string ("Cannot push files when the source and destination are the same."); - - // copy files locally - if (! Path (uri.data).is_directory ()) - throw std::string ("The uri '") + uri.path + "' is not a local directory."; - - std::ifstream ifile1 ((location.data + "/undo.data").c_str(), std::ios_base::binary); - std::ofstream ofile1 ((uri.path + "/undo.data").c_str(), std::ios_base::binary); - ofile1 << ifile1.rdbuf(); - - std::ifstream ifile2 ((location.data + "/pending.data").c_str(), std::ios_base::binary); - std::ofstream ofile2 ((uri.path + "/pending.data").c_str(), std::ios_base::binary); - ofile2 << ifile2.rdbuf(); - - std::ifstream ifile3 ((location.data + "/completed.data").c_str(), std::ios_base::binary); - std::ofstream ofile3 ((uri.path + "/completed.data").c_str(), std::ios_base::binary); - ofile3 << ifile3.rdbuf(); - } - - std::cout << "Local tasks transferred to " << uri.data << "\n"; - } - else - throw std::string ("No uri was specified for the push. Either specify " - "the uri of a remote .task directory, or create a " - "'push.default.uri' entry in your .taskrc file."); -} - //////////////////////////////////////////////////////////////////////////////// int handleModify (std::string& outs) { diff --git a/src/commands/CMakeLists.txt b/src/commands/CMakeLists.txt index a470f7e2f..61eb46b8d 100644 --- a/src/commands/CMakeLists.txt +++ b/src/commands/CMakeLists.txt @@ -34,6 +34,7 @@ set (commands_SRCS Command.cpp Command.h CmdPrepend.cpp CmdPrepend.h CmdProjects.cpp CmdProjects.h CmdPull.cpp CmdPull.h + CmdPush.cpp CmdPush.h CmdQuery.cpp CmdQuery.h CmdReports.cpp CmdReports.h CmdShell.cpp CmdShell.h diff --git a/src/commands/CmdHelp.cpp b/src/commands/CmdHelp.cpp index 1f4451b06..481ad14bb 100644 --- a/src/commands/CmdHelp.cpp +++ b/src/commands/CmdHelp.cpp @@ -106,10 +106,6 @@ int CmdHelp::execute (const std::string&, std::string& output) row = view.addRow (); view.set (row, 1, "task merge URL"); view.set (row, 2, "Merges the specified undo.data file with the local data files."); - - row = view.addRow (); - view.set (row, 1, "task push URL"); - view.set (row, 2, "Pushes the local *.data files to the URL."); */ output = "\n" diff --git a/src/commands/CmdPush.cpp b/src/commands/CmdPush.cpp new file mode 100644 index 000000000..6a2e98dd0 --- /dev/null +++ b/src/commands/CmdPush.cpp @@ -0,0 +1,102 @@ +//////////////////////////////////////////////////////////////////////////////// +// taskwarrior - a command line task list manager. +// +// Copyright 2006 - 2011, Paul Beckingham, Federico Hernandez. +// All rights reserved. +// +// This program is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free Software +// Foundation; either version 2 of the License, or (at your option) any later +// version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +// details. +// +// You should have received a copy of the GNU General Public License along with +// this program; if not, write to the +// +// Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, +// Boston, MA +// 02110-1301 +// USA +// +//////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include +#include +#include +#include +#include + +extern Context context; + +//////////////////////////////////////////////////////////////////////////////// +CmdPush::CmdPush () +{ + _keyword = "push"; + _usage = "task push URL"; + _description = "Pushes the local *.data files to the URL."; + _read_only = true; + _displays_id = false; +} + +//////////////////////////////////////////////////////////////////////////////// +// Transfers the local data (from rc.location.data) to the remote path. Because +// this is potentially on another machine, no checking can be performed. +int CmdPush::execute (const std::string&, std::string& output) +{ + std::string file = trim (context.task.get ("description")); + + Uri uri (file, "push"); + uri.parse (); + + if (uri.data.length ()) + { + Directory location (context.config.get ("data.location")); + + Transport* transport; + if ((transport = Transport::getTransport (uri)) != NULL ) + { + transport->send (location.data + "/{pending,undo,completed}.data"); + delete transport; + } + else + { + // Verify that files are not being copied from rc.data.location to the + // same place. + if (Directory (uri.path) == Directory (context.config.get ("data.location"))) + throw std::string ("Cannot push files when the source and destination are the same."); + + // copy files locally + if (! Path (uri.data).is_directory ()) + throw std::string ("The uri '") + uri.path + "' is not a local directory."; + + std::ifstream ifile1 ((location.data + "/undo.data").c_str(), std::ios_base::binary); + std::ofstream ofile1 ((uri.path + "/undo.data").c_str(), std::ios_base::binary); + ofile1 << ifile1.rdbuf(); + + std::ifstream ifile2 ((location.data + "/pending.data").c_str(), std::ios_base::binary); + std::ofstream ofile2 ((uri.path + "/pending.data").c_str(), std::ios_base::binary); + ofile2 << ifile2.rdbuf(); + + std::ifstream ifile3 ((location.data + "/completed.data").c_str(), std::ios_base::binary); + std::ofstream ofile3 ((uri.path + "/completed.data").c_str(), std::ios_base::binary); + ofile3 << ifile3.rdbuf(); + } + + output += "Local tasks transferred to " + uri.data + "\n"; + } + else + throw std::string ("No uri was specified for the push. Either specify " + "the uri of a remote .task directory, or create a " + "'push.default.uri' entry in your .taskrc file."); + + return 0; +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/commands/CmdPush.h b/src/commands/CmdPush.h new file mode 100644 index 000000000..eb54f2728 --- /dev/null +++ b/src/commands/CmdPush.h @@ -0,0 +1,42 @@ +//////////////////////////////////////////////////////////////////////////////// +// taskwarrior - a command line task list manager. +// +// Copyright 2006 - 2011, Paul Beckingham, Federico Hernandez. +// All rights reserved. +// +// This program is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free Software +// Foundation; either version 2 of the License, or (at your option) any later +// version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +// details. +// +// You should have received a copy of the GNU General Public License along with +// this program; if not, write to the +// +// Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, +// Boston, MA +// 02110-1301 +// USA +// +//////////////////////////////////////////////////////////////////////////////// +#ifndef INCLUDED_CMDPUSH +#define INCLUDED_CMDPUSH +#define L10N // Localization complete. + +#include +#include + +class CmdPush : public Command +{ +public: + CmdPush (); + int execute (const std::string&, std::string&); +}; + +#endif +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/commands/Command.cpp b/src/commands/Command.cpp index 321549895..1b0401e23 100644 --- a/src/commands/Command.cpp +++ b/src/commands/Command.cpp @@ -56,6 +56,7 @@ #include #include #include +#include #include #include #include @@ -116,6 +117,7 @@ void Command::factory (std::map & all) c = new CmdPrepend (); all[c->keyword ()] = c; c = new CmdProjects (); all[c->keyword ()] = c; c = new CmdPull (); all[c->keyword ()] = c; + c = new CmdPush (); all[c->keyword ()] = c; c = new CmdQuery (); all[c->keyword ()] = c; c = new CmdReports (); all[c->keyword ()] = c; c = new CmdShell (); all[c->keyword ()] = c; diff --git a/src/main.h b/src/main.h index 4fd8dd0a7..14493e7cd 100644 --- a/src/main.h +++ b/src/main.h @@ -52,7 +52,6 @@ bool nag (Task&); // command.cpp int handleModify (std::string&); void handleMerge (std::string&); -void handlePush (std::string&); int deltaAppend (Task&); int deltaPrepend (Task&); int deltaDescription (Task&);