Skip to content

Commit

Permalink
Use the Executable module that exists, skip the shell by default
Browse files Browse the repository at this point in the history
  • Loading branch information
elliot-nelson committed Mar 31, 2023
1 parent 769bb07 commit 120eee9
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import { StringBufferTerminalProvider, Terminal } from '@rushstack/node-core-lib

const EXAMPLE_OPTIONS = {
url: 'https://buildcache.example.acme.com',
tokenHandler: 'node tokenHandler.js',
tokenHandler: {
exec: 'node',
args: ['tokenHandler.js']
},
uploadMethod: 'POST',
isCacheWriteAllowed: false,
pluginName: 'example-plugin',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ITerminal } from '@rushstack/node-core-library';
import { ITerminal, Executable } from '@rushstack/node-core-library';
import {
ICloudBuildCacheProvider,
ICredentialCacheEntry,
Expand All @@ -7,7 +7,6 @@ import {
EnvironmentConfiguration
} from '@rushstack/rush-sdk';
import fetch, { BodyInit, Response } from 'node-fetch';
import { exec } from './exec';

enum CredentialsOptions {
Optional,
Expand All @@ -23,12 +22,17 @@ enum FailureType {
Authentication
}

export interface IHttpBuildCacheTokenHandler {
exec: string;
args?: string[];
}

/**
* @public
*/
export interface IHttpBuildCacheProviderOptions {
url: string;
tokenHandler?: string;
tokenHandler?: IHttpBuildCacheTokenHandler;
uploadMethod?: string;
headers?: Record<string, string>;
cacheKeyPrefix?: string;
Expand All @@ -47,7 +51,7 @@ export class HttpBuildCacheProvider implements ICloudBuildCacheProvider {
private readonly _uploadMethod: string;
private readonly _headers: Record<string, string>;
private readonly _cacheKeyPrefix: string;
private readonly _tokenHandler: string | undefined;
private readonly _tokenHandler: IHttpBuildCacheTokenHandler | undefined;
private __credentialCacheId: string | undefined;

public get isCacheWriteAllowed(): boolean {
Expand Down Expand Up @@ -133,7 +137,7 @@ export class HttpBuildCacheProvider implements ICloudBuildCacheProvider {
}

public async updateCachedCredentialInteractiveAsync(terminal: ITerminal): Promise<void> {
if (typeof this._tokenHandler !== 'string') {
if (!this._tokenHandler) {
throw new Error(
`The interactive cloud credentials flow is not configured.\n` +
`Set the 'tokenHandler' setting in 'common/config/rush-plugins/${this._pluginName}.json' to a command that writes your credentials to standard output and exits with code 0 ` +
Expand All @@ -142,9 +146,9 @@ export class HttpBuildCacheProvider implements ICloudBuildCacheProvider {
);
}

const cmd: string = this._tokenHandler;
const cmd: string = `${this._tokenHandler.exec} ${(this._tokenHandler.args || []).join(' ')}`;
terminal.writeVerboseLine(`Running '${cmd}' to get credentials`);
const result = await exec(cmd, this._rushProjectRoot);
const result = Executable.spawnSync(this._tokenHandler.exec, this._tokenHandler.args || []);

terminal.writeErrorLine(result.stderr);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ export interface IRushHttpBuildCachePluginOptions {
headers?: Record<string, string>;

/**
* An optional command that prints the endpoint's credentials to stdout.
* An optional command that prints the endpoint's credentials to stdout. Provide the
* command or script to execute and, optionally, any arguments to pass to the script.
*/
tokenHandler?: string;
tokenHandler?: {
exec: string;
args?: string[];
};

/**
* Prefix for cache keys.
Expand Down
13 changes: 0 additions & 13 deletions rush-plugins/rush-http-build-cache-plugin/src/exec.test.ts

This file was deleted.

35 changes: 0 additions & 35 deletions rush-plugins/rush-http-build-cache-plugin/src/exec.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,21 @@
}
},
"tokenHandler": {
"type": "string",
"description": "(Optional) Shell command that prints the authorization token needed to communicate with the HTTPS server and exits with code 0. This command will be executed from the root of the monorepo."
"type": "object",
"description": "(Optional) Shell command that prints the authorization token needed to communicate with the HTTPS server and exits with code 0. This command will be executed from the root of the monorepo.",
"properties": {
"exec": {
"type": "string",
"description": "(Required) The command or script to execute."
},
"args": {
"type": "array",
"description": "(Optional) Arguments to pass to the command or script.",
"items": {
"type": "string"
}
}
}
},
"cacheKeyPrefix": {
"type": "string",
Expand Down

0 comments on commit 120eee9

Please sign in to comment.