-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
169 lines (134 loc) · 4.61 KB
/
cli.js
File metadata and controls
169 lines (134 loc) · 4.61 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
const fs = require("fs");
const path = require("path");
const readline = require("readline");
const {program} = require("commander");
const chalk = require("chalk");
const colors = {
title: chalk.white.bold,
error: chalk.redBright,
};
const ds = require(".");
const pkg = require("./package.json");
program
.name("ds")
.version(pkg.version)
.description(pkg.description);
// --- Volumes -------------------------------------------------------------- //
const volumesCommands = program
.command("volume")
.alias("v")
.description("Volumes commands");
volumesCommands
.command("list [filter]")
.alias("ls")
.description("List Docker volumes")
.action(async filter => {
(await ds.listVolumes(filter)).forEach(volume => console.log(volume));
});
volumesCommands
.command("create <volume>")
.alias("c")
.description("Create a Docker volume")
.action(async volume => {
await ds.createVolume(volume);
});
volumesCommands
.command("remove <volume-filter>")
.alias("r")
.description("Create a Docker volume")
.action(async filter => {
const volumes = await ds.listVolumes(filter);
if (volumes.length === 0)
return console.log(colors.error(`No volumes matching '${filter}' found.`));
console.log();
console.log(`Volumes matching "${filter}":\n${volumes.map(v => " " + v).join("\n")}`);
console.log();
const rl = readline.createInterface({
input : process.stdin,
output: process.stdout,
});
rl.question(colors.error("Confirm deletion (y/N): "), async answer => {
rl.close();
if (answer.toLowerCase() === "y") {
for (const volume of volumes)
await ds.removeVolume(volume);
}
});
});
volumesCommands
.command("duplicate <volume> [new-volume]")
.alias("d")
.description("Create a Docker volume")
.action(async (source, destination) => {
await ds.duplicateVolume(source, destination);
});
volumesCommands
.command("save <volume> [filename]")
.alias("s")
.description("Save a Docker volume to a 7zip archive")
.option("-f, --force", "Overwrite existing file")
.action(async (volume, filename, options) => {
await ds.saveVolume(volume, filename, options.force);
});
volumesCommands
.command("load <filename> [volume]")
.alias("l")
.description("Load a Docker volume")
.option("-f, --force", "Overwrite existing volume")
.action(async (filename, volume, options) => {
await ds.loadVolume(filename, volume, options.force);
});
volumesCommands
.command("save-all <volume-filter> [directory]")
.alias("sa")
.description("Save Docker volumes matching the filter")
.option("-t, --timestamp", "Create a subdirectory with the current timestamp")
.option("-f, --force", "Overwrite existing backup files")
.option("-l, --limit <n>", "Limit the number of backups (--timestamp required)", parseInt)
.action(async (filter, directory, options) => {
directory = path.resolve(directory || ".");
if (!options.timestamp && options.limit !== undefined)
throw new Error("--limit requires --timestamp");
if (options.limit !== undefined && options.limit <= 0)
throw new Error("--limit must be greater than zero");
if (options.timestamp)
directory = path.join(directory, ds.createTimestamp());
fs.mkdirSync(directory, {recursive: true});
await ds.saveVolumes(filter, directory, options.force);
if (options.limit !== undefined) {
const entries = ds.readTimestamps(path.dirname(directory));
for (const entry of entries.slice(0, -options.limit))
fs.rmSync(entry.directory, {recursive: true});
}
});
volumesCommands
.command("load-all <files-filter>")
.alias("la")
.description("Load Docker volumes files matching the filter")
.option("-f, --force", "Overwrite existing volumes")
.action(async (files, options) => {
await ds.loadVolumes(files, options.force);
});
// --- Images --------------------------------------------------------------- //
const imagesCommands = program
.command("image")
.alias("i")
.description("Images commands");
imagesCommands
.command("list [filter]")
.alias("ls")
.description("List Docker images")
.action(async filter => {
(await ds.listImages(filter)).forEach(image => console.log(image.Repository));
});
// --- Common --------------------------------------------------------------- //
// process.on("unhandledRejection", err => {
// console.error(colors.error(err.message));
// process.exit(1);
// });
program.addHelpText("beforeAll", colors.title([
"",
`Docker Snap v${pkg.version}, by ${pkg.author}`,
"",
].join("\n")));
program.parse(process.argv);