-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcli.ts
More file actions
181 lines (179 loc) · 5.71 KB
/
Copy pathcli.ts
File metadata and controls
181 lines (179 loc) · 5.71 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import yargs from 'yargs';
import { download } from './commands/download';
import { validate } from './commands/validate';
import { seed } from './commands/seed';
import fs from 'fs';
import yaml, { YAMLException } from 'js-yaml';
import { defaultLogger, getElapsedTime, MessageType } from './services/logger';
import { SerializedMarketplace } from './models/serialized-marketplace';
import * as SeedingTemplates from '../seeds/meta.json';
import _ from 'lodash';
yargs.scriptName("@ordercloud/seeding")
.usage('$0 <cmd> [args] -')
.command('seed [data]', 'Create a new sandbox marketplace and seed data.', (yargs) => {
yargs.positional('data', {
type: 'string',
alias: 'd',
default: 'ordercloud-seed.yml',
describe: 'Local file name or HTTP(S) link'
});
yargs.option('username', {
type: 'string',
alias: 'u',
describe: 'Portal username'
})
yargs.option('password', {
type: 'string',
alias: 'p',
describe: 'Portal password'
})
yargs.option('id', {
type: 'string',
alias: 'i',
describe: 'Marketplace ID'
}),
yargs.option('name', {
type: 'string',
alias: 'n',
describe: 'Marketplace Name'
}),
yargs.option('regionId', {
type: 'string',
alias: 'r',
describe: 'Region for the marketplace. See the API docs for more information'
})
yargs.option('target', {
type: 'string',
alias: 't',
describe: 'Target environment name matching the Name attribute in your config file'
})
yargs.option('config', {
type: 'string',
alias: 'c',
describe: 'Path to the config file for client credentials authentication'
})
}, function (argv) {
var dataUrl = argv.d as string;
// Check for short-cut aliases
var template = SeedingTemplates.templates.find(x => x.name === dataUrl);
if (!_.isNil(template)) {
dataUrl = template.dataUrl;
}
var stringData;
if (!dataUrl.startsWith('http')) {
try {
stringData = fs.readFileSync(dataUrl, 'utf8') // consider switching to streams
defaultLogger(`Found file \"${dataUrl}\"`, MessageType.Success);
} catch (err) {
defaultLogger(`No such file \"${dataUrl}\" found`, MessageType.Error);
return;
}
try {
var data = yaml.load(stringData) as SerializedMarketplace;
seed({
username: argv.u as string,
password: argv.p as string,
marketplaceID: argv.i as string,
marketplaceName: argv.n as string,
regionId: argv.r as string,
target: argv.t as string,
configPath: argv.c as string,
rawData: data
});
return;
} catch (e) {
var ex = e as YAMLException;
defaultLogger(`YAML Exception in \"${dataUrl}\": ${ex.message}`, MessageType.Error)
return;
}
}
seed({
username: argv.u as string,
password: argv.p as string,
marketplaceID: argv.i as string,
marketplaceName: argv.n as string,
regionId: argv.r as string,
target: argv.t as string,
configPath: argv.c as string,
dataUrl: dataUrl as string
});
})
.command('download [filePath]', 'Create a local seed file from an existing marketplace.', (yargs) => {
yargs.option('id', {
type: 'string',
alias: 'i',
describe: 'Marketplace ID'
}),
yargs.option('username', {
type: 'string',
alias: 'u',
describe: 'Portal username'
})
yargs.option('password', {
type: 'string',
alias: 'p',
describe: 'Portal password'
})
yargs.option('target', {
type: 'string',
alias: 't',
describe: 'Target environment name matching the Name attribute in your config file'
})
yargs.option('config', {
type: 'string',
alias: 'c',
describe: 'Path to the environments config file'
})
yargs.positional('fileName', {
type: 'string',
alias: 'f',
default: 'ordercloud-seed.yml',
describe: 'File name'
})
}, async function (argv) {
var startTime = Date.now();
var data = await download({
username: argv.u as string,
password: argv.p as string,
marketplaceID: argv.i as string,
target: argv.t as string,
configPath: argv.c as string,
});
if (data) {
var path = argv.f as string ?? 'ordercloud-seed.yml';
fs.writeFileSync(path, yaml.dump(data));
var endTime = Date.now();
defaultLogger(`Wrote to file ${path}. Total elapsed time: ${getElapsedTime(startTime, endTime)}`, MessageType.Done);
}
})
.command('validate [data]', 'Validate a potential data source for seeding.', (yargs) => {
yargs.positional('data', {
type: 'string',
alias: 'd',
default: 'ordercloud-seed.yml',
describe: 'Local file path or HTTP(S) link'
})
}, async function (argv) {
var filePath = argv.d as string;
var stringData;
if (!filePath.startsWith('http')) {
try {
stringData = fs.readFileSync(filePath, 'utf8') // consider switching to streams
defaultLogger(`Found file \"${filePath}\".`, MessageType.Success);
} catch (err) {
return defaultLogger(`No such file \"${filePath}\" found`, MessageType.Error);
}
try {
var data = yaml.load(stringData) as SerializedMarketplace;
await validate({ rawData: data })
return defaultLogger(`Validation done!`, MessageType.Done);
} catch (e) {
var ex = e as YAMLException;
return defaultLogger(`YAML Exception in \"${filePath}\": ${ex.message}`, MessageType.Error)
}
}
await validate({ dataUrl: argv.d as string });
defaultLogger(`Validation done!`, MessageType.Done);
})
.help()
.argv