-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (73 loc) · 2.34 KB
/
index.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
const fs = require('fs')
const AWS = require('aws-sdk')
const colors = require('colors')
const dotenv = require('dotenv')
dotenv.config()
const S3 = new AWS.S3({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
})
const BUCKET = process.env.BUCKET_NAME
const FLAG = process.argv[2]
const ARGV_3 = process.argv[3]
const ARGV_4 = process.argv[4]
const uploadFileToBucket = () => fs.readFile(ARGV_3, (err, file) => {
if (err) return console.log(err)
const pathHasSlash = ARGV_3.indexOf('/') > -1
const fileName = pathHasSlash && ARGV_3.split('/').pop()
const params = {
Bucket: BUCKET,
Key: (fileName || `file(${new Date().toJSON()})`),
Body: JSON.stringify(file, null, 2)
}
S3.upload(params, (err, data) => {
if (err) return console.log(err)
console.log(`File uploaded successfully at ${data.Location}`.bgGreen)
})
})
const listFiles = async (byMatch = false) => {
let isTruncated = true
let marker
while (isTruncated) {
const params = { Bucket: BUCKET }
if (marker) params.Marker = marker
try {
const response = await S3.listObjects(params).promise()
if (byMatch) {
const regexFlags = ARGV_4
const regex = new RegExp(ARGV_3, regexFlags)
response.Contents.forEach(item => item.Key.match(regex) && console.log(`${item.Key}`.cyan))
} else {
response.Contents.forEach(item => console.log(`${item.Key}`.cyan))
}
isTruncated = response.IsTruncated
if (isTruncated) marker = response.Contents.slice(-1)[0].Key
} catch (error) {
console.log(error)
}
}
}
const deleteFilesByMatch = async () => {
const keysToRemove = await listFilesByMatch()
S3.deleteObjects(
{
Bucket: BUCKET,
Delete: {
Objects: keysToRemove
}
}, (err, res) => {
if (err) return console.log(err)
console.log(res)
}
)
}
//---> LIST ALL BUCKET FILES
if (FLAG === '--list' && !ARGV_3) return listFiles()
//---> UPLOAD LOCAL FILE TO A BUCKET
if (FLAG === '--upload' && ARGV_3) return uploadFileToBucket()
//---> LIST FILES FILTERED/MATCHED
if (FLAG === '--list' && ARGV_3) return listFiles(true)
//---> DELETE FILES FILTERED/MATCHED
if (FLAG === '--delete' && ARGV_3) return deleteFilesByMatch()
//---> WRONG COMMAND HANDLER
console.log(`The command was not recognized`.bgRed)