-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
add.ts
103 lines (90 loc) · 2.55 KB
/
add.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import chalk from 'chalk';
import { DomainNotFound, DNSPermissionDenied, DNSInvalidPort, DNSInvalidType } from '../../util/errors-ts';
import { NowContext } from '../../types';
import { Output } from '../../util/output';
import addDNSRecord from '../../util/dns/add-dns-record';
import Client from '../../util/client';
import getScope from '../../util/get-scope';
import parseAddDNSRecordArgs from '../../util/dns/parse-add-dns-record-args';
import stamp from '../../util/output/stamp';
type Options = {
'--debug': boolean;
};
export default async function add(
ctx: NowContext,
opts: Options,
args: string[],
output: Output
) {
const { authConfig: { token }, config } = ctx;
const { currentTeam } = config;
const { apiUrl } = ctx;
const debug = opts['--debug'];
const client = new Client({ apiUrl, token, currentTeam, debug });
let contextName = null;
try {
({ contextName } = await getScope(client));
} catch (err) {
if (err.code === 'NOT_AUTHORIZED') {
output.error(err.message);
return 1;
}
throw err;
}
const parsedParams = parseAddDNSRecordArgs(args);
if (!parsedParams) {
output.error(
`Invalid number of arguments. See: ${chalk.cyan(
'`now dns --help`'
)} for usage.`
);
return 1;
}
const addStamp = stamp();
const { domain, data } = parsedParams;
const record = await addDNSRecord(client, domain, data);
if (record instanceof DomainNotFound) {
output.error(
`The domain ${domain} can't be found under ${chalk.bold(
contextName
)} ${chalk.gray(addStamp())}`
);
return 1;
}
if (record instanceof DNSPermissionDenied) {
output.error(
`You don't have permissions to add records to domain ${domain} under ${chalk.bold(
contextName
)} ${chalk.gray(addStamp())}`
);
return 1;
}
if (record instanceof DNSInvalidPort) {
output.error(
`Invalid <port> parameter. A number was expected ${chalk.gray(addStamp())}`
);
return 1;
}
if (record instanceof DNSInvalidType) {
output.error(
`Invalid <type> parameter "${
record.meta.type
}". Expected one of A, AAAA, ALIAS, CAA, CNAME, MX, SRV, TXT ${
chalk.gray(addStamp())
}`
);
return 1;
}
if (record instanceof Error) {
output.error(record.message);
return 1;
}
console.log(
`${chalk.cyan('> Success!')} DNS record for domain ${chalk.bold(
domain
)} ${chalk.gray(`(${record.uid})`)} created under ${chalk.bold(
contextName
)} ${chalk.gray(addStamp())}`
);
return 0;
}