Permalink
Browse files

issue 86 allow sync folder to be specified, move Config class and add…

… basic unit test
  • Loading branch information...
justin
justin committed Jul 29, 2012
1 parent e0383a8 commit 034891a6103282dfe175eba10e882303c2d211d5
View
@@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
-#include "Config.hh"
+#include "util/Config.hh"
#include "drive/Drive.hh"
@@ -46,6 +46,9 @@
const std::string client_id = "22314510474.apps.googleusercontent.com" ;
const std::string client_secret = "bl4ufi89h-9MkFlypcI7R785" ;
+const std::string defaultRootFolder = ".";
+const std::string defaultConfigFileName = ".grive";
+const char *configFileEnvironmentVariable = "GR_CONFIG";
using namespace gr ;
@@ -66,8 +69,8 @@ int Main( int argc, char **argv )
{
InitGCrypt() ;
- Config config ;
-
+ std::string rootFolder = defaultRootFolder;
+
std::auto_ptr<log::CompositeLog> comp_log(new log::CompositeLog) ;
LogBase* console_log = comp_log->Add( std::auto_ptr<LogBase>( new log::DefaultLog ) ) ;
@@ -81,6 +84,7 @@ int Main( int argc, char **argv )
( "help,h", "Produce help message" )
( "version,v", "Display Grive version" )
( "auth,a", "Request authorization token" )
+ ( "path,p", po::value<std::string>(), "Path to sync")
( "verbose,V", "Verbose mode. Enable more messages than normal.")
( "log-xml", "Log more HTTP responses as XML for debugging.")
( "new-rev", "Create new revisions in server for updated files.")
@@ -101,27 +105,9 @@ int Main( int argc, char **argv )
std::cout << desc << std::endl ;
return 0 ;
}
- if ( vm.count( "auth" ) )
- {
- std::cout
- << "-----------------------\n"
- << "Please go to this URL and get an authentication code:\n\n"
- << OAuth2::MakeAuthURL( client_id )
- << std::endl ;
-
- std::cout
- << "\n-----------------------\n"
- << "Please input the authentication code here: " << std::endl ;
- std::string code ;
- std::cin >> code ;
-
- OAuth2 token( client_id, client_secret ) ;
- token.Auth( code ) ;
-
- // save to config
- config.Get().Add( "refresh_token", Json( token.RefreshToken() ) ) ;
- config.Save() ;
- }
+
+ boost::shared_ptr<Config> config ;
+
if ( vm.count( "log" ) )
{
std::auto_ptr<LogBase> file_log(new log::DefaultLog( vm["log"].as<std::string>() )) ;
@@ -138,12 +124,6 @@ int Main( int argc, char **argv )
comp_log->Add( file_log ) ;
}
- if ( vm.count( "version" ) )
- {
- std::cout
- << "grive version " << VERSION << ' ' << __DATE__ << ' ' << __TIME__ << std::endl ;
- return 0 ;
- }
if ( vm.count( "verbose" ) )
{
console_log->Enable( log::verbose ) ;
@@ -157,6 +137,52 @@ int Main( int argc, char **argv )
console_log->Enable( log::verbose ) ;
console_log->Enable( log::debug ) ;
}
+
+ // config file will be (in order of preference)
+ // value specified in environment string
+ // value specified in defaultConfigFileName in path from commandline --path
+ // value specified in defaultConfigFileName in current directory
+ const char *envConfigFileName = ::getenv( configFileEnvironmentVariable ) ;
+ if (envConfigFileName) {
+ config.reset(new Config(envConfigFileName));
+
+ } else if ( vm.count( "path" ) ) {
+ rootFolder = vm["path"].as<std::string>();
+ config.reset(new Config( fs::path(rootFolder) / fs::path(defaultConfigFileName) ));
+
+ } else {
+ config.reset(new Config( defaultConfigFileName) );
+ }
+
+ Log( "config file name %1%", config->ConfigFile().string(), log::verbose );
+
+ if ( vm.count( "auth" ) )
+ {
+ std::cout
+ << "-----------------------\n"
+ << "Please go to this URL and get an authentication code:\n\n"
+ << OAuth2::MakeAuthURL( client_id )
+ << std::endl ;
+
+ std::cout
+ << "\n-----------------------\n"
+ << "Please input the authentication code here: " << std::endl ;
+ std::string code ;
+ std::cin >> code ;
+
+ OAuth2 token( client_id, client_secret ) ;
+ token.Auth( code ) ;
+
+ // save to config
+ config->Get().Add( "refresh_token", Json( token.RefreshToken() ) ) ;
+ config->Save() ;
+ }
+ if ( vm.count( "version" ) )
+ {
+ std::cout
+ << "grive version " << VERSION << ' ' << __DATE__ << ' ' << __TIME__ << std::endl ;
+ return 0 ;
+ }
if ( vm.count( "force" ) )
{
options.Add( "force", Json(true) ) ;
@@ -167,7 +193,7 @@ int Main( int argc, char **argv )
std::string refresh_token ;
try
{
- refresh_token = config.Get()["refresh_token"].Str() ;
+ refresh_token = config->Get()["refresh_token"].Str() ;
}
catch ( Exception& e )
{
@@ -181,7 +207,8 @@ int Main( int argc, char **argv )
OAuth2 token( refresh_token, client_id, client_secret ) ;
AuthAgent agent( token, std::auto_ptr<http::Agent>( new http::CurlAgent ) ) ;
- Drive drive( &agent, options ) ;
+
+ Drive drive( &agent, options, rootFolder ) ;
drive.DetectChanges() ;
if ( vm.count( "dry-run" ) == 0 )
@@ -192,7 +219,7 @@ int Main( int argc, char **argv )
else
drive.DryRun() ;
- config.Save() ;
+ config->Save() ;
Log( "Finished!", log::info ) ;
return 0 ;
}
@@ -51,9 +51,10 @@ namespace
const std::string state_file = ".grive_state" ;
}
-Drive::Drive( http::Agent *http, const Json& options ) :
- m_http ( http ),
- m_state ( state_file, options ),
+Drive::Drive( http::Agent *http, const Json& options, const std::string &rootFolder) :
+ m_http( http ),
+ m_state( fs::path(rootFolder), fs::path(rootFolder) / fs::path(state_file), options ),
+ m_rootFolder(rootFolder),
m_options ( options )
{
assert( m_http != 0 ) ;
@@ -127,7 +128,7 @@ void Drive::SyncFolders( )
void Drive::DetectChanges()
{
Log( "Reading local directories", log::info ) ;
- m_state.FromLocal( "." ) ;
+ m_state.FromLocal( m_rootFolder ) ;
long prev_stamp = m_state.ChangeStamp() ;
Trace( "previous change stamp is %1%", prev_stamp ) ;
@@ -40,7 +40,7 @@ class Entry ;
class Drive
{
public :
- Drive( http::Agent *http, const Json& options ) ;
+ Drive( http::Agent *http, const Json& options, const std::string &rootFolder ) ;
void DetectChanges() ;
void Update() ;
@@ -60,7 +60,7 @@ private :
http::Agent *m_http ;
std::string m_resume_link ;
State m_state ;
-
+ fs::path m_rootFolder;
Json m_options ;
} ;
@@ -58,8 +58,8 @@ const std::string xml_meta =
/// default constructor creates the root folder
-Resource::Resource() :
- m_name ( "." ),
+Resource::Resource(const fs::path& rootFolder) :
+ m_name ( rootFolder.string() ),
m_kind ( "folder" ),
m_id ( "folder:root" ),
m_href ( root_href ),
@@ -51,7 +51,7 @@ public :
typedef Children::const_iterator iterator ;
public :
- Resource() ;
+ Resource(const fs::path& rootFolder) ;
Resource( const std::string& name, const std::string& kind ) ;
// default copy ctor & op= are fine
@@ -31,8 +31,8 @@ namespace gr {
using namespace details ;
-ResourceTree::ResourceTree( ) :
- m_root( new Resource )
+ResourceTree::ResourceTree( const fs::path& rootFolder ) :
+ m_root(new Resource(rootFolder))
{
m_set.insert( m_root ) ;
}
@@ -64,7 +64,7 @@ public :
typedef details::Set::iterator iterator ;
public :
- ResourceTree( ) ;
+ ResourceTree( const fs::path& rootFolder ) ;
ResourceTree( const ResourceTree& fs ) ;
~ResourceTree( ) ;
@@ -32,8 +32,9 @@
namespace gr {
-State::State( const fs::path& filename, const Json& options ) :
- m_cstamp( -1 )
+State::State( const fs::path& rootFolder, const fs::path& filename, const Json& options ) :
+ m_cstamp( -1 ),
+ m_res(rootFolder)
{
Read( filename ) ;
@@ -43,7 +43,7 @@ public :
typedef ResourceTree::iterator iterator ;
public :
- explicit State( const fs::path& filename, const Json& options ) ;
+ explicit State( const fs::path& rootFolder, const fs::path& filename, const Json& options ) ;
~State() ;
void FromLocal( const fs::path& p ) ;
@@ -21,26 +21,28 @@
#include "util/StdioFile.hh"
+#include <iostream>
#include <iterator>
-namespace gr {
+using namespace gr;
-const std::string& Config::Filename()
+Config::Config(const fs::path& configFile)
+ : m_configFile(configFile)
+ , m_cfg( Read() )
{
- static const char *env_cfg = ::getenv( "GR_CONFIG" ) ;
- static const std::string filename = (env_cfg != 0) ? env_cfg : ".grive" ;
-
- return filename ;
+ if (configFile.empty()) {
+ throw Error() << expt::ErrMsg("Config cannot be initalised with an empty string.");
+ }
}
-Config::Config() :
- m_cfg( Read( Filename() ) )
+const fs::path& Config::ConfigFile() const
{
+ return m_configFile ;
}
void Config::Save( )
{
- StdioFile file( Filename(), 0600 ) ;
+ StdioFile file( m_configFile.string(), 0600 ) ;
m_cfg.Write( file ) ;
}
@@ -49,16 +51,15 @@ Json& Config::Get()
return m_cfg ;
}
-Json Config::Read( const std::string& filename )
+Json Config::Read()
{
try
{
- return Json::ParseFile( filename ) ;
+ return Json::ParseFile( m_configFile.string() ) ;
}
catch ( Exception& e )
{
return Json() ;
}
}
-} // end of namespace
@@ -19,7 +19,8 @@
#pragma once
-#include "util/Exception.hh"
+#include "Exception.hh"
+#include "FileSystem.hh"
#include "protocol/Json.hh"
namespace gr {
@@ -30,17 +31,17 @@ public :
struct Error : virtual Exception {} ;
typedef boost::error_info<struct FileTag, std::string> File ;
- static const std::string& Filename() ;
-
- Config() ;
+ Config(const fs::path& configFile) ;
Json& Get() ;
void Save() ;
+ const fs::path &ConfigFile() const;
private :
- Json Read( const std::string& filename ) ;
+ Json Read() ;
private :
+ const fs::path m_configFile;
Json m_cfg ;
} ;
@@ -27,6 +27,7 @@
#include "drive/StateTest.hh"
#include "util/DateTimeTest.hh"
#include "util/FunctionTest.hh"
+#include "util/ConfigTest.hh"
#include "util/SignalHandlerTest.hh"
#include "xml/NodeTest.hh"
@@ -43,6 +44,7 @@ int main( int argc, char **argv )
runner.addTest( ResourceTreeTest::suite( ) ) ;
runner.addTest( DateTimeTest::suite( ) ) ;
runner.addTest( FunctionTest::suite( ) ) ;
+ runner.addTest( ConfigTest::suite( ) ) ;
runner.addTest( SignalHandlerTest::suite( ) ) ;
runner.addTest( NodeTest::suite( ) ) ;
runner.run();
@@ -38,9 +38,10 @@ ResourceTest::ResourceTest( )
void ResourceTest::TestRootPath()
{
- Resource root ;
+ std::string rootFolder = "/home/usr/grive/grive";
+ Resource root(rootFolder) ;
CPPUNIT_ASSERT( root.IsRoot() ) ;
- GRUT_ASSERT_EQUAL( root.Path(), fs::path( "." ) ) ;
+ GRUT_ASSERT_EQUAL( root.Path(), fs::path( rootFolder ) ) ;
}
void ResourceTest::TestNormal( )
@@ -60,7 +61,7 @@ void ResourceTest::TestNormal( )
Entry remote( entry ) ;
subject.FromRemote( remote, DateTime() ) ;
- GRUT_ASSERT_EQUAL( subject.StateStr(), "local_changed" ) ;
+ GRUT_ASSERT_EQUAL( "local_changed", subject.StateStr() ) ;
}
Oops, something went wrong.

0 comments on commit 034891a

Please sign in to comment.