mirror of https://github.com/vitalif/grive2
added config file class, but not very well encapsulated
parent
3dea8343de
commit
0b63d9bd3c
|
@ -7,8 +7,12 @@ include_directories(
|
|||
|
||||
add_definitions( -DVERSION="${GRIVE_VERSION}" )
|
||||
|
||||
file (GLOB GRIVE_EXE_SRC
|
||||
${grive_SOURCE_DIR}/src/*.cc
|
||||
)
|
||||
|
||||
add_executable( grive_executable
|
||||
src/main.cc
|
||||
${GRIVE_EXE_SRC}
|
||||
)
|
||||
|
||||
target_link_libraries( grive_executable
|
||||
|
|
|
@ -0,0 +1,70 @@
|
|||
/*
|
||||
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.
|
||||
|
||||
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 <fstream>
|
||||
#include <iterator>
|
||||
|
||||
namespace gr {
|
||||
|
||||
const std::string& Config::Filename()
|
||||
{
|
||||
static const char *env_cfg = ::getenv( "GR_CONFIG" ) ;
|
||||
static const std::string filename =
|
||||
(env_cfg != 0) ? env_cfg : std::string( ::getenv( "HOME") ) + "/.grive" ;
|
||||
|
||||
return filename ;
|
||||
}
|
||||
|
||||
Config::Config()
|
||||
{
|
||||
std::ifstream ifile( Filename().c_str() ) ;
|
||||
if ( ifile )
|
||||
{
|
||||
try
|
||||
{
|
||||
std::string cfg_str(
|
||||
(std::istreambuf_iterator<char>( ifile )),
|
||||
(std::istreambuf_iterator<char>()) ) ;
|
||||
|
||||
m_cfg = Json::Parse( cfg_str ) ;
|
||||
}
|
||||
catch ( Exception& e )
|
||||
{
|
||||
throw Error()
|
||||
<< File( Filename() )
|
||||
<< expt::ErrMsg("Cannot open config file ")
|
||||
<< expt::Nested(e) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Config::Save( )
|
||||
{
|
||||
std::ofstream ofile( Filename().c_str() ) ;
|
||||
ofile << m_cfg ;
|
||||
}
|
||||
|
||||
Json& Config::Get()
|
||||
{
|
||||
return m_cfg ;
|
||||
}
|
||||
|
||||
} // end of namespace
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
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.
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "util/Exception.hh"
|
||||
#include "protocol/Json.hh"
|
||||
|
||||
namespace gr {
|
||||
|
||||
class Config
|
||||
{
|
||||
public :
|
||||
struct Error : virtual Exception {} ;
|
||||
typedef boost::error_info<struct FileTag, std::string> File ;
|
||||
|
||||
static const std::string& Filename() ;
|
||||
|
||||
Config() ;
|
||||
|
||||
Json& Get() ;
|
||||
void Save() ;
|
||||
|
||||
private :
|
||||
Json m_cfg ;
|
||||
} ;
|
||||
|
||||
} // end of namespace
|
|
@ -17,6 +17,8 @@
|
|||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include "Config.hh"
|
||||
|
||||
#include "drive/Drive.hh"
|
||||
|
||||
#include "protocol/OAuth2.hh"
|
||||
|
@ -31,68 +33,17 @@
|
|||
|
||||
#include <cassert>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <unistd.h>
|
||||
#include <iterator>
|
||||
|
||||
#include <exception>
|
||||
#include <stdexcept>
|
||||
|
||||
const std::string client_id = "22314510474.apps.googleusercontent.com" ;
|
||||
const std::string client_secret = "bl4ufi89h-9MkFlypcI7R785" ;
|
||||
|
||||
namespace gr
|
||||
{
|
||||
class ConfigError : public std::runtime_error
|
||||
{
|
||||
public :
|
||||
ConfigError( const std::string& msg ) : runtime_error( msg )
|
||||
{
|
||||
}
|
||||
} ;
|
||||
|
||||
const std::string& ConfigFilename()
|
||||
{
|
||||
static const char *env_cfg = ::getenv( "GR_CONFIG" ) ;
|
||||
static const std::string filename =
|
||||
(env_cfg != 0) ? env_cfg : std::string( ::getenv( "HOME") ) + "/.grive" ;
|
||||
|
||||
return filename ;
|
||||
}
|
||||
|
||||
Json ReadConfig()
|
||||
{
|
||||
std::ifstream ifile( ConfigFilename().c_str() ) ;
|
||||
if ( !ifile )
|
||||
return Json() ;
|
||||
|
||||
try
|
||||
{
|
||||
std::string cfg_str(
|
||||
(std::istreambuf_iterator<char>( ifile )),
|
||||
(std::istreambuf_iterator<char>()) ) ;
|
||||
|
||||
return Json::Parse( cfg_str ) ;
|
||||
}
|
||||
catch ( std::runtime_error& e )
|
||||
{
|
||||
throw ConfigError( std::string("Cannot open config file ") + e.what() ) ;
|
||||
}
|
||||
}
|
||||
|
||||
void SaveConfig( const Json& config )
|
||||
{
|
||||
std::ofstream ofile( ConfigFilename().c_str() ) ;
|
||||
ofile << config ;
|
||||
}
|
||||
}
|
||||
|
||||
int main( int argc, char **argv )
|
||||
{
|
||||
using namespace gr ;
|
||||
|
||||
Json config = ReadConfig() ;
|
||||
Config config ;
|
||||
|
||||
DefaultLog nofile_log ;
|
||||
LogBase::Inst( &nofile_log ) ;
|
||||
|
@ -120,9 +71,9 @@ int main( int argc, char **argv )
|
|||
token.Auth( code ) ;
|
||||
|
||||
// save to config
|
||||
config.Add( "refresh_token", Json( token.RefreshToken() ) ) ;
|
||||
assert( config["refresh_token"].As<std::string>() == token.RefreshToken() ) ;
|
||||
SaveConfig( config ) ;
|
||||
config.Get().Add( "refresh_token", Json( token.RefreshToken() ) ) ;
|
||||
assert( config.Get()["refresh_token"].Str() == token.RefreshToken() ) ;
|
||||
config.Save( ) ;
|
||||
|
||||
break ;
|
||||
}
|
||||
|
@ -147,7 +98,7 @@ int main( int argc, char **argv )
|
|||
std::string refresh_token ;
|
||||
try
|
||||
{
|
||||
refresh_token = config["refresh_token"].As<std::string>() ;
|
||||
refresh_token = config.Get()["refresh_token"].Str() ;
|
||||
}
|
||||
catch ( const std::runtime_error& error )
|
||||
{
|
||||
|
|
|
@ -70,6 +70,9 @@ namespace expt
|
|||
|
||||
// generic error message
|
||||
typedef boost::error_info<struct MsgTag, std::string> ErrMsg ;
|
||||
|
||||
// nested exception
|
||||
typedef boost::error_info<struct ExceptionTag, Exception> Nested ;
|
||||
}
|
||||
|
||||
} // end of namespace
|
||||
|
|
Loading…
Reference in New Issue