Skip to content

Commit

Permalink
Revert "Support reading api_url from env var"
Browse files Browse the repository at this point in the history
This reverts commit 144c8bf.
  • Loading branch information
alanhamlett committed Sep 9, 2022
1 parent 12defcd commit 02c9d8e
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 144 deletions.
254 changes: 120 additions & 134 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,13 @@ export class Options {
});
}

public getSetting(
section: string,
key: string,
internal: boolean,
callback: (Setting) => void,
): void {
public getSetting(section: string, key: string, internal: boolean, callback: (Setting) => void): void {
fs.readFile(
this.getConfigFile(internal),
'utf-8',
(err: NodeJS.ErrnoException | null, content: string) => {
if (err) {
callback({
error: new Error(`could not read ${this.getConfigFile(internal)}`),
key: key,
value: null,
});
callback({error: new Error(`could not read ${this.getConfigFile(internal)}`), key: key, value: null});
} else {
let currentSection = '';
let lines = content.split('\n');
Expand All @@ -65,133 +56,141 @@ export class Options {
let parts = line.split('=');
let currentKey = parts[0].trim();
if (currentKey === key && parts.length > 1) {
callback({ key: key, value: this.removeNulls(parts[1].trim()) });
callback({key: key, value: this.removeNulls(parts[1].trim())});
return;
}
}
}

callback({ key: key, value: null });
callback({key: key, value: null});
}
},
);
}

