Skip to content

Commit

Permalink
Support reading api_url from env var
Browse files Browse the repository at this point in the history
  • Loading branch information
alanhamlett committed Sep 10, 2022
1 parent ef11262 commit 17d6f52
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
32 changes: 21 additions & 11 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,26 +202,28 @@ export class Options {
return this.logFile;
}

// Support for gitpod.io https://github.com/wakatime/vscode-wakatime/pull/220
public isApiKeyFromEnv(): boolean {
return !!(process.env.WAKATIME_API_KEY && !Utils.apiKeyInvalid(process.env.WAKATIME_API_KEY));
}

public async getApiKeyAsync(): Promise<string> {
return new Promise<string>(async (resolve, reject) => {
const key = this.getApiKeyFromEnv();
if (!Utils.apiKeyInvalid(key)) {
resolve(key!);
resolve(key);
return;
}

if (!Utils.apiKeyInvalid(this.cache.api_key)) {
resolve(this.cache.api_key);
return;
}

try {
const apiKey = await this.getSettingAsync<string>('settings', 'api_key');
if (!Utils.apiKeyInvalid(apiKey)) this.cache.api_key = apiKey;
resolve(apiKey);
return;
} catch (err) {
this.logger.debug(`Exception while reading API Key from config file: ${err}`);
reject(err);
return;
}
});
}
Expand All @@ -246,13 +248,21 @@ export class Options {
});
}

public getApiKeyFromEnv(): string | undefined {
const cachedApiKey = this.cache.api_key;
if (!Utils.apiKeyInvalid(cachedApiKey)) return cachedApiKey;
// Support for gitpod.io https://github.com/wakatime/vscode-wakatime/pull/220
public getApiKeyFromEnv(): string {
if (this.cache.api_key_from_env !== undefined) return this.cache.api_key_from_env;

this.cache.api_key_from_env = process.env.WAKATIME_API_KEY || '';

return this.cache.api_key_from_env;
}

public getApiUrlFromEnv(): string {
if (this.cache.api_url_from_env !== undefined) return this.cache.api_url_from_env;

if (this.isApiKeyFromEnv()) return process.env.WAKATIME_API_KEY;
this.cache.api_url_from_env = process.env.WAKATIME_API_URL || '';

return undefined;
return this.cache.api_url_from_env;
}

public hasApiKey(callback: (valid: boolean) => void): void {
Expand Down
12 changes: 10 additions & 2 deletions src/wakatime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,10 @@ export class WakaTime {
}

const apiKey = this.options.getApiKeyFromEnv();
if (!Utils.apiKeyInvalid(apiKey)) args.push('--key', Utils.quote(apiKey!));
if (!Utils.apiKeyInvalid(apiKey)) args.push('--key', Utils.quote(apiKey));

const apiUrl = this.options.getApiUrlFromEnv();
if (apiUrl) args.push('--api-url', Utils.quote(apiUrl));

let project = this.getProjectName(file);
if (project) args.push('--alternate-project', Utils.quote(project));
Expand Down Expand Up @@ -565,8 +568,13 @@ export class WakaTime {
let user_agent =
this.agentName + '/' + vscode.version + ' vscode-wakatime/' + this.extension.version;
let args = ['--today', '--plugin', Utils.quote(user_agent)];

const apiKey = this.options.getApiKeyFromEnv();
if (!Utils.apiKeyInvalid(apiKey)) args.push('--key', Utils.quote(apiKey!));
if (!Utils.apiKeyInvalid(apiKey)) args.push('--key', Utils.quote(apiKey));

const apiUrl = this.options.getApiUrlFromEnv();
if (apiUrl) args.push('--api-url', Utils.quote(apiUrl));

if (Dependencies.isWindows()) {
args.push(
'--config',
Expand Down

0 comments on commit 17d6f52

Please sign in to comment.