better fix for the missing config file problem

pull/40/head
Matchman Green 2012-04-29 11:17:15 +08:00
parent 0d4eede114
commit 13001ed087
1 changed files with 20 additions and 5 deletions

View File

@ -35,6 +35,14 @@ const std::string client_secret = "bl4ufi89h-9MkFlypcI7R785" ;
namespace gr namespace gr
{ {
class ConfigError : public std::runtime_error
{
public :
ConfigError( const std::string& msg ) : runtime_error( msg )
{
}
} ;
const std::string& ConfigFilename() const std::string& ConfigFilename()
{ {
static const char *env_cfg = ::getenv( "GR_CONFIG" ) ; static const char *env_cfg = ::getenv( "GR_CONFIG" ) ;
@ -47,8 +55,10 @@ namespace gr
Json ReadConfig() Json ReadConfig()
{ {
std::ifstream ifile( ConfigFilename().c_str() ) ; std::ifstream ifile( ConfigFilename().c_str() ) ;
if ( !ifile )
return Json() ;
if ( ifile ) try
{ {
std::string cfg_str( std::string cfg_str(
(std::istreambuf_iterator<char>( ifile )), (std::istreambuf_iterator<char>( ifile )),
@ -56,8 +66,10 @@ namespace gr
return Json::Parse( cfg_str ) ; return Json::Parse( cfg_str ) ;
} }
else catch ( std::runtime_error& e )
return Json() ; {
throw ConfigError( std::string("cannot open config file ") + e.what() ) ;
}
} }
void SaveConfig( const Json& config ) void SaveConfig( const Json& config )
@ -105,10 +117,10 @@ int main( int argc, char **argv )
} }
} }
std::string refresh_token ;
try try
{ {
OAuth2 token( config["refresh_token"].As<std::string>(), client_id, client_secret ) ; refresh_token = config["refresh_token"].As<std::string>() ;
Drive drive( token ) ;
} }
catch ( const std::runtime_error& error ) catch ( const std::runtime_error& error )
{ {
@ -117,5 +129,8 @@ int main( int argc, char **argv )
return -1; return -1;
} }
OAuth2 token( refresh_token, client_id, client_secret ) ;
Drive drive( token ) ;
return 0 ; return 0 ;
} }