public setSetting(section: string, key: string, val: string, internal: boolean): void {
const configFile = this.getConfigFile(internal);
fs.readFile(configFile, 'utf-8', (err: NodeJS.ErrnoException | null, content: string) => {
// ignore errors because config file might not exist yet
if (err) content = '';

let contents: string[] = [];
let currentSection = '';

let found = false;
let lines = content.split('\n');
for (var i = 0; i < lines.length; i++) {
let line = lines[i];
if (this.startsWith(line.trim(), '[') && this.endsWith(line.trim(), ']')) {
if (currentSection === section && !found) {
contents.push(this.removeNulls(key + ' = ' + val));
found = true;
}
currentSection = line
.trim()
.substring(1, line.trim().length - 1)
.toLowerCase();
contents.push(this.removeNulls(line));
} else if (currentSection === section) {
let parts = line.split('=');
let currentKey = parts[0].trim();
if (currentKey === key) {
if (!found) {
fs.readFile(
configFile,
'utf-8',
(err: NodeJS.ErrnoException | null, content: string) => {
// ignore errors because config file might not exist yet
if (err) content = '';

let contents: string[] = [];
let currentSection = '';

let found = false;
let lines = content.split('\n');
for (var i = 0; i < lines.length; i++) {
let line = lines[i];
if (this.startsWith(line.trim(), '[') && this.endsWith(line.trim(), ']')) {
if (currentSection === section && !found) {
contents.push(this.removeNulls(key + ' = ' + val));
found = true;
}
currentSection = line
.trim()
.substring(1, line.trim().length - 1)
.toLowerCase();
contents.push(this.removeNulls(line));
} else if (currentSection === section) {
let parts = line.split('=');
let currentKey = parts[0].trim();
if (currentKey === key) {
if (!found) {
contents.push(this.removeNulls(key + ' = ' + val));
found = true;
}
} else {
contents.push(this.removeNulls(line));
}
} else {
contents.push(this.removeNulls(line));
}
} else {
contents.push(this.removeNulls(line));
}
}

if (!found) {
if (currentSection !== section) {
contents.push('[' + section + ']');
if (!found) {
if (currentSection !== section) {
contents.push('[' + section + ']');
}
contents.push(this.removeNulls(key + ' = ' + val));
}
contents.push(this.removeNulls(key + ' = ' + val));
}

fs.writeFile(configFile as string, contents.join('\n'), (err) => {
if (err) throw err;
});
});
fs.writeFile(configFile as string, contents.join('\n'), err => {
if (err) throw err;
});
},
);
}

public setSettings(section: string, settings: Setting[], internal: boolean): void {
const configFile = this.getConfigFile(internal);
fs.readFile(configFile, 'utf-8', (err: NodeJS.ErrnoException | null, content: string) => {
// ignore errors because config file might not exist yet
if (err) content = '';

let contents: string[] = [];
let currentSection = '';

const found = {};
let lines = content.split('\n');
for (var i = 0; i < lines.length; i++) {
let line = lines[i];
if (this.startsWith(line.trim(), '[') && this.endsWith(line.trim(), ']')) {
if (currentSection === section) {
settings.forEach((setting) => {
if (!found[setting.key]) {
contents.push(this.removeNulls(setting.key + ' = ' + setting.value));
found[setting.key] = true;
fs.readFile(
configFile,
'utf-8',
(err: NodeJS.ErrnoException | null, content: string) => {
// ignore errors because config file might not exist yet
if (err) content = '';

let contents: string[] = [];
let currentSection = '';

const found = {};
let lines = content.split('\n');
for (var i = 0; i < lines.length; i++) {
let line = lines[i];
if (this.startsWith(line.trim(), '[') && this.endsWith(line.trim(), ']')) {
if (currentSection === section) {
settings.forEach(setting => {
if (!found[setting.key]) {
contents.push(this.removeNulls(setting.key + ' = ' + setting.value));
found[setting.key] = true;
}
});
}
currentSection = line
.trim()
.substring(1, line.trim().length - 1)
.toLowerCase();
contents.push(this.removeNulls(line));
} else if (currentSection === section) {
let parts = line.split('=');
let currentKey = parts[0].trim();
let keepLineUnchanged = true;
settings.forEach(setting => {
if (currentKey === setting.key) {
keepLineUnchanged = false;
if (!found[setting.key]) {
contents.push(this.removeNulls(setting.key + ' = ' + setting.value));
found[setting.key] = true;
}
}
});
}
currentSection = line
.trim()
.substring(1, line.trim().length - 1)
.toLowerCase();
contents.push(this.removeNulls(line));
} else if (currentSection === section) {
let parts = line.split('=');
let currentKey = parts[0].trim();
let keepLineUnchanged = true;
settings.forEach((setting) => {
if (currentKey === setting.key) {
keepLineUnchanged = false;
if (!found[setting.key]) {
contents.push(this.removeNulls(setting.key + ' = ' + setting.value));
found[setting.key] = true;
}
if (keepLineUnchanged) {
contents.push(this.removeNulls(line));
}
});
if (keepLineUnchanged) {
} else {
contents.push(this.removeNulls(line));
}
} else {
contents.push(this.removeNulls(line));
}
}

settings.forEach((setting) => {
if (!found[setting.key]) {
if (currentSection !== section) {
contents.push('[' + section + ']');
currentSection = section;
settings.forEach(setting => {
if (!found[setting.key]) {
if (currentSection !== section) {
contents.push('[' + section + ']');
currentSection = section;
}
contents.push(this.removeNulls(setting.key + ' = ' + setting.value));
found[setting.key] = true;
}
contents.push(this.removeNulls(setting.key + ' = ' + setting.value));
found[setting.key] = true;
}
});
});

fs.writeFile(configFile as string, contents.join('\n'), (err) => {
if (err) throw err;
});
});
fs.writeFile(configFile as string, contents.join('\n'), err => {
if (err) throw err;
});
},
);
}

public getConfigFile(internal: boolean): string {
Expand All @@ -202,74 +201,61 @@ 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)) return this.cache.api_key;

try {
const apiKey = await this.getSettingAsync<string>('settings', 'api_key');
if (!Utils.apiKeyInvalid(apiKey)) this.cache.api_key = apiKey;
resolve(apiKey);
return;
} catch (err) {
} catch(err) {
this.logger.debug(`Exception while reading API Key from config file: ${err}`);
reject(err);
return;
}
});
}

public getApiKey(callback: (apiKey: string | null) => void): void {
public getApiKey(callback: (apiKey: string|null) => void): void {
this.getApiKeyAsync()
.then((apiKey) => {
.then(apiKey => {
if (!Utils.apiKeyInvalid(apiKey)) {
callback(apiKey);
} else {
callback(null);
}
})
.catch((err) => {
.catch(err => {
this.logger.warn(`Unable to get api key: ${err}`);
if (`${err}`.includes('spawn EPERM')) {
vscode.window.showErrorMessage(
'Microsoft Defender is blocking WakaTime. Please allow WakaTime to run so it can upload code stats to your dashboard.',
);
vscode.window.showErrorMessage("Microsoft Defender is blocking WakaTime. Please allow WakaTime to run so it can upload code stats to your dashboard.");
}
callback(null);
});
}

// 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;

if (!Utils.apiKeyInvalid(process.env.WAKATIME_API_KEY)) {
this.cache.api_key_from_env = process.env.WAKATIME_API_KEY;
} else {
this.cache.api_key_from_env = '';
}

return this.cache.api_key_from_env;
}

public getApiUrlFromEnv(): string {
if (this.cache.api_url !== undefined) return this.cache.api_url;
public getApiKeyFromEnv(): string|undefined {
const cachedApiKey = this.cache.api_key;
if (!Utils.apiKeyInvalid(cachedApiKey)) return cachedApiKey;

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

return this.cache.api_url;
return undefined;
}

public hasApiKey(callback: (valid: boolean) => void): void {
this.getApiKeyAsync()
.then((apiKey) => callback(!Utils.apiKeyInvalid(apiKey)))
.catch((err) => {
.then(apiKey => callback(!Utils.apiKeyInvalid(apiKey)))
.catch(err => {
this.logger.warn(`Unable to check for api key: ${err}`);
callback(false);
});
Expand Down
12 changes: 2 additions & 10 deletions src/wakatime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,7 @@ export class WakaTime {
}

const apiKey = this.options.getApiKeyFromEnv();
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 (!Utils.apiKeyInvalid(apiKey)) args.push('--key', Utils.quote(apiKey!));

let project = this.getProjectName(file);
if (project) args.push('--alternate-project', Utils.quote(project));
Expand Down Expand Up @@ -568,13 +565,8 @@ 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));

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

if (!Utils.apiKeyInvalid(apiKey)) args.push('--key', Utils.quote(apiKey!));
if (Dependencies.isWindows()) {
args.push(
'--config',
Expand Down

0 comments on commit 02c9d8e

Please sign in to comment.