-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Ajout d'une commande cli pour vérifier l'existance de fichiers S3
- Loading branch information
1 parent
4fcb985
commit 11a045a
Showing
5 changed files
with
202 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { Command, Flags } from '@oclif/core'; | ||
import { z } from 'zod'; | ||
|
||
import { fileExists } from '@potentiel-libraries/file-storage'; | ||
|
||
import { parseCsvFile, csvFlags } from '../../helpers/parse-file'; | ||
import { makeReporter, reporterFlags } from '../../helpers/reporter'; | ||
|
||
const schema = z.object({ | ||
from: z.string(), | ||
to: z.string().optional(), | ||
}); | ||
|
||
export default class FilesRename extends Command { | ||
static override description = 'Check existance of a list of files from a CSV'; | ||
static override examples: Command.Example[] = [ | ||
{ | ||
command: "npx cli files check --path ./file.csv --delimiter ','", | ||
description: 'change the delimiter to ,', | ||
}, | ||
{ | ||
command: "npx cli files check --path ./file.csv --delimiter 'utf8'", | ||
description: 'change the encoding to utf8', | ||
}, | ||
{ | ||
command: 'npx cli files check --path ./file.csv --report out.csv --reportErrorsOnly', | ||
description: 'reports errors to out.csv', | ||
}, | ||
]; | ||
|
||
static override args = {}; | ||
|
||
static override flags = { | ||
path: Flags.file({ | ||
exists: true, | ||
char: 'p', | ||
description: 'path to the csv file containing the files to check', | ||
required: true, | ||
}), | ||
field: Flags.string({ | ||
description: 'name of the csv field containing the file to check', | ||
default: 'from', | ||
}), | ||
...csvFlags, | ||
...reporterFlags, | ||
}; | ||
|
||
public async run(): Promise<void> { | ||
const { flags } = await this.parse(FilesRename); | ||
const { parsedData: files } = await parseCsvFile(flags.path, schema, { | ||
delimiter: flags.delimiter, | ||
encoding: flags.encoding, | ||
}); | ||
|
||
const reporter = await makeReporter(flags); | ||
|
||
for (const file of files) { | ||
const field = flags.field as keyof z.infer<typeof schema>; | ||
const filename = file[field]; | ||
if (!filename) { | ||
console.info(`Missing filename`); | ||
continue; | ||
} | ||
console.info(`Checking ${filename}`); | ||
const exists = await fileExists(filename); | ||
if (exists) { | ||
await reporter.success(filename); | ||
} else { | ||
await reporter.error(filename); | ||
} | ||
} | ||
console.info(`Checked ${files.length} files`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import fs from 'fs/promises'; | ||
|
||
import { Flags } from '@oclif/core'; | ||
|
||
export const reporterFlags = { | ||
report: Flags.file({ | ||
description: 'the name of the file to report to', | ||
}), | ||
reportErrorsOnly: Flags.boolean({ | ||
description: `only outputs to the csv file`, | ||
dependsOn: ['report'], | ||
}), | ||
}; | ||
|
||
type Reporter = { | ||
success: (obj: string | Record<string, unknown>) => void | Promise<void>; | ||
error: (obj: string | Record<string, unknown>, error?: unknown) => void | Promise<void>; | ||
close: () => void | Promise<void>; | ||
}; | ||
|
||
const consoleReporter: Reporter = { | ||
success: (obj) => { | ||
console.log('✅', obj); | ||
}, | ||
error: (obj, error) => { | ||
console.log('❌', obj, error); | ||
}, | ||
close: () => {}, | ||
}; | ||
|
||
const makeFileReporter = async (filePath: string) => { | ||
// create or overwrite file | ||
await fs.writeFile(filePath, ''); | ||
const resultsFile = await fs.open(filePath, 'a'); | ||
return { | ||
report: (content: string) => resultsFile.appendFile(content + '\n'), | ||
close: () => resultsFile.close(), | ||
}; | ||
}; | ||
|
||
const makeCsvReporter = async (filePath: string, reportErrorsOnly: boolean): Promise<Reporter> => { | ||
const fileReporter = await makeFileReporter(filePath); | ||
return { | ||
success: async (value) => { | ||
if (reportErrorsOnly) { | ||
return; | ||
} | ||
await fileReporter.report(['success', value, ''].join(',')); | ||
}, | ||
error: async (value, error) => { | ||
await fileReporter.report( | ||
['error', value, (error as Error)?.message].filter(Boolean).join(','), | ||
); | ||
}, | ||
close: fileReporter.close, | ||
}; | ||
}; | ||
|
||
export const makeReporter = async ({ | ||
report, | ||
reportErrorsOnly, | ||
}: { | ||
report: string | undefined; | ||
reportErrorsOnly: boolean; | ||
}): Promise<Reporter> => { | ||
const reporters = [consoleReporter]; | ||
if (report) { | ||
reporters.push(await makeCsvReporter(report, reportErrorsOnly)); | ||
} | ||
|
||
return { | ||
success: async (obj) => { | ||
for (const reporter of reporters) { | ||
await reporter.success(obj); | ||
} | ||
}, | ||
error: async (obj, error) => { | ||
for (const reporter of reporters) { | ||
await reporter.error(obj, error); | ||
} | ||
}, | ||
close: async () => { | ||
for (const reporter of reporters) { | ||
await reporter.close(); | ||
} | ||
}, | ||
}; | ||
}; |