1
0
Files
ftp-deploy-action/src/main.ts
T

139 lines
3.9 KiB
TypeScript
Raw Normal View History

2020-08-28 01:11:57 -05:00
import * as core from "@actions/core";
2020-08-26 01:28:40 -05:00
import { deploy } from "@samkirkland/ftp-deploy";
2020-09-07 14:57:56 -05:00
import { IFtpDeployArguments } from "@samkirkland/ftp-deploy/dist/types";
2020-08-26 01:28:40 -05:00
async function runDeployment() {
const args: IFtpDeployArguments = {
server: core.getInput("server", { required: true }),
username: core.getInput("username", { required: true }),
password: core.getInput("password", { required: true }),
2020-08-27 23:43:10 -05:00
port: optionalInt("port", core.getInput("port")),
2020-10-23 23:41:37 -05:00
protocol: optionalProtocol("protocol", core.getInput("protocol")),
2020-08-30 00:05:41 -05:00
"local-dir": optionalString(core.getInput("local-dir")),
"server-dir": optionalString(core.getInput("server-dir")),
"state-name": optionalString(core.getInput("state-name")),
2020-08-27 23:43:10 -05:00
"dry-run": optionalBoolean("dry-run", core.getInput("dry-run")),
"dangerous-clean-slate": optionalBoolean("dangerous-clean-slate", core.getInput("dangerous-clean-slate")),
"exclude": optionalStringArray("exclude", core.getInput("exclude")),
2020-08-30 00:05:41 -05:00
"log-level": optionalLogLevel("log-level", core.getInput("log-level")),
"security": optionalSecurity("security", core.getInput("security"))
2020-08-26 01:28:40 -05:00
};
2020-02-18 00:34:10 -06:00
2020-03-30 23:20:38 -05:00
try {
2020-08-26 01:28:40 -05:00
await deploy(args);
2020-03-30 23:20:38 -05:00
}
catch (error) {
2020-08-26 01:28:40 -05:00
core.setFailed(error);
2020-02-18 00:34:10 -06:00
}
}
2020-08-27 23:43:10 -05:00
runDeployment();
2020-08-30 00:05:41 -05:00
function optionalString(rawValue: string): string | undefined {
if (rawValue.length === 0) {
return undefined;
}
2020-08-27 23:43:10 -05:00
2020-08-30 00:05:41 -05:00
return rawValue;
}
2020-08-27 23:43:10 -05:00
2020-08-30 00:05:41 -05:00
function optionalBoolean(argumentName: string, rawValue: string): boolean | undefined {
if (rawValue.length === 0) {
2020-08-27 23:43:10 -05:00
return undefined;
}
const cleanValue = rawValue.toLowerCase();
if (cleanValue === "true") {
return true;
}
if (cleanValue === "false") {
return false;
}
core.setFailed(`${argumentName}: invalid parameter - please use a boolean, you provided "${rawValue}". Try true or false instead.`);
}
2020-08-30 00:05:41 -05:00
function optionalProtocol(argumentName: string, rawValue: string): "ftp" | "ftps" | "ftps-legacy" | undefined {
if (rawValue.length === 0) {
2020-08-27 23:43:10 -05:00
return undefined;
}
const cleanValue = rawValue.toLowerCase();
if (cleanValue === "ftp") {
return "ftp";
}
if (cleanValue === "ftps") {
return "ftps";
}
if (cleanValue === "ftps-legacy") {
return "ftps-legacy";
}
core.setFailed(`${argumentName}: invalid parameter - you provided "${rawValue}". Try "ftp", "ftps", or "ftps-legacy" instead.`);
}
2020-10-23 23:41:37 -05:00
function optionalLogLevel(argumentName: string, rawValue: string): "minimal" | "standard" | "verbose" | undefined {
2020-08-30 00:05:41 -05:00
if (rawValue.length === 0) {
2020-08-27 23:43:10 -05:00
return undefined;
}
const cleanValue = rawValue.toLowerCase();
2020-10-23 23:41:37 -05:00
if (cleanValue === "minimal") {
return "minimal";
2020-08-27 23:43:10 -05:00
}
2020-10-23 23:41:37 -05:00
if (cleanValue === "standard") {
return "standard";
2020-08-27 23:43:10 -05:00
}
2020-10-23 23:41:37 -05:00
if (cleanValue === "verbose") {
return "verbose";
2020-08-27 23:43:10 -05:00
}
2020-10-23 23:41:37 -05:00
core.setFailed(`${argumentName}: invalid parameter - you provided "${rawValue}". Try "minimal", "standard", or "verbose" instead.`);
2020-08-27 23:43:10 -05:00
}
2020-08-30 00:05:41 -05:00
function optionalSecurity(argumentName: string, rawValue: string): "loose" | "strict" | undefined {
if (rawValue.length === 0) {
2020-08-27 23:43:10 -05:00
return undefined;
}
const cleanValue = rawValue.toLowerCase();
2020-08-30 00:05:41 -05:00
if (cleanValue === "loose") {
return "loose";
}
if (cleanValue === "strict") {
return "strict";
}
core.setFailed(`${argumentName}: invalid parameter - you provided "${rawValue}". Try "loose" or "strict" instead.`);
}
function optionalInt(argumentName: string, rawValue: string): number | undefined {
if (rawValue.length === 0) {
return undefined;
}
const valueAsNumber = parseFloat(rawValue);
2020-08-27 23:43:10 -05:00
if (Number.isInteger(valueAsNumber)) {
return valueAsNumber;
}
core.setFailed(`${argumentName}: invalid parameter - you provided "${rawValue}". Try a whole number (no decimals) instead like 1234`);
}
2020-08-30 00:05:41 -05:00
function optionalStringArray(argumentName: string, rawValue: string): string[] | undefined {
if (rawValue.length === 0) {
2020-08-27 23:43:10 -05:00
return undefined;
}
// split value by space and comma
2020-11-13 13:25:32 -06:00
return rawValue.split(" - ").filter(str => str !== "");
2020-08-27 23:43:10 -05:00
}