From 563d4ffb965449f9dd2d0c02972d41420eb970b3 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Thu, 5 Dec 2019 00:23:49 +0100 Subject: [PATCH] remove duplicate code --- src/config.ts | 6 +++--- src/forget.ts | 4 ++-- src/handlers.ts | 9 ++++----- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/config.ts b/src/config.ts index c63e3d0..71c359c 100644 --- a/src/config.ts +++ b/src/config.ts @@ -3,7 +3,7 @@ import { resolve } from 'path' import yaml from 'js-yaml' import { flags } from './autorestic' import { Backend, Config } from './types' -import { makeObjectKeysLowercase, rand } from './utils' +import { makeArrayIfIsNot, makeObjectKeysLowercase, rand } from './utils' import { homedir } from 'os' @@ -48,8 +48,8 @@ export const normalizeAndCheckBackups = (config: Config) => { `The backup "${name}" is missing some required attributes`, ) - if (Array.isArray(to)) for (const t of to) checkDestination(t, name) - else checkDestination(to, name) + for (const t of makeArrayIfIsNot(to)) + checkDestination(t, name) } } diff --git a/src/forget.ts b/src/forget.ts index 52e32f9..640a7d8 100644 --- a/src/forget.ts +++ b/src/forget.ts @@ -3,7 +3,7 @@ import { Writer } from 'clitastic' import { config, VERBOSE } from './autorestic' import { getEnvFromBackend } from './backend' 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('') 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 forgetSingle(nameOrBlankSpaces, t, backup, dryRun) if (first) first = false diff --git a/src/handlers.ts b/src/handlers.ts index 0af7f3e..59153ae 100644 --- a/src/handlers.ts +++ b/src/handlers.ts @@ -8,15 +8,14 @@ import { config, INSTALL_DIR, VERSION } from './autorestic' import { checkAndConfigureBackends, getBackendsFromLocations, getEnvFromBackend } from './backend' import { backupAll } from './backup' import { forgetAll } from './forget' -import { Backend, Backends, Flags, Locations } from './types' +import { Backends, Flags, Locations } from './types' import { checkIfCommandIsAvailable, checkIfResticIsAvailable, downloadFile, exec, filterObjectByKey, - singleToArray, - ConfigError, + ConfigError, makeArrayIfIsNot, } from './utils' @@ -35,7 +34,7 @@ const parseBackend = (flags: Flags): Backends => { ) if (flags.all) return config.backends else { - const backends = singleToArray(flags.backend) + const backends = makeArrayIfIsNot(flags.backend) for (const backend of backends) if (!config.backends[backend]) throw new Error('Invalid backend: '.red + backend) @@ -55,7 +54,7 @@ const parseLocations = (flags: Flags): Locations => { if (flags.all) { return config.locations } else { - const locations = singleToArray(flags.location) + const locations = makeArrayIfIsNot(flags.location) for (const location of locations) if (!config.locations[location]) throw new Error('Invalid location: '.red + location)