1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-15 09:04:04 +02:00
joplin/CliClient/app/command-tag.js

56 lines
1.7 KiB
JavaScript
Raw Normal View History

const { BaseCommand } = require('./base-command.js');
const { app } = require('./app.js');
const { _ } = require('lib/locale.js');
2017-12-14 20:12:14 +02:00
const Tag = require('lib/models/Tag.js');
const BaseModel = require('lib/BaseModel.js');
2017-07-10 22:03:46 +02:00
class Command extends BaseCommand {
usage() {
2017-08-04 18:02:43 +02:00
return 'tag <tag-command> [tag] [note]';
2017-07-10 22:03:46 +02:00
}
description() {
2017-08-04 18:02:43 +02:00
return _('<tag-command> can be "add", "remove" or "list" to assign or remove [tag] from [note], or to list the notes associated with [tag]. The command `tag list` can be used to list all the tags.');
2017-07-10 22:03:46 +02:00
}
async action(args) {
let tag = null;
if (args.tag) tag = await app().loadItem(BaseModel.TYPE_TAG, args.tag);
2017-07-11 20:17:23 +02:00
let notes = [];
if (args.note) {
notes = await app().loadItems(BaseModel.TYPE_NOTE, args.note);
}
2017-07-10 22:03:46 +02:00
2017-08-04 18:02:43 +02:00
const command = args['tag-command'];
2017-07-10 22:03:46 +02:00
2017-08-04 18:02:43 +02:00
if (command == 'remove' && !tag) throw new Error(_('Cannot find "%s".', args.tag));
if (command == 'add') {
2017-07-18 20:21:03 +02:00
if (!notes.length) throw new Error(_('Cannot find "%s".', args.note));
2017-11-12 01:13:14 +02:00
if (!tag) tag = await Tag.save({ title: args.tag }, { userSideValidation: true });
2017-07-11 20:17:23 +02:00
for (let i = 0; i < notes.length; i++) {
await Tag.addNote(tag.id, notes[i].id);
}
2017-08-04 18:02:43 +02:00
} else if (command == 'remove') {
2017-07-18 20:21:03 +02:00
if (!tag) throw new Error(_('Cannot find "%s".', args.tag));
if (!notes.length) throw new Error(_('Cannot find "%s".', args.note));
2017-07-11 20:17:23 +02:00
for (let i = 0; i < notes.length; i++) {
await Tag.removeNote(tag.id, notes[i].id);
}
2017-08-04 18:02:43 +02:00
} else if (command == 'list') {
2017-07-10 22:03:46 +02:00
if (tag) {
let notes = await Tag.notes(tag.id);
2017-10-07 18:30:27 +02:00
notes.map((note) => { this.stdout(note.title); });
2017-07-10 22:03:46 +02:00
} else {
let tags = await Tag.all();
2017-10-07 18:30:27 +02:00
tags.map((tag) => { this.stdout(tag.title); });
2017-07-10 22:03:46 +02:00
}
} else {
2017-08-04 18:02:43 +02:00
throw new Error(_('Invalid command: "%s"', command));
2017-07-10 22:03:46 +02:00
}
}
}
module.exports = Command;