2012-05-15 19:07:03 +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.
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
2013-04-28 14:40:21 +04:00
|
|
|
#include "util/File.hh"
|
2015-05-16 00:27:59 +03:00
|
|
|
#include "json/JsonWriter.hh"
|
|
|
|
#include "json/JsonParser.hh"
|
2012-05-24 15:06:03 +04:00
|
|
|
|
2012-07-30 11:24:47 +04:00
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
|
2012-07-29 21:17:10 +04:00
|
|
|
#include <iostream>
|
2012-05-15 19:07:03 +04:00
|
|
|
#include <iterator>
|
|
|
|
|
2012-07-30 12:52:59 +04:00
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
2013-04-28 19:50:20 +04:00
|
|
|
namespace gr {
|
2012-05-15 19:07:03 +04:00
|
|
|
|
2012-07-30 12:52:59 +04:00
|
|
|
const std::string default_filename = ".grive";
|
|
|
|
const char *env_name = "GR_CONFIG";
|
|
|
|
const std::string default_root_folder = ".";
|
|
|
|
|
|
|
|
Config::Config( const po::variables_map& vm )
|
|
|
|
{
|
2015-05-16 00:27:59 +03:00
|
|
|
m_cmd.Add( "new-rev", Val(vm.count("new-rev") > 0) ) ;
|
|
|
|
m_cmd.Add( "force", Val(vm.count("force") > 0 ) ) ;
|
|
|
|
m_cmd.Add( "path", Val(vm.count("path") > 0
|
2012-07-30 12:52:59 +04:00
|
|
|
? vm["path"].as<std::string>()
|
|
|
|
: default_root_folder ) ) ;
|
2015-05-16 00:27:59 +03:00
|
|
|
m_cmd.Add( "dir", Val(vm.count("dir") > 0
|
2013-09-08 02:36:30 +04:00
|
|
|
? vm["dir"].as<std::string>()
|
|
|
|
: "" ) ) ;
|
2015-09-25 13:01:26 +03:00
|
|
|
m_cmd.Add( "ignore", Val(vm.count("ignore") > 0
|
|
|
|
? vm["ignore"].as<std::string>()
|
|
|
|
: "" ) ) ;
|
2012-07-30 12:52:59 +04:00
|
|
|
|
|
|
|
m_path = GetPath( fs::path(m_cmd["path"].Str()) ) ;
|
|
|
|
m_file = Read( ) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
fs::path Config::GetPath( const fs::path& root_path )
|
|
|
|
{
|
|
|
|
// 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 *env = ::getenv( env_name ) ;
|
|
|
|
return root_path / (env ? env : default_filename) ;
|
2012-05-15 19:07:03 +04:00
|
|
|
}
|
|
|
|
|
2012-07-30 12:52:59 +04:00
|
|
|
const fs::path Config::Filename() const
|
2012-05-15 19:07:03 +04:00
|
|
|
{
|
2012-07-30 12:52:59 +04:00
|
|
|
return m_path ;
|
2012-05-15 19:07:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Config::Save( )
|
|
|
|
{
|
2013-04-28 14:40:21 +04:00
|
|
|
gr::File file( m_path.string(), 0600 ) ;
|
2015-05-16 00:27:59 +03:00
|
|
|
JsonWriter wr( &file ) ;
|
|
|
|
m_file.Visit( &wr ) ;
|
2012-07-30 12:52:59 +04:00
|
|
|
}
|
|
|
|
|
2015-05-16 00:27:59 +03:00
|
|
|
void Config::Set( const std::string& key, const Val& value )
|
2012-07-30 12:52:59 +04:00
|
|
|
{
|
|
|
|
m_file.Add( key, value ) ;
|
|
|
|
}
|
|
|
|
|
2015-05-16 00:27:59 +03:00
|
|
|
Val Config::Get( const std::string& key ) const
|
2012-07-30 12:52:59 +04:00
|
|
|
{
|
|
|
|
return m_cmd.Has(key) ? m_cmd[key] : m_file[key] ;
|
2012-05-15 19:07:03 +04:00
|
|
|
}
|
|
|
|
|
2015-05-16 00:27:59 +03:00
|
|
|
Val Config::GetAll() const
|
2012-05-15 19:07:03 +04:00
|
|
|
{
|
2015-05-16 00:27:59 +03:00
|
|
|
Val::Object obj = m_file.AsObject() ;
|
|
|
|
Val::Object cmd_obj = m_cmd.AsObject() ;
|
2012-07-30 12:52:59 +04:00
|
|
|
|
2015-05-16 00:27:59 +03:00
|
|
|
for ( Val::Object::iterator i = cmd_obj.begin() ; i != cmd_obj.end() ; ++i )
|
2012-07-30 12:52:59 +04:00
|
|
|
obj[i->first] = i->second ;
|
|
|
|
|
2015-05-16 00:27:59 +03:00
|
|
|
return Val( obj ) ;
|
2012-05-15 19:07:03 +04:00
|
|
|
}
|
|
|
|
|
2015-05-16 00:27:59 +03:00
|
|
|
Val Config::Read()
|
2012-05-15 20:54:58 +04:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2013-04-28 14:40:21 +04:00
|
|
|
gr::File file(m_path) ;
|
2015-05-16 00:27:59 +03:00
|
|
|
return ParseJson( file ) ;
|
2012-05-15 20:54:58 +04:00
|
|
|
}
|
|
|
|
catch ( Exception& e )
|
|
|
|
{
|
2015-05-16 00:27:59 +03:00
|
|
|
return Val() ;
|
2012-05-15 20:54:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 11:24:47 +04:00
|
|
|
} // end of namespace
|