mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-08-28 04:27:20 +02:00
Feature #462: url support
- curl enhancements (push/pull multiple files) - now supports push/pull to/from filesystem
This commit is contained in:
parent
042d7b40de
commit
1a16b3ae6b
7 changed files with 144 additions and 77 deletions
|
@ -116,4 +116,19 @@ int Transport::execute()
|
|||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
bool Transport::is_directory(const std::string& path)
|
||||
{
|
||||
return path[path.length()-1] == '/';
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
bool Transport::is_filelist(const std::string& path)
|
||||
{
|
||||
return (path.find ("*") != std::string::npos)
|
||||
|| (path.find ("?") != std::string::npos)
|
||||
|| (path.find ("{") != std::string::npos);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
|
|
@ -41,6 +41,9 @@ public:
|
|||
virtual void send (const std::string&) = 0;
|
||||
virtual void recv (std::string) = 0;
|
||||
|
||||
static bool is_directory(const std::string&);
|
||||
static bool is_filelist(const std::string&);
|
||||
|
||||
protected:
|
||||
std::string executable;
|
||||
std::vector<std::string> arguments;
|
||||
|
|
|
@ -26,6 +26,8 @@
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "TransportCurl.h"
|
||||
#include "text.h"
|
||||
#include "util.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
TransportCurl::TransportCurl(const Uri& uri) : Transport(uri)
|
||||
|
@ -40,17 +42,42 @@ void TransportCurl::send(const std::string& source)
|
|||
throw std::string ("Hostname is empty");
|
||||
}
|
||||
|
||||
// Wildcards arent supported
|
||||
if ( (source.find ("*") != std::string::npos)
|
||||
|| (source.find ("?") != std::string::npos) )
|
||||
{
|
||||
throw std::string ("Failed to use curl with wildcards!");
|
||||
}
|
||||
if (is_filelist(source))
|
||||
{
|
||||
std::string::size_type pos;
|
||||
pos = source.find("{");
|
||||
|
||||
if (pos == std::string::npos)
|
||||
throw std::string ("Curl does not support wildcards!");
|
||||
|
||||
if (!uri.is_directory())
|
||||
throw std::string ("'" + uri.path + "' is not a directory!");
|
||||
|
||||
std::string toSplit;
|
||||
std::string suffix;
|
||||
std::string prefix;
|
||||
std::vector<std::string> splitted;
|
||||
|
||||
prefix = source.substr (0, pos);
|
||||
toSplit = source.substr (pos+1);
|
||||
|
||||
pos = toSplit.find ("}");
|
||||
suffix = toSplit.substr (pos+1);
|
||||
split (splitted, toSplit.substr(0, pos), ',');
|
||||
|
||||
foreach (file, splitted)
|
||||
{
|
||||
arguments.push_back ("-T");
|
||||
arguments.push_back (prefix + *file + suffix);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
arguments.push_back ("-T");
|
||||
arguments.push_back (source);
|
||||
}
|
||||
|
||||
// cmd line is: curl -T source protocol://host:port/path
|
||||
arguments.push_back ("-T");
|
||||
arguments.push_back (source);
|
||||
|
||||
if (uri.port != "")
|
||||
{
|
||||
arguments.push_back (uri.protocol + "://" + uri.host + ":" + uri.port + "/" + uri.path);
|
||||
|
@ -71,12 +98,36 @@ void TransportCurl::recv(std::string target)
|
|||
throw std::string ("Hostname is empty");
|
||||
}
|
||||
|
||||
// Wildcards arent supported
|
||||
if ( (uri.path.find ("*") != std::string::npos)
|
||||
|| (uri.path.find ("?") != std::string::npos) )
|
||||
{
|
||||
throw std::string ("Failed to use curl with wildcards!");
|
||||
}
|
||||
if (is_filelist(uri.path))
|
||||
{
|
||||
std::string::size_type pos;
|
||||
pos = uri.path.find("{");
|
||||
|
||||
if (pos == std::string::npos)
|
||||
throw std::string ("Curl does not support wildcards!");
|
||||
|
||||
if (!is_directory(target))
|
||||
throw std::string ("'" + target + "' is not a directory!");
|
||||
|
||||
std::string toSplit;
|
||||
std::string suffix;
|
||||
std::string prefix = target;
|
||||
std::vector<std::string> splitted;
|
||||
toSplit = uri.path.substr (pos+1);
|
||||
pos = toSplit.find ("}");
|
||||
suffix = toSplit.substr (pos+1);
|
||||
split (splitted, toSplit.substr(0, pos), ',');
|
||||
|
||||
target = "";
|
||||
foreach (file, splitted)
|
||||
{
|
||||
target += " -o " + prefix + *file + suffix;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
target = "-o " + target;
|
||||
}
|
||||
|
||||
// cmd line is: curl protocol://host:port/path/to/source/file -o path/to/target/file
|
||||
if (uri.port != "")
|
||||
|
@ -88,7 +139,6 @@ void TransportCurl::recv(std::string target)
|
|||
arguments.push_back (uri.protocol + "://" + uri.host + "/" + uri.path);
|
||||
}
|
||||
|
||||
arguments.push_back ("-o");
|
||||
arguments.push_back (target);
|
||||
|
||||
if (execute())
|
||||
|
|
|
@ -42,18 +42,8 @@ void TransportRSYNC::send(const std::string& source)
|
|||
|
||||
// Is there more than one file to transfer?
|
||||
// Then path has to end with a '/'
|
||||
if ( (source.find ("*") != std::string::npos)
|
||||
|| (source.find ("?") != std::string::npos)
|
||||
|| (source.find (" ") != std::string::npos) )
|
||||
{
|
||||
std::string::size_type pos;
|
||||
|
||||
pos = uri.path.find_last_of ("/");
|
||||
if (pos != uri.path.length()-1)
|
||||
{
|
||||
uri.path = uri.path.substr (0, pos+1);
|
||||
}
|
||||
}
|
||||
if (is_filelist(source) && !uri.is_directory())
|
||||
throw std::string ("'" + uri.path + "' is not a directory!");
|
||||
|
||||
// cmd line is: rsync [--port=PORT] source [user@]host::path
|
||||
if (uri.port != "")
|
||||
|
@ -85,16 +75,8 @@ void TransportRSYNC::recv(std::string target)
|
|||
|
||||
// Is there more than one file to transfer?
|
||||
// Then target has to end with a '/'
|
||||
if ( (uri.path.find ("*") != std::string::npos)
|
||||
|| (uri.path.find ("?") != std::string::npos) )
|
||||
{
|
||||
std::string::size_type pos;
|
||||
pos = target.find_last_of ("/");
|
||||
if (pos != target.length()-1)
|
||||
{
|
||||
target = target.substr( 0, pos+1);
|
||||
}
|
||||
}
|
||||
if (is_filelist(uri.path) && !is_directory(target))
|
||||
throw std::string ("'" + target + "' is not a directory!");
|
||||
|
||||
// cmd line is: rsync [--port=PORT] [user@]host::path target
|
||||
if (uri.port != "")
|
||||
|
|
|
@ -42,18 +42,8 @@ void TransportSSH::send(const std::string& source)
|
|||
|
||||
// Is there more than one file to transfer?
|
||||
// Then path has to end with a '/'
|
||||
if ( (source.find ("*") != std::string::npos)
|
||||
|| (source.find ("?") != std::string::npos)
|
||||
|| (source.find (" ") != std::string::npos) )
|
||||
{
|
||||
std::string::size_type pos;
|
||||
|
||||
pos = uri.path.find_last_of ("/");
|
||||
if (pos != uri.path.length()-1)
|
||||
{
|
||||
uri.path = uri.path.substr (0, pos+1);
|
||||
}
|
||||
}
|
||||
if (is_filelist(source) && !uri.is_directory())
|
||||
throw std::string ("'" + uri.path + "' is not a directory!");
|
||||
|
||||
// cmd line is: scp [-p port] [user@]host:path
|
||||
if (uri.port != "")
|
||||
|
@ -86,16 +76,8 @@ void TransportSSH::recv(std::string target)
|
|||
|
||||
// Is there more than one file to transfer?
|
||||
// Then target has to end with a '/'
|
||||
if ( (uri.path.find ("*") != std::string::npos)
|
||||
|| (uri.path.find ("?") != std::string::npos) )
|
||||
{
|
||||
std::string::size_type pos;
|
||||
pos = target.find_last_of ("/");
|
||||
if (pos != target.length()-1)
|
||||
{
|
||||
target = target.substr( 0, pos+1);
|
||||
}
|
||||
}
|
||||
if (is_filelist(uri.path) && !is_directory(target))
|
||||
throw std::string ("'" + target + "' is not a directory!");
|
||||
|
||||
// cmd line is: scp [-p port] [user@]host:path
|
||||
if (uri.port != "")
|
||||
|
|
|
@ -104,7 +104,7 @@ std::string Uri::parent () const
|
|||
{
|
||||
std::string::size_type slash = path.rfind ('/');
|
||||
if (slash != std::string::npos)
|
||||
return path.substr (0, slash);
|
||||
return path.substr (0, slash+1);
|
||||
}
|
||||
|
||||
return "";
|
||||
|
|
|
@ -659,17 +659,26 @@ void handlePush (std::string& outs)
|
|||
Transport* transport;
|
||||
if ((transport = Transport::getTransport (uri)) != NULL )
|
||||
{
|
||||
// TODO specify data files
|
||||
transport->send (location.data + "/*.data");
|
||||
transport->send (location.data + "/{pending,undo,completed}.data");
|
||||
delete transport;
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO copy files
|
||||
//std::ifstream ifile (location.data + "/undo.data", std::ios_base::binary);
|
||||
//std::ofstream ofile (location.data + "/undo.data", std::ios_base::binary);
|
||||
// copy files locally
|
||||
if (!uri.is_directory())
|
||||
throw std::string ("'" + uri.path + "' is not a directory!");
|
||||
|
||||
throw std::string ("Push to local targets is not (yet) supported.");
|
||||
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();
|
||||
}
|
||||
|
||||
context.hooks.trigger ("post-push-command");
|
||||
|
@ -693,7 +702,8 @@ void handlePull (std::string& outs)
|
|||
{
|
||||
Directory location (context.config.get ("data.location"));
|
||||
|
||||
uri.append ("*.data");
|
||||
if (!uri.append ("{pending,undo,completed}.data"))
|
||||
throw std::string ("'" + uri.path + "' is not a directory!");
|
||||
|
||||
Transport* transport;
|
||||
if ((transport = Transport::getTransport (uri)) != NULL )
|
||||
|
@ -703,8 +713,33 @@ void handlePull (std::string& outs)
|
|||
}
|
||||
else
|
||||
{
|
||||
// TODO copy files
|
||||
throw std::string ("Pull failed");
|
||||
// copy files locally
|
||||
|
||||
// remove {pending,undo,completed}.data
|
||||
uri.path = uri.parent();
|
||||
|
||||
Path path1 (uri.path + "undo.data");
|
||||
Path path2 (uri.path + "pending.data");
|
||||
Path path3 (uri.path + "completed.data");
|
||||
|
||||
if (path1.exists() && path2.exists() && path3.exists())
|
||||
{
|
||||
std::ofstream ofile1 ((location.data + "/undo.data").c_str(), std::ios_base::binary);
|
||||
std::ifstream ifile1 (path1.data.c_str() , std::ios_base::binary);
|
||||
ofile1 << ifile1.rdbuf();
|
||||
|
||||
std::ofstream ofile2 ((location.data + "/pending.data").c_str(), std::ios_base::binary);
|
||||
std::ifstream ifile2 (path2.data.c_str() , std::ios_base::binary);
|
||||
ofile2 << ifile2.rdbuf();
|
||||
|
||||
std::ofstream ofile3 ((location.data + "/completed.data").c_str(), std::ios_base::binary);
|
||||
std::ifstream ifile3 (path3.data.c_str() , std::ios_base::binary);
|
||||
ofile3 << ifile3.rdbuf();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw std::string ("At least one of the database files in '" + uri.path + "' is not present.");
|
||||
}
|
||||
}
|
||||
|
||||
context.hooks.trigger ("post-pull-command");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue