/************************************************************************* * * Copyright (c) 2023 Fabian Posch * * 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; either version 2 * of the License, or (at your option) any later version. * * 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 #include #include "util.h" #include "cli_util.hpp" void get_db_cred_env (db::db_credentials_t& db_cred) { // check if a server was given if (!db_cred.server_override) { if (std::getenv(SERVER_ADDR_ENV) == nullptr) { throw std::invalid_argument("No database server set in environment variable " SERVER_ADDR_ENV); } db_cred.server = std::getenv(SERVER_ADDR_ENV); db_cred.server_override = false; } // check if a port was given if (!db_cred.port_override) { // if no env variable is set, we use postgresql's default port if (std::getenv(SERVER_PORT_ENV) == nullptr) { std::cerr << "Warning: No port given in environment variable " SERVER_PORT_ENV ". Using default Postgresql port." << std::endl; db_cred.port = POSTGRES_DEFAULT_PORT; db_cred.port_override = false; // otherwise we try to parse it from the environment variable } else { std::string port_st = std::getenv(SERVER_PORT_ENV); DEBUG_PRINT("Parsing port " + port_st); try { db_cred.port = std::atoi(port_st.c_str()); } catch (std::invalid_argument& e) { throw std::invalid_argument("Could not parse port from environment variable " SERVER_PORT_ENV); } // make sure the port is valid if (db_cred.port <= 0) { throw std::invalid_argument("Port " SERVER_PORT_ENV " given in environment variable is not a valid port."); } db_cred.port_override = false; } } // check if a user was given if (!db_cred.uname_override) { if (std::getenv(SERVER_USER_ENV) == nullptr) { throw std::invalid_argument("No database user set in environment variable " SERVER_USER_ENV); } db_cred.uname = std::getenv(SERVER_USER_ENV); db_cred.uname_override = false; } // check if a password was given if (!db_cred.pwd_override) { if (std::getenv(SERVER_PWD_ENV) == nullptr) { throw std::invalid_argument("No database password set in environment variable " SERVER_PWD_ENV); } db_cred.pwd = std::getenv(SERVER_PWD_ENV); db_cred.pwd_override = false; } // check if a database was given if (!db_cred.dbase_override) { if (std::getenv(SERVER_DBASE_ENV) == nullptr) { throw std::invalid_argument("No database set in environment variable " SERVER_DBASE_ENV); } db_cred.dbase = std::getenv(SERVER_DBASE_ENV); db_cred.dbase_override = false; } }