From 0aa7178d18803e175e9f7fa245f7b9a421410547 Mon Sep 17 00:00:00 2001 From: AlexisG Date: Sat, 2 Mar 2024 14:41:56 +0100 Subject: [PATCH 1/3] feat: Migrate generateFiles command with new client BREACKING CHANGE: The `path` argument is replaced by `dirId`, and goes to second position. Example to generate 2 files at the root of your Drive: Before ``` ACH generateFiles ``` Now ``` ACH generateFiles ``` --- cli.js | 10 +++++----- libs/ACH.js | 2 +- libs/createFiles.js | 24 +++++++++++++++++------- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/cli.js b/cli.js index 16d409a..913aa44 100755 --- a/cli.js +++ b/cli.js @@ -83,12 +83,12 @@ const handleImportDirCommand = async args => { } const handleGenerateFilesCommand = async args => { - const { path = '/', filesCount = 10, url, token } = args + const { dirId = 'io.cozy.files.root-dir', filesCount = 10, url, token } = args const ach = new ACH(token || autotoken(url, ['io.cozy.files']), url, [ 'io.cozy.files' ]) await ach.connect() - await ach.createFiles(path, parseInt(filesCount)) + await ach.createFiles(parseInt(filesCount), dirId) } const handleDropCommand = async args => { @@ -321,14 +321,14 @@ program ) program - .command('generateFiles [path] [filesCount]') + .command('generateFiles [filesCount] [dirId]') .description('Generates a given number of small files.') .action( - handleErrors(async function(path, filesCount) { + handleErrors(async function(filesCount, dirId) { await handleGenerateFilesCommand({ url: program.url, token: program.token, - path, + dirId, filesCount }) }) diff --git a/libs/ACH.js b/libs/ACH.js index bf5382c..1fed1b6 100644 --- a/libs/ACH.js +++ b/libs/ACH.js @@ -121,7 +121,7 @@ const methods = { }, createFiles: { method: createFiles, - oldClient: true + oldClient: false }, export: { method: exportDocs, diff --git a/libs/createFiles.js b/libs/createFiles.js index 764f7d1..c82f224 100644 --- a/libs/createFiles.js +++ b/libs/createFiles.js @@ -1,16 +1,26 @@ const ProgressBar = require('progress') const faker = require('faker') +const { + uploadFileWithConflictStrategy +} = require('cozy-client/dist/models/file') -module.exports = async (client, path, filesCount) => { - const { forceCreateByPath } = client.files - +module.exports = async ( + client, + filesCount, + dirId = 'io.cozy.files.root-dir' +) => { const bar = new ProgressBar(':bar', { total: filesCount }) for (let i = 0; i < filesCount; i++) { - const name = faker.datatype.uuid() - await forceCreateByPath(`${path}/${name}.txt`, name, { - name: `${name}.txt`, - contentType: 'text/plain' + const name = faker.lorem.word() + const content = faker.lorem.paragraph() + const buffer = new Buffer.from(content, 'utf-8') + + await uploadFileWithConflictStrategy(client, buffer.toString(), { + name, + dirId, + contentType: 'text/plain', + conflictStrategy: 'rename' }) bar.tick() } From 85e685f08e8d36e99fb14954048c0aba34c6dd22 Mon Sep 17 00:00:00 2001 From: Alexis G Date: Mon, 11 Mar 2024 19:42:47 +0100 Subject: [PATCH 2/3] docs: Fix links & add generateFiles command --- README.md | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8eaa41f..a24d9f1 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,9 @@ Automated Cozy Hydrater (ACH *[ax]*) is a CLI that lets you **request, create an + [Import data](#import-data) + [Import repositories with files](#import-repositories-with-files) + [Create photo albums with ACH](#create-photo-albums-with-ach) - + [Export data keeping the ids](#export-data-keeping-the-ids) - + [How to export all data from a konnector](#how-to-export-all-data-from-a-konnector) + + [Export data removing the ids](#export-data-removing-the-ids) + + [Import data from a CSV](#Import-data-from-a-CSV) + + [Generate some files](#Generate-some-files) + [Generate some qualified files with referenced contacts (mespapiers)](#Generate-some-papers) ## Install @@ -39,7 +40,7 @@ Options: Commands: import [handlebarsOptionsFile] The file containing the JSON data to import. Defaults to "example-data.json". If the file doesn't exist in the application, ACH will try to find it inside its data folder. Then the dummy helpers JS file (optional). importDir The path to the directory content to import. Defaults to "./DirectoriesToInject". - generateFiles [path] [filesCount] Generates a given number of small files. + generateFiles [filesCount] [dirId] Generates a given number of small files. (optional dirId) drop [options] Deletes all documents of the provided doctypes. For real. export [filename] Exports data from the doctypes (separated by commas) to filename downloadFile Download the file @@ -228,6 +229,17 @@ environment variable `ACH_KEEP_REV`. See the [example](./examples/data-from-csv/README.md). +### Generate some files + +To create files into a Cozy, use the `generateFiles` command: + +```shell +$ ACH generateFiles 10 # by default will be at the root of your Drive +``` +Options: +- `-q`/`--qualify`: In order to have a random qualification on your files +- `-m`/`--mime`: In order to change the default mime of files (`text/plain`) + ### Generate some papers (usefull for MesPapiers) From 92a1d409d98c1628aaa61b4bc4155781067879d9 Mon Sep 17 00:00:00 2001 From: Alexis G Date: Fri, 8 Mar 2024 13:28:17 +0100 Subject: [PATCH 3/3] feat: Add options to generateFiles command --- cli.js | 30 ++++++++++++++++++++++++++---- libs/createFiles.js | 22 ++++++++++++++++------ 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/cli.js b/cli.js index 913aa44..5759356 100755 --- a/cli.js +++ b/cli.js @@ -83,12 +83,31 @@ const handleImportDirCommand = async args => { } const handleGenerateFilesCommand = async args => { - const { dirId = 'io.cozy.files.root-dir', filesCount = 10, url, token } = args + const { + dirId = 'io.cozy.files.root-dir', + filesCount = 10, + url, + token, + qualify, + mime = 'text/plain' + } = args + + if (qualify && !token) { + log.warn( + 'To view files in MyPapers, an app/konnector token is required to create qualified files.\nThis token allows you to have the `cozyMetadata.createdByApp` prop on files.' + ) + } + const ach = new ACH(token || autotoken(url, ['io.cozy.files']), url, [ 'io.cozy.files' ]) await ach.connect() - await ach.createFiles(parseInt(filesCount), dirId) + await ach.createFiles({ + filesCount: parseInt(filesCount), + dirId, + qualify, + mime + }) } const handleDropCommand = async args => { @@ -322,14 +341,17 @@ program program .command('generateFiles [filesCount] [dirId]') + .option('-q, --qualify', 'Add qualification to the files') + .option('-m, --mime ', 'Create files with this mime type') .description('Generates a given number of small files.') .action( - handleErrors(async function(filesCount, dirId) { + handleErrors(async function(filesCount, dirId, options) { await handleGenerateFilesCommand({ url: program.url, token: program.token, + filesCount, dirId, - filesCount + ...options }) }) ) diff --git a/libs/createFiles.js b/libs/createFiles.js index c82f224..adbdfce 100644 --- a/libs/createFiles.js +++ b/libs/createFiles.js @@ -1,14 +1,23 @@ +const crypto = require('crypto') const ProgressBar = require('progress') const faker = require('faker') + const { uploadFileWithConflictStrategy } = require('cozy-client/dist/models/file') +const { Qualification } = require('cozy-client/dist/models/document') +const { + qualifications +} = require('cozy-client/dist/assets/qualifications.json') + +const addQualification = () => { + const { label } = qualifications[crypto.randomInt(0, qualifications.length)] + const qualification = Qualification.getByLabel(label) + + return { qualification } +} -module.exports = async ( - client, - filesCount, - dirId = 'io.cozy.files.root-dir' -) => { +module.exports = async (client, { filesCount, dirId, qualify, mime }) => { const bar = new ProgressBar(':bar', { total: filesCount }) for (let i = 0; i < filesCount; i++) { @@ -19,7 +28,8 @@ module.exports = async ( await uploadFileWithConflictStrategy(client, buffer.toString(), { name, dirId, - contentType: 'text/plain', + contentType: mime, + ...(qualify ? { metadata: addQualification() } : {}), conflictStrategy: 'rename' }) bar.tick()