Add files::get_access_token().

This commit is contained in:
tastytea 2020-07-02 00:18:16 +02:00
parent 0c7df4b98a
commit 119d304164
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
2 changed files with 25 additions and 0 deletions

View File

@ -19,6 +19,8 @@
#include <cstdint>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <string>
@ -26,8 +28,10 @@ namespace FediBlock::files
{
using std::getenv;
using std::ifstream;
using std::runtime_error;
using std::string;
using std::stringstream;
using std::uintmax_t;
string _tmpdir;
@ -82,4 +86,18 @@ fs::path get_datadir()
throw runtime_error{"Could not find data dir"};
}
string get_access_token()
{
ifstream file(get_datadir() / "gitea_access_token");
if (!file.good())
{
throw runtime_error{"Could not read access token: "
+ (get_datadir() / "gitea_access_token").string()};
}
stringstream ss;
ss << file.rdbuf();
return ss.str().substr(0, ss.str().find('\n'));
}
} // namespace FediBlock::files

View File

@ -19,9 +19,13 @@
#include "fs-compat.hpp"
#include <string>
namespace FediBlock::files
{
using std::string;
// Creates and returns the temporary directory.
[[nodiscard]] fs::path get_tmpdir();
@ -31,6 +35,9 @@ void remove_tmpdir();
// Returns the data directory and creates it if necessary.
[[nodiscard]] fs::path get_datadir();
// Read and return access token.
[[nodiscard]] string get_access_token();
} // namespace FediBlock::files
#endif // FEDIBLOCK_BACKEND_FILES_HPP