diff --git a/main.cpp b/main.cpp index f999eb1..dbb85f7 100644 --- a/main.cpp +++ b/main.cpp @@ -21,6 +21,11 @@ namespace bpo = boost::program_options; +template void set_vm_value(std::map& vm, const std::string& option, const T& value) +{ + vm[option].value() = boost::any(value); +} + int main(int argc, char *argv[]) { Config config; @@ -288,10 +293,22 @@ int main(int argc, char *argv[]) Downloader downloader(config); int result = downloader.init(); + int iLoginResult = 0; if (config.bLogin) - return result; - else if (config.bSaveConfig) { + iLoginResult = result; + if (iLoginResult == 0) + return 1; + } + + if (config.bSaveConfig || iLoginResult == 1) + { + if (iLoginResult == 1) + { + set_vm_value(vm, "token", downloader.config.sToken); + set_vm_value(vm, "secret", downloader.config.sSecret); + bpo::notify(vm); + } std::ofstream ofs(config.sConfigFilePath.c_str()); if (ofs) { diff --git a/src/downloader.cpp b/src/downloader.cpp index b4e07f5..cd97f14 100644 --- a/src/downloader.cpp +++ b/src/downloader.cpp @@ -102,8 +102,8 @@ int Downloader::init() } /* Login - returns 1 if login fails - returns 0 if successful + returns 0 if login fails + returns 1 if successful */ int Downloader::login() { @@ -116,7 +116,7 @@ int Downloader::login() if (email.empty() || password.empty()) { std::cout << "Email and/or password empty" << std::endl; - return 1; + return 0; } else { @@ -124,7 +124,7 @@ int Downloader::login() if (!HTTP_Login(email, password)) { std::cout << "HTTP: Login failed" << std::endl; - return 1; + return 0; } else { @@ -134,24 +134,14 @@ int Downloader::login() if (!gogAPI->login(email, password)) { std::cout << "API: Login failed" << std::endl; - return 1; + return 0; } else { std::cout << "API: Login successful" << std::endl; - // Save token and secret to config file - std::ofstream ofs(config.sConfigFilePath.c_str()); - if (ofs) - { - ofs << "token = " << gogAPI->getToken() << std::endl << "secret = " << gogAPI->getSecret() << std::endl; - ofs.close(); - return 0; - } - else - { - std::cout << "Failed to create config: " << config.sConfigFilePath << std::endl; - return 1; - } + config.sToken = gogAPI->getToken(); + config.sSecret = gogAPI->getSecret(); + return 1; } } }