Add option to set login info on command line or read it from file

Add --login-email and --login-password to set login info on command line

Read login info from $XDG_CONFIG_HOME/lgogdownloader/login.txt if the file exists
login.txt must contain email on the first line and password on the second line
This commit is contained in:
Sude 2017-03-06 05:46:48 +02:00
parent f9981c1be0
commit f5af312fd4
3 changed files with 55 additions and 19 deletions

View File

@ -231,9 +231,6 @@ class Config
// Download
DownloadConfig dlConf;
// Galaxy
//GalaxyConfig galaxyConf;
// Directories
DirectoryConfig dirConf;
std::string sCacheDirectory;
@ -261,6 +258,8 @@ class Config
// General strings
std::string sVersionString;
std::string sVersionNumber;
std::string sEmail;
std::string sPassword;
GogAPIConfig apiConf;

View File

@ -211,6 +211,8 @@ int main(int argc, char *argv[])
("galaxy-install", bpo::value<std::string>(&galaxy_product_id_install)->default_value(""), "Install game using product id")
("galaxy-show-builds", bpo::value<std::string>(&galaxy_product_id_show_builds)->default_value(""), "Show game builds using product id")
("galaxy-platform", bpo::value<std::string>(&sGalaxyPlatform)->default_value("w"), galaxy_platform_text.c_str())
("login-email", bpo::value<std::string>(&Globals::globalConfig.sEmail)->default_value(""), "login email")
("login-password", bpo::value<std::string>(&Globals::globalConfig.sPassword)->default_value(""), "login password")
;
options_cli_all.add(options_cli_no_cfg).add(options_cli_cfg);
@ -349,6 +351,32 @@ int main(int argc, char *argv[])
}
}
if (bLogin || Globals::globalConfig.bLoginAPI || Globals::globalConfig.bLoginHTTP)
{
std::string login_conf = Globals::globalConfig.sConfigDirectory + "/login.txt";
if (boost::filesystem::exists(login_conf))
{
std::ifstream ifs(login_conf);
if (!ifs)
{
std::cerr << "Could not open login conf: " << login_conf << std::endl;
return 1;
}
else
{
std::string line;
std::vector<std::string> lines;
while (!ifs.eof())
{
std::getline(ifs, line);
lines.push_back(std::move(line));
}
Globals::globalConfig.sEmail = lines[0];
Globals::globalConfig.sPassword = lines[1];
}
}
}
if (vm.count("chunk-size"))
Globals::globalConfig.iChunkSize <<= 20; // Convert chunk size from bytes to megabytes

View File

@ -247,23 +247,32 @@ int Downloader::init()
int Downloader::login()
{
std::string email;
if (!isatty(STDIN_FILENO)) {
std::cerr << "Unable to read email and password" << std::endl;
return 0;
}
std::cerr << "Email: ";
std::getline(std::cin,email);
std::string password;
std::cerr << "Password: ";
struct termios termios_old, termios_new;
tcgetattr(STDIN_FILENO, &termios_old); // Get current terminal attributes
termios_new = termios_old;
termios_new.c_lflag &= ~ECHO; // Set ECHO off
tcsetattr(STDIN_FILENO, TCSANOW, &termios_new); // Set terminal attributes
std::getline(std::cin, password);
tcsetattr(STDIN_FILENO, TCSANOW, &termios_old); // Restore old terminal attributes
std::cerr << std::endl;
if (!Globals::globalConfig.sEmail.empty() && !Globals::globalConfig.sPassword.empty())
{
email = Globals::globalConfig.sEmail;
password = Globals::globalConfig.sPassword;
}
else
{
if (!isatty(STDIN_FILENO)) {
std::cerr << "Unable to read email and password" << std::endl;
return 0;
}
std::cerr << "Email: ";
std::getline(std::cin,email);
std::cerr << "Password: ";
struct termios termios_old, termios_new;
tcgetattr(STDIN_FILENO, &termios_old); // Get current terminal attributes
termios_new = termios_old;
termios_new.c_lflag &= ~ECHO; // Set ECHO off
tcsetattr(STDIN_FILENO, TCSANOW, &termios_new); // Set terminal attributes
std::getline(std::cin, password);
tcsetattr(STDIN_FILENO, TCSANOW, &termios_old); // Restore old terminal attributes
std::cerr << std::endl;
}
if (email.empty() || password.empty())
{