Skip to content

Commit

Permalink
Merge pull request #147 from cozy/feat/ver-331
Browse files Browse the repository at this point in the history
feat: Improve generateFiles command to create qualified files
  • Loading branch information
Merkur39 authored Mar 12, 2024
2 parents f4b2de8 + 92a1d40 commit 4594ea2
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 16 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -39,7 +40,7 @@ Options:
Commands:
import <filepath> [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 <directoryPath> 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] <doctypes...> Deletes all documents of the provided doctypes. For real.
export <doctypes> [filename] Exports data from the doctypes (separated by commas) to filename
downloadFile <fileid> Download the file
Expand Down Expand Up @@ -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 <id of directory> # 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)

Expand Down
34 changes: 28 additions & 6 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,31 @@ 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,
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(path, parseInt(filesCount))
await ach.createFiles({
filesCount: parseInt(filesCount),
dirId,
qualify,
mime
})
}

const handleDropCommand = async args => {
Expand Down Expand Up @@ -321,15 +340,18 @@ program
)

program
.command('generateFiles [path] [filesCount]')
.command('generateFiles [filesCount] [dirId]')
.option('-q, --qualify', 'Add qualification to the files')
.option('-m, --mime <s>', 'Create files with this mime type')
.description('Generates a given number of small files.')
.action(
handleErrors(async function(path, filesCount) {
handleErrors(async function(filesCount, dirId, options) {
await handleGenerateFilesCommand({
url: program.url,
token: program.token,
path,
filesCount
filesCount,
dirId,
...options
})
})
)
Expand Down
2 changes: 1 addition & 1 deletion libs/ACH.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const methods = {
},
createFiles: {
method: createFiles,
oldClient: true
oldClient: false
},
export: {
method: exportDocs,
Expand Down
32 changes: 26 additions & 6 deletions libs/createFiles.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
const crypto = require('crypto')
const ProgressBar = require('progress')
const faker = require('faker')

module.exports = async (client, path, filesCount) => {
const { forceCreateByPath } = client.files
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, qualify, mime }) => {
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: mime,
...(qualify ? { metadata: addQualification() } : {}),
conflictStrategy: 'rename'
})
bar.tick()
}
Expand Down

0 comments on commit 4594ea2

Please sign in to comment.