Skip to content

Commit fe94015

Browse files
authored
(feat): list backups added to command-line ... (#40)
* (feat): list backups added to command-line ... * (ref): applied some code formatting * (ref): startup params
1 parent 7a14913 commit fe94015

File tree

5 files changed

+60
-3
lines changed

5 files changed

+60
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
1010
},
1111
"scripts": {
12-
"start": "NODE_ENV=dev tsc && node dist/app.js backup --name test-ref --source ~/Documents/Personal --dest ./dev",
12+
"start": "NODE_ENV=dev tsc && node dist/app.js restore --ref 1bc41083-afd2-4b5e-8270-5e475844ba50 --dest ./dev/restore",
1313
"build": "NODE_ENV=production tsc",
1414
"lint": "NODE_ENV=test eslint \"{src,libs,test}/**/*.ts\" --fix",
1515
"test": "echo \"WARN: no test specified\" && exit 0"

src/app.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#! /usr/bin/env node
22

33
// Entrypoint to application
4-
import { makeBackup } from './common/commands/backup.js'
4+
import { listBackups, makeBackup } from './common/commands/backup.js'
55
import { parseArgs } from './common/commands/parsing.js'
66
import { restoreBackup } from './common/commands/restore.js'
77
import { ppRecord, sayHello } from './common/functions.js'
@@ -14,8 +14,8 @@ const cmdArgs = parseArgs()
1414

1515
const run = async () => {
1616
sayHello()
17-
// Handle: Perform all app configurations first
1817

18+
// Handle: Perform all app configurations first
1919
if (cmdArgs['_'].toString() === 'config') {
2020
const conf = AppConfig.getInstance()
2121
const options = Object.keys(cmdArgs).slice(1)
@@ -62,6 +62,13 @@ const run = async () => {
6262
log(`Backup with ID, ${res}, successfully restored to ${cmdArgs['dest']}`)
6363
break
6464
}
65+
case 'list': {
66+
const err = await listBackups(cmdArgs['name'])
67+
if (err) {
68+
log(`Could not list backups. Reason: ${err.message}`)
69+
}
70+
break
71+
}
6572
default: {
6673
log(`WARN: Invalid command specified`)
6774
process.exit(2)

src/common/commands/backup.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import ora from 'ora'
22
import { resolve } from 'path/posix'
33
import { cwd } from 'process'
44
import { BackupManager } from '../../lib/backup.js'
5+
import { DatabaseManager } from '../../lib/database.js'
56
import { log } from '../../lib/logger.js'
67
import { BackupRecord } from '../types.js'
78

@@ -27,6 +28,36 @@ export async function makeBackup(
2728
return [ null, err ]
2829
}
2930
}
31+
export async function listBackups(name?: string): Promise<Error> {
32+
const db: DatabaseManager = DatabaseManager.getInstance()
33+
const [ records, err ] = await db.findAllRecords()
34+
if (err) return err
35+
let fRecords = records
36+
37+
if (name && name.length > 0) {
38+
// Filter by name
39+
fRecords = fRecords.filter((r) => r.name.match(name))
40+
}
41+
42+
if (fRecords.length === 0) {
43+
log(`No records were found ...`)
44+
return
45+
}
46+
47+
const tableData = fRecords.map((r) => {
48+
return {
49+
id: r.id,
50+
name: r.name,
51+
created: r.created,
52+
type: r.type,
53+
size: `${Math.round(r.bytelength / 1024 / 1024)}MB`
54+
}
55+
})
56+
57+
// Print the result
58+
log(`Found a total of ${fRecords.length} record(s) matching list criteria...`)
59+
console.table(tableData)
60+
}
3061

3162
export async function differentialBackup(
3263
name: string,

src/common/commands/parsing.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ export function parseArgs():
2828
})
2929
})
3030

31+
cmd.command(
32+
'list',
33+
'displays a list of all backups that are currently known by the system. Use --name to filter backups by name',
34+
(yargs) => {
35+
return yargs.option('name', {
36+
describe: 'An optional variable used for some info commands to refine info operations',
37+
type: 'string'
38+
})
39+
}
40+
)
41+
3142
// Configure custom backups
3243
cmd.command('backup', 'performs a custom backup of a select directory(s)', (yargs) => {
3344
return yargs

src/lib/database.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ export class DatabaseManager {
4040
}
4141
}
4242

43+
async findAllRecords(): Promise<[BackupRecord[], Error]> {
44+
try {
45+
return [ this.dbClient.data.backups, null ]
46+
} catch (err) {
47+
return [ null, err ]
48+
}
49+
}
50+
4351
findRecordById(id: string, table: RecordTable = RecordTable.BACKUPS): [BackupRecord, Error] {
4452
try {
4553
let backup: BackupRecord

0 commit comments

Comments
 (0)