Skip to content

Commit 97efeac

Browse files
committed
feat(mite-amend): use first argument as note if it’s not numeric
1 parent e699c8e commit 97efeac

File tree

4 files changed

+91
-22
lines changed

4 files changed

+91
-22
lines changed

README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ line-separatedEase to use CLI tool for creating, listing, starting and stopping
2929
- [Stop Tracking](#Stop-Tracking)
3030
- [Modify Time-Entries](#Modify-Time-Entries)
3131
- [Change Note](#Change-Note)
32+
- [Change Service or Project](#Change-Service-or-Project)
3233
- [Un-/Lock Entry](#Un-Lock-Entry)
3334
- [Delete entry](#Delete-entry)
3435
- [Open](#Open)
@@ -56,7 +57,7 @@ Features
5657

5758
- Create and start new Entries with interactive survey-like cli interface
5859
- Delete, lock, unlock single time entries
59-
- Edit the currently running entries text for fast updating the work log
60+
- Edit the currently running entries text, service and project for quickly updating the work log
6061
- Show, filter, group time entries to show reports for last month, current week etc.
6162
- Delete single entries by id
6263
- List, sort, filter, update & delete user accounts, customers, projects & services using variable columns and cli-tables, CSV or TSV data
@@ -81,21 +82,21 @@ Options:
8182
-h, --help output usage information
8283
8384
Commands:
84-
amend|reword edit the text for a specific time entry or the currently runnning entry
85+
amend|reword edit note, service, project of a specific time entry or the currently runnning entry
8586
autocomplete install/uninstall autocompletion
8687
config show or set configuration settings
87-
customer update/delete single customer
88+
customer create/delete/list/update customer
8889
customers|clients list, filter & search customers
8990
delete|rm delete a specific time entry
9091
list|st list time entries
9192
lock lock single time entry
9293
new|create create a new time entry
9394
open open the given time entry in browser
94-
project update/delete a single project
95+
project create/delete/list/update a single project
9596
projects list, filter, archive/unarchive & search projects
96-
service update/delete single service
97+
service create/delete/list/update single service
9798
services list, filter & search services
98-
start start the tracker for the given id, will also stop already running entry
99+
start start the tracker for the given id, will also stop allready running entry
99100
stop stop any running counter
100101
unlock unlock single time entry
101102
users list, filter & search for users
@@ -362,6 +363,12 @@ You can also pipe in the note:
362363

363364
cat myVerLongNote.txt | mite amend 1234567
364365

366+
#### Change Service or Project
367+
368+
Changing the project or service id of the current or any other time entry works the same:
369+
370+
mite amend 12345678 --service-id 12834 --project-id 12938123
371+
365372
#### Un-/Lock Entry
366373

367374
Lock a single time entry

source/lib/auto-complete/completions/amend.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,25 @@ const NOTE_MAX_LENGTH = (process.stdout.columns || 80) - 20;
1717
* @param {string} env.line - the current complete input line in the cli
1818
* @returns {Promise<Array<string>>}
1919
*/
20-
module.exports = async ({ line, words }) => {
20+
module.exports = async ({ line, words, prev }) => {
21+
22+
switch (prev) {
23+
case '--project-id':
24+
return miteApi.getProjects({ archived: false }).then(
25+
projects => projects.map(c => ({
26+
name: String(c.id),
27+
description: c.name
28+
}))
29+
);
30+
case '--service-id':
31+
return miteApi.getServices({ archived: false }).then(
32+
service => service.map(c => ({
33+
name: String(c.id),
34+
description: c.name + (c.billable ? ' $' : '')
35+
}))
36+
);
37+
}
38+
2139
const defaults = [
2240
(words < 3 ? {
2341
name: '--help',
@@ -26,7 +44,15 @@ module.exports = async ({ line, words }) => {
2644
(line.indexOf('--editor') === -1 ? {
2745
name: '--editor',
2846
description: 'open $EDITOR for editing the entry’s note'
29-
} : undefined)
47+
} : undefined),
48+
{
49+
name: '--project-id',
50+
description: 'id of the project the entry should be assigned to'
51+
},
52+
{
53+
name: '--service -id',
54+
description: 'id of the service the entry should be assigned to'
55+
}
3056
];
3157

3258
// try to find the latest entries created by the current user and propose the

source/mite-amend.js

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,32 @@ program
1616
.version(pkg.version)
1717
.arguments('[timeEntryId] [note]')
1818
.description(
19-
'Rewrite the note for the given time entry id or use the currently ' +
20-
'running time entry and edit it’s note',
19+
`Updates the note of the currently tracked time entry (when no id is given) \
20+
or the one specified in the first argument. The content of the note can be \
21+
second argument or piped into the program. See the examples for the different \
22+
usages.
23+
24+
Also the project id and service id of a time entry can be altered if the user \
25+
has the required permissions to do it.`,
2126
{
22-
timeEntryId: 'optional id of the time entry that should be altered, ' +
23-
'if not given the currently running entry is used',
27+
timeEntryId:
28+
'optional id of the time entry that should be altered, if not given ' +
29+
'the currently running entry is used',
2430
note: 'optional value to which the time entries note should be set'
2531
}
2632
)
2733
.option(
2834
'-e, --editor',
2935
'open preferred $EDITOR for editing'
3036
)
37+
.option(
38+
'--project-id <id>',
39+
'the project id which should be set'
40+
)
41+
.option(
42+
'--service-id <id>',
43+
'the service id which should be set'
44+
)
3145
.on('--help', () => console.log(`
3246
Examples:
3347
@@ -42,9 +56,21 @@ Examples:
4256
4357
Change the note via command line argument:
4458
mite amend 12345678 "created a programmable list of items"
59+
60+
If you leave out the id the currently tracked note will be changed
61+
mite amend "created a programmable list of items"
62+
63+
Change project and service
64+
mite amend 12345678 --service-id 918772 --project-id 129379
4565
`));
4666

4767
function main(timeEntryId, note) {
68+
// if first argument is the note instead of the timeEntry
69+
if (!note && !timeEntryId.match(/^\d+/)) {
70+
note = timeEntryId;
71+
timeEntryId = undefined;
72+
}
73+
4874
const mite = miteApi(config.get());
4975
const miteTracker = tracker(config.get());
5076
const getTimeEntry = util.promisify(mite.getTimeEntry);
@@ -77,25 +103,35 @@ function main(timeEntryId, note) {
77103
promise = getTimeEntry(timeEntryId);
78104
}
79105

106+
let updateData = {
107+
...(program.projectId && { project_id: program.projectId }),
108+
...(program.serviceId && { service_id: program.serviceId }),
109+
};
110+
80111
return promise
81112
.then(data => {
82113
if (!data) {
83114
throw new Error('Unable to find time entry with the given ID');
84115
}
85116
return data.time_entry;
86117
})
118+
/**
119+
* @return {Object.<string>}
120+
* @return {string} entry.note new note
121+
*/
87122
.then(timeEntry => {
88123
timeEntryId = timeEntry.id;
89124

90-
// only ask for updated note entered via editor or inquirer if it’s
91-
// not been set before
92-
if (typeof note !== 'undefined') {
93-
// use note passed over via pipe
125+
// if only service and project should be changed (no note given)
126+
if (typeof note === 'undefined' && updateData !== {}) {
127+
return updateData;
128+
} else if (typeof note !== 'undefined') {
129+
// note passed over via pipe or argument
94130
return { note };
95131
} else if (program.editor) {
96132
// use $EDITOR and return content
97-
return openEditor(timeEntry.note).then((editedText) => {
98-
return { note: editedText };
133+
return openEditor(timeEntry.note).then((editorContent) => {
134+
return { note: editorContent };
99135
});
100136
} else {
101137
// use interactive mode (inquirer)

source/mite.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ const autoComplete = require('./lib/auto-complete');
88

99
program
1010
.version(pkg.version)
11-
.command('amend', 'edit the text for a specific time entry or the currently runnning entry').alias('reword')
11+
.command('amend', 'edit note, service, project of a specific time entry or the currently runnning entry').alias('reword')
1212
.command('autocomplete', 'install/uninstall autocompletion')
1313
.command('config', 'show or set configuration settings')
14-
.command('customer', 'update/delete single customer')
14+
.command('customer', 'create/delete/list/update customer')
1515
.command('customers', 'list, filter & search customers').alias('clients')
1616
.command('delete', 'delete a specific time entry').alias('rm')
1717
.command('list', 'list time entries', { isDefault: true }).alias('ls').alias('status').alias('st')
1818
.command('lock', 'lock single time entry')
1919
.command('new', 'create a new time entry').alias('create')
2020
.command('open', 'open the given time entry in browser')
21-
.command('project', 'update/delete a single project')
21+
.command('project', 'create/delete/list/update a single project')
2222
.command('projects', 'list, filter, archive/unarchive & search projects')
23-
.command('service', 'update/delete single service')
23+
.command('service', 'create/delete/list/update single service')
2424
.command('services', 'list, filter & search services')
2525
.command('start', 'start the tracker for the given id, will also stop allready running entry')
2626
.command('stop', 'stop any running counter')

0 commit comments

Comments
 (0)