-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (81 loc) · 2.21 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
#!/usr/bin/env node
const fs = require("fs");
const crypto = require("crypto");
const { program } = require("commander");
const { authorizeUser } = require("./input.js");
const { executeCommand } = require("./execution.js");
// Define the 'read' command
program
.command("read")
.description("Read the environment variables")
.action(async () => {
try {
const encryptionKey = await authorizeUser();
const env = readEnvFile(encryptionKey);
console.log(env);
} catch (error) {
console.error("Error:", error);
}
});
// Define the 'add' command
program
.command("add <name> <value>")
.description("Add a new environment variable")
.action(async (name, value) => {
try {
const encryptionKey = await authorizeUser();
executeCommand("add", name, value, encryptionKey);
} catch (error) {
console.error("Error:", error);
}
});
// Define the 'update' command
program
.command("update <name> <newValue>")
.description("Update an existing environment variable")
.action(async (name, newValue) => {
try {
const encryptionKey = await authorizeUser();
executeCommand("update", name, newValue, encryptionKey);
} catch (error) {
console.error("Error:", error);
}
});
// Define the 'list' command
program
.command("list")
.description("List all environment variables")
.action(async () => {
try {
const encryptionKey = await authorizeUser();
executeCommand("list", null, null, encryptionKey);
} catch (error) {
console.error("Error:", error);
}
});
// Define the 'delete' command
program
.command("delete <name>")
.description("Delete an environment variable")
.action(async (name) => {
try {
const encryptionKey = await authorizeUser();
executeCommand("delete", name, null, encryptionKey);
} catch (error) {
console.error("Error:", error);
}
});
// Parse command-line arguments
program.parse(process.argv);
// File: index.js
function readEnvFile(encryptionKey) {
const encryptedEnv = fs.readFileSync(".env.json", "utf8");
const decipher = crypto.createDecipheriv(
"aes-256-cbc",
encryptionKey,
Buffer.from("1234567890123456", "hex")
);
let decrypted = decipher.update(encryptedEnv, "hex", "utf8");
decrypted += decipher.final("utf8");
return JSON.parse(decrypted);
}