@datatruck/cli 0.41.5 → 0.41.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- export declare function formatBytes(bytes: number): string;
1
+ export declare function formatBytes(inBytes: number, sign?: boolean): string;
2
2
  export declare function parseSize(size: string): number;
@@ -4,14 +4,15 @@ exports.formatBytes = formatBytes;
4
4
  exports.parseSize = parseSize;
5
5
  const units = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
6
6
  const sizeRegex = new RegExp(`^(\\d+(?:\\.\\d+)?)\\s*(${units.join("|")})$`, "i");
7
- function formatBytes(bytes) {
7
+ function formatBytes(inBytes, sign = false) {
8
8
  let u = 0;
9
- let n = bytes;
10
- if (bytes < 0n)
11
- throw new Error(`Invalid bytes: ${bytes.toString()}`);
9
+ let n = Math.abs(inBytes);
10
+ if (n < 0n)
11
+ throw new Error(`Invalid bytes: ${n.toString()}`);
12
12
  while (n >= 1024n && ++u)
13
13
  n = n / 1024;
14
- return Number(n).toFixed(n < 10 && u > 0 ? 1 : 0) + units[u];
14
+ const result = Number(n).toFixed(n < 10 && u > 0 ? 1 : 0) + units[u];
15
+ return inBytes < 0 ? `-${result}` : `${sign ? "+" : ""}${result}`;
15
16
  }
16
17
  function parseSize(size) {
17
18
  const matches = sizeRegex.exec(size);
@@ -7,6 +7,8 @@ export type MysqlCliOptions = {
7
7
  username: string;
8
8
  verbose?: boolean;
9
9
  database?: string;
10
+ ssl?: boolean;
11
+ vars?: Record<string, string | number>;
10
12
  };
11
13
  export declare function assertDumpFile(path: string): Promise<void>;
12
14
  export declare function createMysqlCli(options: MysqlCliOptions): Promise<{
@@ -66,9 +66,11 @@ async function createMysqlCli(options) {
66
66
  const data = [
67
67
  `[client]`,
68
68
  `host = "${options.hostname}"`,
69
+ ...(options.ssl === false ? [`skip-ssl`] : []),
69
70
  ...(options.port ? [`port = "${options.port}"`] : []),
70
71
  `user = "${options.username}"`,
71
72
  `password = "${password}"`,
73
+ ...Object.entries(options.vars || {}).map(([key, value]) => `${key} = "${value}"`),
72
74
  ];
73
75
  const path = (0, path_1.join)(dir, "mysql.conf");
74
76
  await (0, promises_1.writeFile)(path, data.join("\n"));
@@ -58,8 +58,10 @@ export declare class Restic {
58
58
  private createProcess;
59
59
  exec(args: string[], options?: AsyncProcessOptions): Promise<number>;
60
60
  json<T>(args: string[], options?: AsyncProcessOptions): Promise<T>;
61
+ init(): Promise<void>;
62
+ tryInit(): Promise<boolean>;
61
63
  checkRepository(): Promise<boolean>;
62
- forget(options: {
64
+ forget<JSON extends boolean = false>(options: {
63
65
  snapshotId?: string;
64
66
  keepLast?: number;
65
67
  keepHourly?: number;
@@ -72,11 +74,22 @@ export declare class Restic {
72
74
  tag?: string[];
73
75
  prune?: boolean;
74
76
  args?: string[];
75
- }): Promise<void>;
77
+ json?: JSON;
78
+ }): Promise<[
79
+ true
80
+ ] extends [JSON] ? {
81
+ tags: any;
82
+ host: any;
83
+ paths: any;
84
+ keep: any[] | null;
85
+ remove: any[] | null;
86
+ reason: any[] | null;
87
+ }[] : string>;
76
88
  snapshots(options: {
77
89
  ids?: string[];
78
90
  tags?: string[];
79
91
  paths?: string[];
92
+ host?: string;
80
93
  latest?: number;
81
94
  json?: boolean;
82
95
  group?: ("path" | "tags" | "host")[];
@@ -96,6 +109,7 @@ export declare class Restic {
96
109
  cwd?: string;
97
110
  tags?: string[];
98
111
  paths: string[];
112
+ host?: string;
99
113
  setPaths?: string[];
100
114
  exclude?: string[];
101
115
  excludeFile?: string[];
@@ -109,15 +109,27 @@ class Restic {
109
109
  const stdout = await this.createProcess(args, options).stdout.fetch();
110
110
  return JSON.parse(stdout);
111
111
  }
112
+ async init() {
113
+ await this.exec(["init"]);
114
+ }
115
+ async tryInit() {
116
+ const exists = await this.checkRepository();
117
+ if ((0, fs_1.isLocalDir)(this.options.env.RESTIC_REPOSITORY) && !exists)
118
+ await this.init();
119
+ return exists;
120
+ }
112
121
  async checkRepository() {
113
122
  return ((await this.exec(["cat", "config"], {
114
123
  $exitCode: false,
115
124
  })) === 0);
116
125
  }
117
126
  async forget(options) {
118
- await this.exec([
127
+ const p = this.createProcess([
119
128
  "forget",
120
- ...(options.keepLast ? ["--keep-last", options.keepLast.toString()] : []),
129
+ ...(options.json ? ["--json"] : []),
130
+ ...(options.keepLast
131
+ ? ["--keep-last", options.keepLast.toString()]
132
+ : []),
121
133
  ...(options.keepHourly
122
134
  ? ["--keep-hourly", options.keepHourly.toString()]
123
135
  : []),
@@ -143,13 +155,24 @@ class Restic {
143
155
  ...(options.prune ? ["--prune"] : []),
144
156
  ...(options.args || []),
145
157
  ...(options.snapshotId ? [options.snapshotId] : []),
146
- ]);
158
+ ], {});
159
+ const stdout = await p.stdout.fetch();
160
+ if (options.json) {
161
+ if (stdout === "")
162
+ return [];
163
+ const [json] = stdout.split("\n");
164
+ return JSON.parse(json);
165
+ }
166
+ else {
167
+ return stdout;
168
+ }
147
169
  }
148
170
  async snapshots(options) {
149
171
  const json = options.json ?? true;
150
172
  return await this.json([
151
173
  "snapshots",
152
174
  ...(json ? ["--json"] : []),
175
+ ...(options.host ? ["--host", options.host] : []),
153
176
  ...(options.tags?.flatMap((tag) => [`--tag`, tag]) ?? []),
154
177
  ...(options.paths?.flatMap((path) => ["--path", path]) ?? []),
155
178
  ...(options.group ? ["--group-by", options.group.join(",")] : []),
@@ -163,6 +186,7 @@ class Restic {
163
186
  const backup = this.createProcess([
164
187
  "backup",
165
188
  "--json",
189
+ ...(options.host ? ["--host", options.host] : []),
166
190
  ...(options.exclude?.flatMap((v) => ["-e", v]) ?? []),
167
191
  ...(options.excludeFile?.flatMap((v) => ["--exclude-file", v]) ?? []),
168
192
  ...(options.tags?.flatMap((v) => ["--tag", v]) ?? []),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@datatruck/cli",
3
- "version": "0.41.5",
3
+ "version": "0.41.7",
4
4
  "description": "Tool for creating and managing backups",
5
5
  "homepage": "https://github.com/swordev/datatruck#readme",
6
6
  "bugs": {