-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
265 lines (231 loc) · 7.62 KB
/
main.js
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env node
const chalk = require('chalk');
const cli = require('./cli')
const s3api = require('./s3api');
const VERSION = '0.1.10606';
// read .env and .env.defaults
require('dotenv-defaults/config');
const logger = console; // for now
let args;
let bucket;
let Prefix = '';
let cmd0 = 's3sh'; // the name of this command
let commandLineOptions = {
// Types
'--access': String, // --access <string> or --access=<string>
'--secret': String, // --access <string> or --access=<string>
'--profile': String, // --profile <string> or --profile=<string>
'--region': String, // --region <string> or --region=<string>
'--bucket': String, // --bucket <string> or --bucket=<string>
'--quiet': Boolean,
'--verbose': Boolean,
'--version': Boolean,
'--help': Boolean,
// Aliases
'-a': '--access',
'-s': '--secret',
'-b': '--bucket',
'-r': '--region',
'-p': '--profile',
'-q': '--quiet',
'-V': '--verbose',
'-v': '--version',
'-h': '--help',
'-?': '--help'
};
cli.setCommandLineHelp('--access', 'specifies AWS IAM access key, e.g. AKIAIOSFODNN7EXAMPLE');
cli.setCommandLineHelp('--secret', 'specifies AWS IAM secret key, e.g. wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY');
cli.setCommandLineHelp('--profile', 'specifies AWS user credentials profile to use (from the ~/.aws folder)');
cli.setCommandLineHelp('--bucket', 'specifies AWS S3 bucket to use');
cli.setCommandLineHelp('--region', 'specifies AWS region to use, e.g. ca-central-1');
cli.setCommandLineHelp('--verbose', 'enables verbose mode, showing more output');
cli.setCommandLineHelp('--version', 'displays the version number of this utility');
cli.setCommandLineHelp('--help', 'shows this command-line syntax help');
function handleShutdown(rc) {
process.exit(rc);
}
process.on('SIGTERM', () => {
logger.info('Terminate (SIGTERM) signal received.');
handleShutdown(0);
});
process.on('SIGINT', () => {
logger.info('Interrupt (SIGINT) signal received.');
handleShutdown(0);
});
function onError(err) {
logger.error("Server error:", err.message);
handleShutdown(1); //mandatory return code (as per the Node.js docs)
}
process.on('uncaughtException', onError);
function onAccessKey(tokens) {
if ((tokens.length !== 2) || (tokens[1]==='?') || (tokens[1]==='help')) {
console.log('Syntax: access <accessKey>');
return;
}
s3api.setAccessKey(tokens[1]);
console.log("Access key set to:", tokens[1]);
}
function onSecretKey(tokens) {
if ((tokens.length !== 2) || (tokens[1]==='?') || (tokens[1]==='help')) {
console.log('Syntax: secret <secretKey>');
return;
}
s3api.setSecretAccessKey(tokens[1]);
console.log("Secret key set to:", tokens[1]);
}
function onBucket(tokens) {
if ((tokens.length !== 2) || (tokens[1]==='?') || (tokens[1]==='help')) {
console.log('Syntax: bucket <bucketName>');
return;
}
s3api.setBucket(tokens[1]);
console.log("Bucket ID set to:", tokens[1]);
}
function onRegion(tokens) {
if ((tokens.length !== 2) || (tokens[1]==='?') || (tokens[1]==='help')) {
console.log('Syntax: region <regionId>');
return;
}
s3api.setRegion(tokens[1]);
console.log("Region ID set to:", tokens[1]);
}
function onProfile(tokens) {
if ((tokens.length !== 2) || (tokens[1]==='?') || (tokens[1]==='help')) {
console.log('Syntax: region <regionId>');
return;
}
s3api.setProfile(tokens[1]);
console.log("Profile ID set to:", tokens[1]);
}
function onPWD(tokens) {
Prefix = s3api.normalizePrefix(Prefix);
console.log(`Context is now: /${Prefix || ''}`);
}
function onCD(tokens) {
if (tokens.length < 2) {
return onPWD(tokens);
}
let where = applyFolderToPath(Prefix, tokens[1]);
Prefix = s3api.normalizePrefix(where);
onPWD();
}
function isAbsolute(what) {
if (!what) return false;
return what.startsWith('/') || what.startsWith('~');
}
function applyFolderToPath(base, folder) {
let where = base;
if (folder) {
if (isAbsolute(folder)) {
where = folder;
} else {
where += folder;
}
}
return where;
}
async function onList(tokens) {
if ((tokens[1]==='?') || (tokens[1]==='help')) {
console.log('Syntax: list [<what>]');
return;
}
let where = (tokens.length >= 2) ? applyFolderToPath(Prefix, tokens[1]) : Prefix;
let results = await s3api.docList(where);
for (let item of results) {
if (item.type === 'folder') {
console.log(` ${chalk.yellow(item.name)} [${chalk.magenta(item.type)}]`);
} else {
console.log(` ${chalk.green(item.name)} ${chalk.magenta(item.size+' bytes')} [${chalk.grey(item.modified)}]`);
}
}
}
async function onGet(tokens) {
if ((tokens.length !== 2) || (tokens[1]==='?') || (tokens[1]==='help')) {
console.log('Syntax: read <what>');
return;
}
let where = (tokens.length >= 2) ? applyFolderToPath(Prefix, tokens[1]) : Prefix;
let doc = await s3api.docGet(where);
if (doc) {
// console.log(`${doc.path}:\n`+JSON.stringify(doc.data, null, 2));
console.log(where+":\n"+JSON.stringify(doc, null, 2));
}
}
async function onShow(tokens) {
if ((tokens.length !== 2) || (tokens[1]==='?') || (tokens[1]==='help')) {
console.log('Syntax: read <what>');
return;
}
let where = (tokens.length >= 2) ? applyFolderToPath(Prefix, tokens[1]) : Prefix;
let doc = await s3api.docGet(where);
if (doc) {
console.log(where+":\n"+doc);
}
}
function onEnv(tokens) {
let accessKey = process.env.AWS_ACCESS_KEY_ID;
let secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY
console.log("AWS_ACCESS_KEY_ID:", accessKey ? accessKey.slice(0,4)+'...'+accessKey.slice(-2) : '(not set)');
console.log("AWS_SECRET_ACCESS_KEY:", secretAccessKey ? secretAccessKey.slice(0,4)+'...'+secretAccessKey.slice(-2) : '(not set)');
console.log("AWS_PROFILE:", process.env.AWS_PROFILE || '(not set)');
console.log("AWS_REGION_ID:", process.env.AWS_REGION_ID || '(not set)');
console.log("AWS_BUCKET:", process.env.AWS_BUCKET || '(not set)');
}
try {
args = cli.initCommandLine(cmd0, commandLineOptions);
} catch (err) {
console.error(err.message);
handleShutdown(1);
}
// Mainline / top-level async function invocation.
(async () => {
try {
if (!args['--quiet']) {
console.log(chalk.yellow("s3sh - S3 Shell ")+chalk.magenta(VERSION))
}
if (args['--help']) {
cli.onCommandLineHelp();
handleShutdown(0);
}
if (args['--access']) {
s3api.setAccessKey(args['--access']);
}
if (args['--secret']) {
s3api.setSecretAccessKey(args['--secret']);
}
if (args['--profile']) {
s3api.setProfile(args['--profile']);
if (!args['--verbose']) {
console.log('Profile set to:', args['--profile']);
}
}
if (args['--region']) {
s3api.setRegion(args['--region']);
if (!args['--verbose']) {
console.log('Region set to:', args['--region']);
}
}
if (args['--bucket']) {
s3api.setBucket(args['--bucket']);
if (!args['--verbose']) {
console.log('Bucket set to:', args['--bucket']);
}
}
await s3api.init();
cli.init(process.stdin, process.stdout);
cli.addCommand(onAccessKey, ['access']);
cli.addCommand(onSecretKey, ['secret']);
cli.addCommand(onProfile, ['profile']);
cli.addCommand(onRegion, ['region']);
cli.addCommand(onBucket, ['bucket', 'proj', 'project']);
cli.addCommand(onEnv, ['env']);
cli.addCommand(onPWD, ['cwd', 'pwd']);
cli.addCommand(onCD, ['cd']);
cli.addCommand(onList, ['list', 'ls', 'dir']);
cli.addCommand(onGet, ['get', 'dump']);
cli.addCommand(onShow, ['read', 'type', 'cat', 'show', 'more']);
await cli.run();
} catch (e) {
logger.error(e);
}
})();