zbackup/config.cc

83 lines
2.1 KiB
C++
Raw Normal View History

// Copyright (c) 2012-2014 Konstantin Isakov <ikm@zbackup.org> and ZBackup contributors, see CONTRIBUTORS
// Part of ZBackup. Licensed under GNU GPLv2 or later + OpenSSL, see LICENSE
2014-12-30 17:13:48 +03:00
#include "zbackup_base.hh"
#include "zbackup.pb.h"
#include "debug.hh"
2014-12-30 19:27:31 +03:00
#include "config.hh"
2014-12-29 17:24:09 +03:00
2015-01-13 18:01:47 +03:00
bool ZConfig::parseOption( Config & config, const char * option )
{
dPrintf( "Parsing option \"%s\"...\n", option );
return false;
}
void ZConfig::showHelp()
{
fprintf( stderr,
"Available options overview:\n"
" help - show this message\n"
"");
}
ZConfig::ZConfig( string const & storageDir, string const & password ):
ZBackupBase( storageDir, password, true )
{
}
2015-01-13 18:01:47 +03:00
ZConfig::ZConfig( string const & storageDir, string const & password, Config & configIn ):
ZBackupBase( storageDir, password, true )
{
}
2014-12-30 19:27:31 +03:00
bool ZConfig::parse( const string & str, google::protobuf::Message * mutable_message )
{
return google::protobuf::TextFormat::ParseFromString( str, mutable_message );
}
string ZConfig::toString( google::protobuf::Message const & message )
{
std::string str;
google::protobuf::TextFormat::PrintToString( message, &str );
return str;
}
2014-12-30 17:13:48 +03:00
void ZConfig::show()
{
printf( "%s", toString( extendedStorageInfo.config() ).c_str() );
}
2014-12-30 19:27:31 +03:00
bool ZConfig::validate( const string & configData, const string & newConfigData )
2014-12-29 17:24:09 +03:00
{
2014-12-30 19:27:31 +03:00
ConfigInfo newConfig;
return parse( newConfigData, &newConfig );
}
2014-12-29 17:24:09 +03:00
2014-12-30 19:27:31 +03:00
bool ZConfig::editInteractively()
{
string configData( toString( extendedStorageInfo.config() ) );
string newConfigData( configData );
2014-12-29 17:24:09 +03:00
2014-12-30 19:27:31 +03:00
if ( !spawnEditor( newConfigData, &validate ) )
return false;
2014-12-30 17:13:48 +03:00
ConfigInfo newConfig;
if ( !parse( newConfigData, &newConfig ) )
2014-12-30 19:27:31 +03:00
return false;
if ( toString( extendedStorageInfo.config() ) == toString( newConfig ) )
2014-12-30 17:13:48 +03:00
{
2014-12-30 19:27:31 +03:00
verbosePrintf( "No changes made to config\n" );
return false;
2014-12-29 17:24:09 +03:00
}
2014-12-30 19:27:31 +03:00
verbosePrintf( "Updating configuration...\n" );
2014-12-30 17:13:48 +03:00
2014-12-30 19:27:31 +03:00
extendedStorageInfo.mutable_config()->CopyFrom( newConfig );
verbosePrintf(
2014-12-30 17:13:48 +03:00
"Configuration successfully updated!\n"
"Updated configuration:\n\n%s", toString( extendedStorageInfo.config() ).c_str() );
2014-12-29 17:24:09 +03:00
2014-12-30 19:27:31 +03:00
return true;
}