Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
go.gopath and go.toolsGopath now expand ${env:XXX} (#1743)
Browse files Browse the repository at this point in the history
* go.gopath and go.toolsGopath now expand ${env:XXX}

* Fix linting errors

* remove 'm' flag from substituteEnv

* add test for substituteEnv

* add newline on end of utils.test.ts
  • Loading branch information
sphawk authored and ramya-rao-a committed Oct 20, 2018
1 parent 009501d commit ea15676
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ export function getToolsGopath(useCache: boolean = true): string {

function resolveToolsGopath(): string {

let toolsGopathForWorkspace = vscode.workspace.getConfiguration('go')['toolsGopath'] || '';
let toolsGopathForWorkspace = substituteEnv(vscode.workspace.getConfiguration('go')['toolsGopath'] || '');

// In case of single root
if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length <= 1) {
Expand Down Expand Up @@ -408,6 +408,12 @@ export function getToolsEnvVars(): any {
return envVars;
}

export function substituteEnv(input: string): string {
return input.replace(/\${env:([^}]+)}/g, function (match, capture) {
return process.env[capture.trim()] || '';
});
}

export function getCurrentGoPath(workspaceUri?: vscode.Uri): string {
let currentFilePath: string;
if (vscode.window.activeTextEditor && vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri)) {
Expand Down Expand Up @@ -443,7 +449,7 @@ export function getCurrentGoPath(workspaceUri?: vscode.Uri): string {
}
}

const configGopath = config['gopath'] ? resolvePath(config['gopath'], currentRoot) : '';
let configGopath = config['gopath'] ? resolvePath(substituteEnv(config['gopath']), currentRoot) : '';
return inferredGopath ? inferredGopath : (configGopath || process.env['GOPATH']);
}

Expand Down
25 changes: 25 additions & 0 deletions test/unit/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------*/

import * as assert from 'assert';
import { substituteEnv } from '../../src/util';

suite('utils Tests', () => {
test('substituteEnv: default', () => {

// prepare test
const env = Object.assign({}, process.env);
process.env['test1'] = 'abcd';
process.env['test2'] = 'defg';

let actual = substituteEnv(' ${env:test1} \r\n ${env:test2}\r\n${env:test1}');
let expected = ' abcd \r\n defg\r\nabcd';

assert.equal(actual, expected);

// test completed
process.env = env;
});
});

0 comments on commit ea15676

Please sign in to comment.