added config file class, but not very well encapsulated

pull/40/head
Matchman Green 2012-05-15 23:07:03 +08:00
parent 3dea8343de
commit 0b63d9bd3c
5 changed files with 129 additions and 57 deletions

View File

@ -7,8 +7,12 @@ include_directories(
add_definitions( -DVERSION="${GRIVE_VERSION}" ) add_definitions( -DVERSION="${GRIVE_VERSION}" )
file (GLOB GRIVE_EXE_SRC
${grive_SOURCE_DIR}/src/*.cc
)
add_executable( grive_executable add_executable( grive_executable
src/main.cc ${GRIVE_EXE_SRC}
) )
target_link_libraries( grive_executable target_link_libraries( grive_executable

70
grive/src/Config.cc Normal file
View File

@ -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

44
grive/src/Config.hh Normal file
View File

@ -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

View File

@ -17,6 +17,8 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/ */
#include "Config.hh"
#include "drive/Drive.hh" #include "drive/Drive.hh"
#include "protocol/OAuth2.hh" #include "protocol/OAuth2.hh"
@ -31,68 +33,17 @@
#include <cassert> #include <cassert>
#include <cstdlib> #include <cstdlib>
#include <fstream>
#include <iostream> #include <iostream>
#include <unistd.h> #include <unistd.h>
#include <iterator>
#include <exception>
#include <stdexcept>
const std::string client_id = "22314510474.apps.googleusercontent.com" ; const std::string client_id = "22314510474.apps.googleusercontent.com" ;
const std::string client_secret = "bl4ufi89h-9MkFlypcI7R785" ; 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 ) int main( int argc, char **argv )
{ {
using namespace gr ; using namespace gr ;
Json config = ReadConfig() ; Config config ;
DefaultLog nofile_log ; DefaultLog nofile_log ;
LogBase::Inst( &nofile_log ) ; LogBase::Inst( &nofile_log ) ;
@ -120,9 +71,9 @@ int main( int argc, char **argv )
token.Auth( code ) ; token.Auth( code ) ;
// save to config // save to config
config.Add( "refresh_token", Json( token.RefreshToken() ) ) ; config.Get().Add( "refresh_token", Json( token.RefreshToken() ) ) ;
assert( config["refresh_token"].As<std::string>() == token.RefreshToken() ) ; assert( config.Get()["refresh_token"].Str() == token.RefreshToken() ) ;
SaveConfig( config ) ; config.Save( ) ;
break ; break ;
} }
@ -147,7 +98,7 @@ int main( int argc, char **argv )
std::string refresh_token ; std::string refresh_token ;
try try
{ {
refresh_token = config["refresh_token"].As<std::string>() ; refresh_token = config.Get()["refresh_token"].Str() ;
} }
catch ( const std::runtime_error& error ) catch ( const std::runtime_error& error )
{ {

View File

@ -70,6 +70,9 @@ namespace expt
// generic error message // generic error message
typedef boost::error_info<struct MsgTag, std::string> ErrMsg ; typedef boost::error_info<struct MsgTag, std::string> ErrMsg ;
// nested exception
typedef boost::error_info<struct ExceptionTag, Exception> Nested ;
} }
} // end of namespace } // end of namespace