Skip to content

Commit

Permalink
fix(cli): apply include/exclude pattern filter
Browse files Browse the repository at this point in the history
  • Loading branch information
juanrgm committed Jan 30, 2024
1 parent a396784 commit 7898c13
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 125 deletions.
5 changes: 5 additions & 0 deletions .changeset/old-jobs-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@datatruck/cli": patch
---

Fix filters
16 changes: 6 additions & 10 deletions packages/cli/src/actions/InitAction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { filterRepositoryByEnabled } from "../utils/datatruck/config";
import type { Config } from "../utils/datatruck/config-type";
import { createRepo } from "../utils/datatruck/repository";
import { createPatternFilter } from "../utils/string";
import { IfRequireKeys } from "../utils/ts";

export type InitActionOptions = {
Expand All @@ -23,18 +24,13 @@ export class InitAction<TRequired extends boolean = true> {
error: Error | null;
}[] = [];

const filterRepo = createPatternFilter(this.options.repositoryNames);
const filterRepoType = createPatternFilter(this.options.repositoryTypes);

for (const repoConfig of this.config.repositories) {
if (!filterRepositoryByEnabled(repoConfig, "init")) continue;
if (
this.options.repositoryNames &&
!this.options.repositoryNames.includes(repoConfig.name)
)
continue;
if (
this.options.repositoryTypes &&
!this.options.repositoryTypes.includes(repoConfig.type)
)
continue;
if (!filterRepo(repoConfig.name)) continue;
if (!filterRepoType(repoConfig.type)) continue;
const repo = createRepo(repoConfig, this.options.verbose);
let initError: Error | null = null;

Expand Down
16 changes: 5 additions & 11 deletions packages/cli/src/actions/RestoreAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { ensureFreeDiskSpace, initEmptyDir } from "../utils/fs";
import { Listr3, Listr3TaskResultEnd } from "../utils/list";
import { Progress, ProgressManager, ProgressMode } from "../utils/progress";
import { StdStreams } from "../utils/stream";
import { createPatternFilter } from "../utils/string";
import { GargabeCollector, ensureFreeDiskTempSpace } from "../utils/temp";
import { IfRequireKeys } from "../utils/ts";
import { SnapshotsAction } from "./SnapshotsAction";
Expand Down Expand Up @@ -58,19 +59,12 @@ export class RestoreAction<TRequired extends boolean = true> {

protected async findSnapshots() {
const result: RestoreSnapshot[] = [];
const filterRepo = createPatternFilter(this.options.repositoryNames);
const filterRepoType = createPatternFilter(this.options.repositoryTypes);

for (const repository of this.config.repositories) {
if (
this.options.repositoryNames &&
!this.options.repositoryNames.includes(repository.name)
)
continue;

if (
this.options.repositoryTypes &&
!this.options.repositoryTypes.includes(repository.type)
)
continue;
if (!filterRepo(repository.name)) continue;
if (!filterRepoType(repository.type)) continue;

const snapshotsAction = new SnapshotsAction<false>(this.config, {
repositoryNames: [repository.name],
Expand Down
15 changes: 5 additions & 10 deletions packages/cli/src/actions/SnapshotsAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
} from "../utils/datatruck/config-type";
import { createAndInitRepo } from "../utils/datatruck/repository";
import { groupAndFilter } from "../utils/datatruck/snapshot";
import { createPatternFilter } from "../utils/string";
import { IfRequireKeys } from "../utils/ts";

export type SnapshotGroupByType = keyof Pick<
Expand Down Expand Up @@ -46,18 +47,12 @@ export class SnapshotsAction<TRequired extends boolean = true> {
async exec(sourceAction?: RepositoryConfigEnabledAction) {
if (!sourceAction) sourceAction = "snapshots";
let result: ExtendedSnapshot[] = [];
const filterRepo = createPatternFilter(this.options.repositoryNames);
const filterRepoType = createPatternFilter(this.options.repositoryTypes);
for (const repoConfig of this.config.repositories) {
if (!filterRepositoryByEnabled(repoConfig, sourceAction)) continue;
if (
this.options.repositoryNames &&
!this.options.repositoryNames.includes(repoConfig.name)
)
continue;
if (
this.options.repositoryTypes &&
!this.options.repositoryTypes.includes(repoConfig.type)
)
continue;
if (!filterRepo(repoConfig.name)) continue;
if (!filterRepoType(repoConfig.type)) continue;
const repo = await createAndInitRepo(repoConfig, this.options.verbose);
const configPackageNames = this.config.packages.map((pkg) => pkg.name);

Expand Down
16 changes: 6 additions & 10 deletions packages/cli/src/repositories/DatatruckRepository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { logExec } from "../utils/cli";
import { calcFileHash } from "../utils/crypto";
import { createFs } from "../utils/datatruck/client";
import { createPkgFilter, createTaskFilter } from "../utils/datatruck/config";
import { BackupPathsOptions, parseBackupPaths } from "../utils/datatruck/paths";
import { AppError } from "../utils/error";
import {
Expand All @@ -12,7 +13,7 @@ import {
} from "../utils/fs";
import { progressPercent } from "../utils/math";
import { ProgressStats } from "../utils/progress";
import { checkMatch, match, makePathPatterns } from "../utils/string";
import { match } from "../utils/string";
import {
extractTar,
createTar,
Expand All @@ -32,7 +33,6 @@ import {
} from "./RepositoryAbstract";
import { ok } from "assert";
import { rm, stat } from "fs/promises";
import { isMatch } from "micromatch";
import { basename, join, resolve } from "path";

export type MetaData = {
Expand Down Expand Up @@ -135,18 +135,14 @@ export class DatatruckRepository extends RepositoryAbstract<DatatruckRepositoryC

const snapshots: Snapshot[] = [];
const snapshotNames = await fs.readdir(".");
const packagePatterns = makePathPatterns(data.options.packageNames);
const taskPatterns = makePathPatterns(data.options.packageTaskNames);
const filterPkg = createPkgFilter(data.options.packageNames);
const filterTask = createTaskFilter(data.options.packageTaskNames);

for (const snapshotName of snapshotNames) {
const snapshotNameData =
DatatruckRepository.parseSnapshotName(snapshotName);
if (!snapshotNameData) continue;
if (
packagePatterns &&
!isMatch(snapshotNameData.packageName, packagePatterns)
)
continue;
if (!filterPkg(snapshotNameData.packageName)) continue;

if (
data.options.ids &&
Expand All @@ -162,7 +158,7 @@ export class DatatruckRepository extends RepositoryAbstract<DatatruckRepositoryC

if (!meta) continue;

if (taskPatterns && !checkMatch(meta.task, taskPatterns)) continue;
if (!filterTask(meta.task)) continue;

if (
data.options.ids &&
Expand Down
16 changes: 6 additions & 10 deletions packages/cli/src/repositories/GitRepository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { logExec } from "../utils/cli";
import { createPkgFilter, createTaskFilter } from "../utils/datatruck/config";
import { BackupPathsOptions, parseBackupPaths } from "../utils/datatruck/paths";
import {
fastFolderSizeAsync,
Expand All @@ -8,7 +9,6 @@ import {
parsePackageFile,
} from "../utils/fs";
import { Git } from "../utils/git";
import { checkMatch, makePathPatterns } from "../utils/string";
import { mkTmpDir, tmpDir } from "../utils/temp";
import {
RepositoryAbstract,
Expand All @@ -24,7 +24,6 @@ import {
} from "./RepositoryAbstract";
import fg from "fast-glob";
import { copyFile, rm, mkdir } from "fs/promises";
import { isMatch } from "micromatch";
import { join, dirname } from "path";

export type GitRepositoryConfig = {
Expand Down Expand Up @@ -142,28 +141,25 @@ export class GitRepository extends RepositoryAbstract<GitRepositoryConfig> {
log: data.options.verbose,
});

const pkgPatterns = makePathPatterns(data.options.packageNames);
const pkgTaskPatterns = makePathPatterns(data.options.packageTaskNames);

await git.clone({ repo: this.config.repo });

const tagNames = data.options.ids?.map(
(id) => `${GitRepository.refPrefix}/*/${id}*`,
) || [`${GitRepository.refPrefix}/*`];
const tags = await git.getTags(tagNames);

const filterPkg = createPkgFilter(data.options.packageNames);
const filterTask = createTaskFilter(data.options.packageTaskNames);

return tags
.reduce((result, tag) => {
const parsedTag = tag.message
? GitRepository.parseSnapshotTag(tag.name, tag.message)
: null;
if (!parsedTag) return result;

if (pkgPatterns && !isMatch(parsedTag.package, pkgPatterns))
return result;

if (pkgTaskPatterns && !checkMatch(parsedTag.task, pkgTaskPatterns))
return result;
if (!filterPkg(parsedTag.package)) return result;
if (!filterTask(parsedTag.task)) return result;

if (
data.options.tags &&
Expand Down
15 changes: 7 additions & 8 deletions packages/cli/src/repositories/ResticRepository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { logExec } from "../utils/cli";
import { createPkgFilter, createTaskFilter } from "../utils/datatruck/config";
import { BackupPathsOptions, parseBackupPaths } from "../utils/datatruck/paths";
import { AppError } from "../utils/error";
import {
Expand All @@ -11,7 +12,7 @@ import {
import { progressPercent } from "../utils/math";
import { Progress } from "../utils/progress";
import { ResticRepositoryUri, Restic } from "../utils/restic";
import { checkMatch, formatUri, makePathPatterns } from "../utils/string";
import { formatUri } from "../utils/string";
import { mkTmpDir } from "../utils/temp";
import {
RepositoryAbstract,
Expand All @@ -27,7 +28,6 @@ import {
} from "./RepositoryAbstract";
import FastGlob from "fast-glob";
import { writeFile } from "fs/promises";
import { isMatch } from "micromatch";
import { join, resolve } from "path";

export type ResticRepositoryConfig = {
Expand Down Expand Up @@ -116,8 +116,6 @@ export class ResticRepository extends RepositoryAbstract<ResticRepositoryConfig>
env: await this.buildEnv(),
log: data.options.verbose,
});
const packagePatterns = makePathPatterns(data.options.packageNames);
const taskNamePatterns = makePathPatterns(data.options.packageTaskNames);
const result = await restic.snapshots({
json: true,
tags: [
Expand All @@ -129,13 +127,14 @@ export class ResticRepository extends RepositoryAbstract<ResticRepositoryConfig>
) ?? []),
],
});
const filterPkg = createPkgFilter(data.options.packageNames);
const filterTask = createTaskFilter(data.options.packageTaskNames);

return result.reduce((items, item) => {
const tag = ResticRepository.parseSnapshotTags(item.tags ?? []);
if (!tag.id) return items;
if (packagePatterns && !isMatch(tag.package, packagePatterns))
return items;
if (taskNamePatterns && !checkMatch(tag.task, taskNamePatterns))
return items;
if (!filterPkg(tag.package)) return items;
if (!filterTask(tag.task)) return items;
const itemTags = tag.tags ?? [];
if (data.options.tags && !itemTags.some((t) => itemTags.includes(t)))
return items;
Expand Down
11 changes: 5 additions & 6 deletions packages/cli/src/tasks/MssqlTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { AsyncProcess } from "../utils/async-process";
import { resolveDatabaseName } from "../utils/datatruck/config";
import { AppError } from "../utils/error";
import { readDir } from "../utils/fs";
import { createPatternFilter } from "../utils/string";
import { mkTmpDir } from "../utils/temp";
import { TaskBackupData, TaskRestoreData, TaskAbstract } from "./TaskAbstract";
import { readFile } from "fs/promises";
import { isMatch } from "micromatch";
import { join } from "path";

export type MssqlTaskConfig = {
Expand Down Expand Up @@ -80,11 +80,10 @@ export class MssqlTask extends TaskAbstract<MssqlTaskConfig> {
);

const databaseNames = (await this.fetchDatabaseNames()).filter(
(databaseName) =>
(!this.config.includeDatabases ||
isMatch(databaseName, this.config.includeDatabases)) &&
(!this.config.excludeDatabases ||
!isMatch(databaseName, this.config.excludeDatabases)),
createPatternFilter({
include: this.config.includeDatabases,
exclude: this.config.excludeDatabases,
}),
);

for (const databaseName of databaseNames) {
Expand Down
15 changes: 7 additions & 8 deletions packages/cli/src/tasks/SqlDumpTaskAbstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
readDir,
} from "../utils/fs";
import { progressPercent } from "../utils/math";
import { createPatternFilter } from "../utils/string";
import { mkTmpDir } from "../utils/temp";
import {
TaskBackupData,
Expand All @@ -17,7 +18,6 @@ import {
} from "./TaskAbstract";
import { ok } from "assert";
import { mkdir, readFile } from "fs/promises";
import { isMatch } from "micromatch";
import { join } from "path";

export type TargetDatabase = {
Expand Down Expand Up @@ -120,13 +120,12 @@ export abstract class SqlDumpTaskAbstract<

const config = this.config;
const allTableNames = await this.onFetchTableNames(this.config.database);
const tableNames = allTableNames.filter((tableName) => {
if (config.includeTables && !isMatch(tableName, config.includeTables))
return false;
if (config.excludeTables && isMatch(tableName, config.excludeTables))
return false;
return true;
});
const tableNames = allTableNames.filter(
createPatternFilter({
include: config.includeTables,
exclude: config.excludeTables,
}),
);

ok(typeof snapshotPath === "string");

Expand Down
Loading

0 comments on commit 7898c13

Please sign in to comment.