setup-java/src/auth.ts

72 lines
1.9 KiB
TypeScript
Raw Normal View History

2019-11-16 00:01:13 +00:00
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as core from '@actions/core';
import * as io from '@actions/io';
export const M2_DIR = '.m2';
export const SETTINGS_FILE = 'settings.xml';
2019-11-28 20:40:45 +00:00
export async function configAuthentication(
id: string,
username: string,
password: string
) {
2019-11-28 20:40:08 +00:00
if (id && username && password) {
2019-11-28 20:54:29 +00:00
console.log(
`creating ${SETTINGS_FILE} with server-id: ${id}, username: ${username}, and a password`
);
// when an alternate m2 location is specified use only that location (no .m2 directory)
// otherwise use the home/.m2/ path
const directory: string = path.join(
2019-12-10 21:02:21 +00:00
core.getInput('settings-path') || os.homedir(),
core.getInput('settings-path') ? '' : M2_DIR
);
await io.mkdirP(directory);
core.debug(`created directory ${directory}`);
2019-11-28 20:40:08 +00:00
await write(directory, generate(id, username, password));
} else {
core.debug(
2019-11-28 20:54:29 +00:00
`no ${SETTINGS_FILE} without server-id: ${id}, username: ${username}, and a password`
);
}
2019-11-16 00:01:13 +00:00
}
2019-12-10 17:26:51 +00:00
function escapeXML(value: string) {
return value
.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;');
}
2019-11-16 00:01:13 +00:00
// only exported for testing purposes
2019-11-28 20:40:08 +00:00
export function generate(id: string, username: string, password: string) {
return `
<settings>
<servers>
<server>
2019-12-10 17:26:51 +00:00
<id>${escapeXML(id)}</id>
<username>${escapeXML(username)}</username>
<password>${escapeXML(password)}</password>
</server>
</servers>
</settings>
`;
2019-11-16 00:01:13 +00:00
}
async function write(directory: string, settings: string) {
const location = path.join(directory, SETTINGS_FILE);
2019-12-19 16:52:26 +00:00
if (fs.existsSync(location)) {
console.warn(`overwriting existing file ${location}`);
} else {
console.log(`writing ${location}`);
2019-12-05 04:54:21 +00:00
}
2019-12-19 16:52:26 +00:00
return fs.writeFileSync(location, settings, {
encoding: 'utf-8',
flag: 'w'
});
2019-11-16 00:01:13 +00:00
}