remove duplicated code and make the forget function compatible with the new docker mounts options

This commit is contained in:
cupcakearmy 2019-12-24 17:31:44 +01:00
parent 9ece1d867d
commit 886b6362cd
4 changed files with 24 additions and 10 deletions

View File

@ -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)

View File

@ -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

View File

@ -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 {

View File

@ -188,6 +188,8 @@ 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,
@ -195,3 +197,4 @@ export const checkIfDockerVolumeExistsOrFail = (volume: string) => {
if (cmd.err.length > 0)
throw new Error('Volume not found')
}