Skip to content

Commit

Permalink
Use built-in getExecOutput
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Jun 22, 2021
1 parent 4608add commit 39efbd2
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 170 deletions.
36 changes: 18 additions & 18 deletions __tests__/docker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ import * as exec from '@actions/exec';
process.env['RUNNER_TEMP'] = path.join(__dirname, 'runner');

test('loginStandard calls exec', async () => {
const execSpy: jest.SpyInstance = jest.spyOn(exec, 'exec');
// don't let exec try to actually run the commands
execSpy.mockImplementation(() => {});
const execSpy: jest.SpyInstance = jest.spyOn(exec, 'getExecOutput');
execSpy.mockImplementation(() =>
Promise.resolve({
exitCode: expect.any(Number),
stdout: expect.any(Function),
stderr: expect.any(Function)
})
);

const username: string = 'dbowie';
const password: string = 'groundcontrol';
Expand All @@ -20,30 +25,25 @@ test('loginStandard calls exec', async () => {
expect(execSpy).toHaveBeenCalledWith(`docker`, ['login', '--password-stdin', '--username', username, registry], {
input: Buffer.from(password),
silent: true,
ignoreReturnCode: true,
listeners: expect.objectContaining({
stdout: expect.any(Function),
stderr: expect.any(Function)
})
ignoreReturnCode: true
});
});

test('logout calls exec', async () => {
const execSpy: jest.SpyInstance = jest.spyOn(exec, 'exec');
// don't let exec try to actually run the commands
execSpy.mockImplementation(() => {});
const execSpy: jest.SpyInstance = jest.spyOn(exec, 'getExecOutput');
execSpy.mockImplementation(() =>
Promise.resolve({
exitCode: expect.any(Number),
stdout: expect.any(Function),
stderr: expect.any(Function)
})
);

const registry: string = 'https://ghcr.io';

await logout(registry);

expect(execSpy).toHaveBeenCalledWith(`docker`, ['logout', registry], {
silent: false,
ignoreReturnCode: true,
input: Buffer.from(''),
listeners: expect.objectContaining({
stdout: expect.any(Function),
stderr: expect.any(Function)
})
ignoreReturnCode: true
});
});
115 changes: 36 additions & 79 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 15 additions & 10 deletions src/aws.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as semver from 'semver';
import * as exec from '@actions/exec';
import * as io from '@actions/io';
import * as execm from './exec';

const ecrRegistryRegex = /^(([0-9]{12})\.dkr\.ecr\.(.+)\.amazonaws\.com(.cn)?)(\/([^:]+)(:.+)?)?$/;

Expand Down Expand Up @@ -43,15 +43,20 @@ export const getCLI = async (): Promise<string> => {
};

export const execCLI = async (args: string[]): Promise<string> => {
return execm.exec(await getCLI(), args, true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(res.stderr);
} else if (res.stderr != '') {
return res.stderr.trim();
} else {
return res.stdout.trim();
}
});
return exec
.getExecOutput(await getCLI(), args, {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr.trim());
} else if (res.stderr.length > 0) {
return res.stderr.trim();
} else {
return res.stdout.trim();
}
});
};

export const getCLIVersion = async (): Promise<string> => {
Expand Down
61 changes: 38 additions & 23 deletions src/docker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as core from '@actions/core';
import * as aws from './aws';
import * as execm from './exec';
import * as core from '@actions/core';
import * as exec from '@actions/exec';

export async function login(registry: string, username: string, password: string): Promise<void> {
if (await aws.isECR(registry)) {
Expand All @@ -11,11 +11,15 @@ export async function login(registry: string, username: string, password: string
}

export async function logout(registry: string): Promise<void> {
await execm.exec('docker', ['logout', registry], false).then(res => {
if (res.stderr != '' && !res.success) {
core.warning(res.stderr);
}
});
await exec
.getExecOutput('docker', ['logout', registry], {
ignoreReturnCode: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
core.warning(res.stderr.trim());
}
});
}

export async function loginStandard(registry: string, username: string, password: string): Promise<void> {
Expand All @@ -32,12 +36,18 @@ export async function loginStandard(registry: string, username: string, password
} else {
core.info(`Logging into Docker Hub...`);
}
await execm.exec('docker', loginArgs, true, password).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(res.stderr);
}
core.info(`Login Succeeded!`);
});
await exec
.getExecOutput('docker', loginArgs, {
ignoreReturnCode: true,
silent: true,
input: Buffer.from(password)
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr.trim());
}
core.info(`Login Succeeded!`);
});
}

export async function loginECR(registry: string, username: string, password: string): Promise<void> {
Expand All @@ -60,15 +70,20 @@ export async function loginECR(registry: string, username: string, password: str

core.info(`Logging into ${registry}...`);
loginCmds.forEach((loginCmd, index) => {
execm.exec(loginCmd, [], true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(res.stderr);
}
if (loginCmds.length > 1) {
core.info(`Login Succeeded! (${index}/${loginCmds.length})`);
} else {
core.info('Login Succeeded!');
}
});
exec
.getExecOutput(loginCmd, [], {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr.trim());
}
if (loginCmds.length > 1) {
core.info(`Login Succeeded! (${index}/${loginCmds.length})`);
} else {
core.info('Login Succeeded!');
}
});
});
}
40 changes: 0 additions & 40 deletions src/exec.ts

This file was deleted.

0 comments on commit 39efbd2

Please sign in to comment.