From 886b6362cd67c4f6beea22da33fd0eb7fb0dcc0c Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Tue, 24 Dec 2019 17:31:44 +0100 Subject: [PATCH] remove duplicated code and make the forget function compatible with the new docker mounts options --- src/backup.ts | 5 +++-- src/forget.ts | 15 +++++++++++++-- src/restore.ts | 9 ++++----- src/utils.ts | 5 ++++- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/backup.ts b/src/backup.ts index 6c7ada0..0f0b78e 100644 --- a/src/backup.ts +++ b/src/backup.ts @@ -15,7 +15,8 @@ import { MeasureDuration, fill, decodeLocationFromPrefix, - hash, checkIfDockerVolumeExistsOrFail, + checkIfDockerVolumeExistsOrFail, + getPathFromVolume, } from './utils' @@ -33,7 +34,7 @@ export const backupFromFilesystem = (from: string, location: Location, backend: } export const backupFromVolume = (volume: string, location: Location, backend: Backend) => { - const tmp = pathRelativeToConfigFile(hash(volume)) + const tmp = getPathFromVolume(volume) try { mkdirSync(tmp) checkIfDockerVolumeExistsOrFail(volume) diff --git a/src/forget.ts b/src/forget.ts index 8f19bed..5dedd8b 100644 --- a/src/forget.ts +++ b/src/forget.ts @@ -2,6 +2,7 @@ import { Writer } from 'clitastic' import { config, VERBOSE } from './autorestic' import { getEnvFromBackend } from './backend' +import { LocationFromPrefixes } from './config' import { Locations, Location, Flags } from './types' import { exec, @@ -9,7 +10,7 @@ import { pathRelativeToConfigFile, getFlagsFromLocation, makeArrayIfIsNot, - fill, + fill, decodeLocationFromPrefix, getPathFromVolume, } from './utils' @@ -20,9 +21,19 @@ export const forgetSingle = (name: string, to: string, location: Location, dryRu const writer = new Writer(base + 'Removing old snapshots… ⏳') const backend = config.backends[to] - const path = pathRelativeToConfigFile(location.from) const flags = getFlagsFromLocation(location, 'forget') + const [type, value] = decodeLocationFromPrefix(location.from) + let path: string + switch (type) { + case LocationFromPrefixes.Filesystem: + path = pathRelativeToConfigFile(value) + break + case LocationFromPrefixes.DockerVolume: + path = getPathFromVolume(value) + break + } + if (flags.length == 0) { writer.done(base + 'Skipping, no policy declared') return diff --git a/src/restore.ts b/src/restore.ts index a48ced8..dfaafb9 100644 --- a/src/restore.ts +++ b/src/restore.ts @@ -9,9 +9,9 @@ import { checkIfDockerVolumeExistsOrFail, ConfigError, decodeLocationFromPrefix, - exec, execPlain, - hash, - pathRelativeToConfigFile, + exec, + execPlain, + getPathFromVolume, } from './utils' @@ -25,8 +25,7 @@ export const restoreToFilesystem = (from: string, to: string, backend: Backend) } export const restoreToVolume = (volume: string, backend: Backend) => { - const tmp = pathRelativeToConfigFile(hash(volume)) - + const tmp = getPathFromVolume(volume) try { restoreToFilesystem(tmp, tmp, backend) try { diff --git a/src/utils.ts b/src/utils.ts index 97edea2..9195e47 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -188,10 +188,13 @@ export const decodeLocationFromPrefix = (from: string): [LocationFromPrefixes, s export const hash = (plain: string): string => createHash('sha1').update(plain).digest().toString('hex') +export const getPathFromVolume = (volume: string) => pathRelativeToConfigFile(hash(volume)) + export const checkIfDockerVolumeExistsOrFail = (volume: string) => { const cmd = exec('docker', [ 'volume', 'inspect', volume, ]) if (cmd.err.length > 0) throw new Error('Volume not found') -} \ No newline at end of file +} +