Skip to content

Commit

Permalink
Add support for pre-modify & post-modify hooks, update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-si committed Sep 22, 2024
1 parent 8ee1c51 commit 24b0b21
Show file tree
Hide file tree
Showing 8 changed files with 470 additions and 137 deletions.
3 changes: 2 additions & 1 deletion docs-source/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
- [Installation](./cli/installation.md)
- [Usage](./cli/usage.md)
- [Import](./cli/import.md)
- [Hooks](./cli/hooks.md)
- [Hooks](./cli/hooks/main.md)
- [Examples](./cli/hooks/examples.md)

- [Web Based GUIs](./web_based_guis/main.md)
- [Web App](./web_based_guis/web_app.md)
Expand Down
313 changes: 313 additions & 0 deletions docs-source/cli/hooks/examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,313 @@
# Examples

---
<!-- toc -->
---

## Config

You can add a `hooks` field to your config file like this:

```yaml
hooks:
directory: /Users/adrian/Dropbox/TaskLite/hooks
launch:
pre:
- interpreter: v
body: |
fn main() {
println('{
"message": "📜 Pre launch message",
"warning": "⚠️ Pre launch warning",
"error": "❌ Pre launch error"
}')
}
post: []
add:
pre: []
post: []
modify:
pre: []
post: []
exit:
pre: []
```
## V Lang Scripts
Save the examples in the hooks directory as `pre-launch.v`, etc.


### Pre Launch

```v
fn main() {
println('{
"message": "📜 Pre launch message",
"warning": "⚠️ Pre launch warning",
"error": "❌ Pre launch error"
}')
}
```


### Post Launch

```v
import os
import json
struct PostLaunchInput {
mut:
arguments []string
}
struct PostLaunchOutput {
message string
warning string
error string
}
fn main() {
stdin := os.get_raw_lines_joined()
mut input := json.decode(PostLaunchInput, stdin)!
println(json.encode(PostLaunchOutput{
message: '📜 Post launch message\n' + //
'Provided arguments: ${input.arguments}\n'
warning: '⚠️ Post-launch warning'
error: '❌ Post-launch error'
}))
}
```


### Pre Add

```v
import os
import json
struct Task {
mut:
body string
tags []string
}
struct PreAddInput {
mut:
task_to_add Task @[json: 'taskToAdd']
arguments []string
}
struct PreAddOutput {
mut:
task Task
message string
warning string
error string
}
fn main() {
stdin := os.get_raw_lines_joined()
input_data := json.decode(PreAddInput, stdin)!
mut task := input_data.task_to_add
task.tags << 'additional-tag'
pre_add_output := PreAddOutput{
task: task
message: '📜 Pre-add message\n' + //
'Provided arguments: ${input_data.arguments.str()}\n' + //
'Provided task: ${input_data.task_to_add.str()}\n' + //
'Updated task: ${task.str()}\n'
warning: '⚠️ Pre-add warning'
error: '❌ Pre-add error'
}
println(json.encode(pre_add_output))
}
```


### Post Add

```v
import os
import json
struct Task {
mut:
body string
tags []string
}
struct PostAddInput {
mut:
arguments []string
task_added Task @[json: 'taskAdded']
}
struct PostAddOutput {
mut:
message string
warning string
error string
}
fn main() {
stdin := os.get_raw_lines_joined()
mut input_data := json.decode(PostAddInput, stdin)!
post_add_output := PostAddOutput{
message: '📜 Post-add message\n' + //
'Provided arguments: ${input_data.arguments.str()}\n' + //
'Added task: ${input_data.task_added.str()}\n'
warning: '⚠️ Post-add warning'
error: '❌ Post-add error'
}
println(json.encode(post_add_output))
}
```


### Pre Modify

```v
import os
import json
struct Task {
mut:
body string
tags []string
}
struct PreModifyInput {
mut:
arguments []string
task_to_modify Task @[json: 'taskToModify']
}
struct PreModifyOutput {
mut:
task Task
message string
warning string
error string
}
fn main() {
stdin := os.get_raw_lines_joined()
input_data := json.decode(PreModifyInput, stdin)!
mut task := input_data.task_to_modify
task.tags << 'additional-tag'
pre_modify_output := PreModifyOutput{
task: task // TODO: This should be parsed by TaskLite and used downstream
message: '📜 Pre-modify message\n' + //
'Provided arguments: ${input_data.arguments.str()}\n' + //
'Provided task: ${input_data.task_to_modify.str()}\n' + //
'Updated task: ${task.str()}\n'
warning: '⚠️ Pre-modify warning'
error: '❌ Pre-modify error'
}
println(json.encode(pre_modify_output))
}
```


### Post Modify

```v
import os
import json
struct Task {
mut:
body string
tags []string
}
struct PostModifyInput {
mut:
arguments []string
task_modified Task @[json: 'taskModified']
}
struct PostModifyOutput {
mut:
message string
warning string
error string
}
fn main() {
stdin := os.get_raw_lines_joined()
mut input_data := json.decode(PostModifyInput, stdin)!
post_modify_output := PostModifyOutput{
message: '📜 Post-modify message\n' + //
'Provided arguments: ${input_data.arguments.str()}\n' + //
'Modified task: ${input_data.task_modified.str()}\n'
warning: '⚠️ Post-modify warning'
error: '❌ Post-modify error'
}
println(json.encode(post_modify_output))
}
```


### Pre Exit

```v
fn main() {
println('{
"message": "📜 Pre exit message",
"warning": "⚠️ Pre exit warning",
"error": "❌ Pre exit error"
}')
}
```


## Shell Scripts

Save the examples in the hooks directory as `pre-launch.sh`, etc.

### Pre Launch

```sh
stdin=$(cat)
>&2 echo "File > pre-launch: Input via stdin:"
>&2 echo "$stdin"
echo "{}"
```


### Post Launch

```sh
stdin=$(cat)
>&2 echo "File > post-launch: Input via stdin:"
>&2 echo "$stdin"
echo "{}"
```


### Pre Add

```sh
stdin=$(cat)
>&2 echo "File > pre-add: Input via stdin:"
>&2 echo "$stdin"
echo "{}"
```
Loading

0 comments on commit 24b0b21

Please sign in to comment.