remove duplicate code

This commit is contained in:
cupcakearmy 2019-12-05 00:23:49 +01:00
parent 1c6a061dd1
commit 563d4ffb96
3 changed files with 9 additions and 10 deletions

View File

@ -3,7 +3,7 @@ import { resolve } from 'path'
import yaml from 'js-yaml' import yaml from 'js-yaml'
import { flags } from './autorestic' import { flags } from './autorestic'
import { Backend, Config } from './types' import { Backend, Config } from './types'
import { makeObjectKeysLowercase, rand } from './utils' import { makeArrayIfIsNot, makeObjectKeysLowercase, rand } from './utils'
import { homedir } from 'os' import { homedir } from 'os'
@ -48,8 +48,8 @@ export const normalizeAndCheckBackups = (config: Config) => {
`The backup "${name}" is missing some required attributes`, `The backup "${name}" is missing some required attributes`,
) )
if (Array.isArray(to)) for (const t of to) checkDestination(t, name) for (const t of makeArrayIfIsNot(to))
else checkDestination(to, name) checkDestination(t, name)
} }
} }

View File

@ -3,7 +3,7 @@ import { Writer } from 'clitastic'
import { config, VERBOSE } from './autorestic' import { config, VERBOSE } from './autorestic'
import { getEnvFromBackend } from './backend' import { getEnvFromBackend } from './backend'
import { Locations, Location, Flags } from './types' import { Locations, Location, Flags } from './types'
import { exec, ConfigError, pathRelativeToConfigFile, getFlagsFromLocation } from './utils' import { exec, ConfigError, pathRelativeToConfigFile, getFlagsFromLocation, makeArrayIfIsNot } from './utils'
@ -38,7 +38,7 @@ export const forgetLocation = (name: string, backup: Location, dryRun: boolean)
const filler = new Array(name.length + 3).fill(' ').join('') const filler = new Array(name.length + 3).fill(' ').join('')
let first = true let first = true
for (const t of Array.isArray(backup.to) ? backup.to : [backup.to]) { for (const t of makeArrayIfIsNot(backup.to)) {
const nameOrBlankSpaces: string = first ? display : filler const nameOrBlankSpaces: string = first ? display : filler
forgetSingle(nameOrBlankSpaces, t, backup, dryRun) forgetSingle(nameOrBlankSpaces, t, backup, dryRun)
if (first) first = false if (first) first = false

View File

@ -8,15 +8,14 @@ import { config, INSTALL_DIR, VERSION } from './autorestic'
import { checkAndConfigureBackends, getBackendsFromLocations, getEnvFromBackend } from './backend' import { checkAndConfigureBackends, getBackendsFromLocations, getEnvFromBackend } from './backend'
import { backupAll } from './backup' import { backupAll } from './backup'
import { forgetAll } from './forget' import { forgetAll } from './forget'
import { Backend, Backends, Flags, Locations } from './types' import { Backends, Flags, Locations } from './types'
import { import {
checkIfCommandIsAvailable, checkIfCommandIsAvailable,
checkIfResticIsAvailable, checkIfResticIsAvailable,
downloadFile, downloadFile,
exec, exec,
filterObjectByKey, filterObjectByKey,
singleToArray, ConfigError, makeArrayIfIsNot,
ConfigError,
} from './utils' } from './utils'
@ -35,7 +34,7 @@ const parseBackend = (flags: Flags): Backends => {
) )
if (flags.all) return config.backends if (flags.all) return config.backends
else { else {
const backends = singleToArray<string>(flags.backend) const backends = makeArrayIfIsNot<string>(flags.backend)
for (const backend of backends) for (const backend of backends)
if (!config.backends[backend]) if (!config.backends[backend])
throw new Error('Invalid backend: '.red + backend) throw new Error('Invalid backend: '.red + backend)
@ -55,7 +54,7 @@ const parseLocations = (flags: Flags): Locations => {
if (flags.all) { if (flags.all) {
return config.locations return config.locations
} else { } else {
const locations = singleToArray<string>(flags.location) const locations = makeArrayIfIsNot<string>(flags.location)
for (const location of locations) for (const location of locations)
if (!config.locations[location]) if (!config.locations[location])
throw new Error('Invalid location: '.red + location) throw new Error('Invalid location: '.red + location)