grive2/grive/src/main.cc

138 lines
3.0 KiB
C++
Raw Normal View History

2012-04-25 20:13:17 +04:00
/*
grive: an GPL program to sync a local directory with Google Drive
Copyright (C) 2012 Wan Wai Ho
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 version 2
of the License.
2012-04-25 20:13:17 +04:00
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 "Config.hh"
#include "drive/Drive.hh"
2012-05-13 11:27:58 +04:00
#include "protocol/OAuth2.hh"
#include "protocol/Json.hh"
2012-04-25 20:13:17 +04:00
2012-05-13 11:27:58 +04:00
#include "bfd/Backtrace.hh"
#include "util/Exception.hh"
2012-05-13 12:10:18 +04:00
#include "util/Log.hh"
#include "util/DefaultLog.hh"
2012-05-13 11:27:58 +04:00
#include <boost/exception/all.hpp>
2012-04-26 05:20:41 +04:00
#include <cassert>
2012-04-25 20:13:17 +04:00
#include <cstdlib>
#include <iostream>
2012-04-29 19:40:13 +04:00
#include <unistd.h>
const std::string client_id = "22314510474.apps.googleusercontent.com" ;
const std::string client_secret = "bl4ufi89h-9MkFlypcI7R785" ;
2012-04-25 20:13:17 +04:00
int main( int argc, char **argv )
{
using namespace gr ;
2012-04-26 05:20:41 +04:00
Config config ;
2012-04-25 20:13:17 +04:00
DefaultLog nofile_log ;
LogBase::Inst( &nofile_log ) ;
2012-04-25 20:13:17 +04:00
int c ;
while ((c = getopt(argc, argv, "al:vV")) != -1)
2012-04-25 20:13:17 +04:00
{
switch ( c )
{
case 'a' :
{
2012-04-26 05:20:41 +04:00
std::cout
<< "-----------------------\n"
<< "Please go to this URL and get an authentication code:\n\n"
<< OAuth2::MakeAuthURL( client_id )
2012-04-26 05:20:41 +04:00
<< std::endl ;
std::cout
<< "\n-----------------------\n"
<< "Please input the authentication code here: " << std::endl ;
2012-04-26 05:20:41 +04:00
std::string code ;
std::cin >> code ;
OAuth2 token( client_id, client_secret ) ;
2012-04-26 05:20:41 +04:00
token.Auth( code ) ;
2012-04-25 20:13:17 +04:00
2012-04-26 05:20:41 +04:00
// save to config
config.Get().Add( "refresh_token", Json( token.RefreshToken() ) ) ;
assert( config.Get()["refresh_token"].Str() == token.RefreshToken() ) ;
config.Save() ;
2012-04-26 05:20:41 +04:00
break ;
2012-04-25 20:13:17 +04:00
}
2012-05-06 18:40:58 +04:00
case 'l' :
{
static DefaultLog log( optarg ) ;
LogBase::Inst( &log ) ;
break ;
}
2012-05-06 18:40:58 +04:00
case 'v' :
{
std::cout
<< "grive version " VERSION " " __DATE__ " " __TIME__ << std::endl ;
return 0 ;
}
case 'V' :
{
LogBase::Inst()->Enable( log::verbose ) ;
break ;
}
2012-04-25 20:13:17 +04:00
}
}
2012-04-25 20:13:17 +04:00
std::string refresh_token ;
try
{
refresh_token = config.Get()["refresh_token"].Str() ;
}
2012-05-19 05:03:34 +04:00
catch ( Exception& e )
{
2012-05-19 05:03:34 +04:00
Trace( "cannot read config: %1%", boost::diagnostic_information(e) ) ;
2012-05-13 12:45:27 +04:00
Log(
2012-05-13 12:10:18 +04:00
"Please run grive with the \"-a\" option if this is the "
"first time you're accessing your Google Drive!",
2012-05-13 12:45:27 +04:00
log::critical ) ;
2012-05-13 12:10:18 +04:00
return -1;
}
2012-04-25 20:13:17 +04:00
2012-05-13 11:27:58 +04:00
try
{
OAuth2 token( refresh_token, client_id, client_secret ) ;
2012-05-16 20:52:17 +04:00
Drive drive( token ) ;
2012-05-13 21:07:23 +04:00
drive.Update() ;
drive.SaveState() ;
config.Save() ;
2012-05-13 11:27:58 +04:00
}
catch ( gr::Exception& e )
{
2012-05-13 12:45:27 +04:00
Log( "exception: %1%", boost::diagnostic_information(e), log::critical ) ;
2012-05-13 12:10:18 +04:00
return -1 ;
2012-05-13 11:27:58 +04:00
}
2012-04-25 20:13:17 +04:00
return 0 ;
}