added --print-token

This commit is contained in:
skilion 2017-05-28 20:14:50 +02:00
parent 80c60beef9
commit 3d8daa086d
2 changed files with 9 additions and 1 deletions

View file

@ -16,6 +16,8 @@ int main(string[] args)
bool logout; bool logout;
// enable verbose logging // enable verbose logging
bool verbose; bool verbose;
// print the access token
bool printAccessToken;
try { try {
auto opt = getopt( auto opt = getopt(
@ -25,7 +27,8 @@ int main(string[] args)
"resync", "Forget the last saved state, perform a full sync.", &resync, "resync", "Forget the last saved state, perform a full sync.", &resync,
"logout", "Logout the current user.", &logout, "logout", "Logout the current user.", &logout,
"confdir", "Set the directory to use to store the configuration files.", &configDirName, "confdir", "Set the directory to use to store the configuration files.", &configDirName,
"verbose|v", "Print more details, useful for debugging.", &log.verbose "verbose|v", "Print more details, useful for debugging.", &log.verbose,
"print-token", "Print the access token, useful for debugging.", &printAccessToken
); );
if (opt.helpWanted) { if (opt.helpWanted) {
defaultGetoptPrinter( defaultGetoptPrinter(
@ -71,6 +74,7 @@ int main(string[] args)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
auto onedrive = new OneDriveApi(cfg); auto onedrive = new OneDriveApi(cfg);
onedrive.printAccessToken = printAccessToken;
if (!onedrive.init()) { if (!onedrive.init()) {
log.log("Could not initialize the OneDrive API"); log.log("Could not initialize the OneDrive API");
// workaround for segfault in std.net.curl.Curl.shutdown() on exit // workaround for segfault in std.net.curl.Curl.shutdown() on exit

View file

@ -50,6 +50,9 @@ final class OneDriveApi
private SysTime accessTokenExpiration; private SysTime accessTokenExpiration;
/* private */ HTTP http; /* private */ HTTP http;
// if true, every new access token is printed
bool printAccessToken;
this(Config cfg) this(Config cfg)
{ {
this.cfg = cfg; this.cfg = cfg;
@ -244,6 +247,7 @@ final class OneDriveApi
refreshToken = response["refresh_token"].str(); refreshToken = response["refresh_token"].str();
accessTokenExpiration = Clock.currTime() + dur!"seconds"(response["expires_in"].integer()); accessTokenExpiration = Clock.currTime() + dur!"seconds"(response["expires_in"].integer());
std.file.write(cfg.refreshTokenFilePath, refreshToken); std.file.write(cfg.refreshTokenFilePath, refreshToken);
if (printAccessToken) writeln("New access token: ", accessToken);
} }
private void checkAccessTokenExpired() private void checkAccessTokenExpired()