2020-08-21 14:45:16 +02:00
|
|
|
import * as semver from 'semver';
|
|
|
|
import * as io from '@actions/io';
|
|
|
|
import * as execm from './exec';
|
|
|
|
|
|
|
|
export const isECR = async (registry: string): Promise<boolean> => {
|
|
|
|
return registry.includes('amazonaws');
|
|
|
|
};
|
|
|
|
|
2020-08-21 15:07:43 +02:00
|
|
|
export const getRegion = async (registry: string): Promise<string> => {
|
|
|
|
return registry.substring(registry.indexOf('ecr.') + 4, registry.indexOf('.amazonaws'));
|
|
|
|
};
|
|
|
|
|
2020-08-21 14:45:16 +02:00
|
|
|
export const getCLI = async (): Promise<string> => {
|
|
|
|
return io.which('aws', true);
|
|
|
|
};
|
|
|
|
|
2020-08-21 14:56:11 +02:00
|
|
|
export const getCLICmdOutput = async (args: string[]): Promise<string> => {
|
|
|
|
return execm.exec(await getCLI(), args, true).then(res => {
|
2020-08-21 14:45:16 +02:00
|
|
|
if (res.stderr != '' && !res.success) {
|
|
|
|
throw new Error(res.stderr);
|
2020-08-21 15:07:43 +02:00
|
|
|
} else if (res.stderr != '') {
|
|
|
|
return res.stderr.trim();
|
|
|
|
} else {
|
|
|
|
return res.stdout.trim();
|
2020-08-21 14:45:16 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-08-21 14:56:11 +02:00
|
|
|
export const getCLIVersion = async (): Promise<string | undefined> => {
|
|
|
|
return parseCLIVersion(await getCLICmdOutput(['--version']));
|
|
|
|
};
|
|
|
|
|
2020-08-21 14:45:16 +02:00
|
|
|
export const parseCLIVersion = async (stdout: string): Promise<string | undefined> => {
|
|
|
|
const matches = /aws-cli\/([0-9.]+)/.exec(stdout);
|
|
|
|
if (matches) {
|
|
|
|
return semver.clean(matches[1]);
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
};
|