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, MeasureDuration,
fill, fill,
decodeLocationFromPrefix, decodeLocationFromPrefix,
hash, checkIfDockerVolumeExistsOrFail, checkIfDockerVolumeExistsOrFail,
getPathFromVolume,
} from './utils' } from './utils'
@ -33,7 +34,7 @@ export const backupFromFilesystem = (from: string, location: Location, backend:
} }
export const backupFromVolume = (volume: string, location: Location, backend: Backend) => { export const backupFromVolume = (volume: string, location: Location, backend: Backend) => {
const tmp = pathRelativeToConfigFile(hash(volume)) const tmp = getPathFromVolume(volume)
try { try {
mkdirSync(tmp) mkdirSync(tmp)
checkIfDockerVolumeExistsOrFail(volume) checkIfDockerVolumeExistsOrFail(volume)

View File

@ -2,6 +2,7 @@ import { Writer } from 'clitastic'
import { config, VERBOSE } from './autorestic' import { config, VERBOSE } from './autorestic'
import { getEnvFromBackend } from './backend' import { getEnvFromBackend } from './backend'
import { LocationFromPrefixes } from './config'
import { Locations, Location, Flags } from './types' import { Locations, Location, Flags } from './types'
import { import {
exec, exec,
@ -9,7 +10,7 @@ import {
pathRelativeToConfigFile, pathRelativeToConfigFile,
getFlagsFromLocation, getFlagsFromLocation,
makeArrayIfIsNot, makeArrayIfIsNot,
fill, fill, decodeLocationFromPrefix, getPathFromVolume,
} from './utils' } 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 writer = new Writer(base + 'Removing old snapshots… ⏳')
const backend = config.backends[to] const backend = config.backends[to]
const path = pathRelativeToConfigFile(location.from)
const flags = getFlagsFromLocation(location, 'forget') 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) { if (flags.length == 0) {
writer.done(base + 'Skipping, no policy declared') writer.done(base + 'Skipping, no policy declared')
return return

View File

@ -9,9 +9,9 @@ import {
checkIfDockerVolumeExistsOrFail, checkIfDockerVolumeExistsOrFail,
ConfigError, ConfigError,
decodeLocationFromPrefix, decodeLocationFromPrefix,
exec, execPlain, exec,
hash, execPlain,
pathRelativeToConfigFile, getPathFromVolume,
} from './utils' } from './utils'
@ -25,8 +25,7 @@ export const restoreToFilesystem = (from: string, to: string, backend: Backend)
} }
export const restoreToVolume = (volume: string, backend: Backend) => { export const restoreToVolume = (volume: string, backend: Backend) => {
const tmp = pathRelativeToConfigFile(hash(volume)) const tmp = getPathFromVolume(volume)
try { try {
restoreToFilesystem(tmp, tmp, backend) restoreToFilesystem(tmp, tmp, backend)
try { 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 hash = (plain: string): string => createHash('sha1').update(plain).digest().toString('hex')
export const getPathFromVolume = (volume: string) => pathRelativeToConfigFile(hash(volume))
export const checkIfDockerVolumeExistsOrFail = (volume: string) => { export const checkIfDockerVolumeExistsOrFail = (volume: string) => {
const cmd = exec('docker', [ const cmd = exec('docker', [
'volume', 'inspect', volume, 'volume', 'inspect', volume,
@ -195,3 +197,4 @@ export const checkIfDockerVolumeExistsOrFail = (volume: string) => {
if (cmd.err.length > 0) if (cmd.err.length > 0)
throw new Error('Volume not found') throw new Error('Volume not found')
} }