-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.v
77 lines (69 loc) · 2.16 KB
/
hooks.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
module vargus
pub enum CmdHooksType {
pre_run
post_run
persistent_pre_run
persistent_post_run
}
// CmdHooks are command hooks run specifically only on
// specific command target / on which it is set.
struct CmdHooks {
mut:
use_pre_run bool
use_post_run bool
pre_run CmdFunction
post_run CmdFunction
}
// Config required to be passed for adding hooks
pub struct HooksConfig {
hooks_type CmdHooksType
function CmdFunction
}
// PersistentCmdHooks are command hooks that will run
// on the command and all of it's sub_command if not defined.
struct PersistentCmdHooks {
mut:
use_persistent_pre_run bool
use_persistent_post_run bool
persistent_pre_run CmdFunction
persistent_post_run CmdFunction
}
// add_hooks adds the capability to add hooks to a command
pub fn (mut c Commander) add_hooks(h_cfg HooksConfig) {
match h_cfg.hooks_type {
.pre_run {
c.hooks.use_pre_run = true
c.hooks.pre_run = h_cfg.function
}
.post_run {
c.hooks.use_post_run = true
c.hooks.post_run = h_cfg.function
}
.persistent_pre_run {
c.persistent_hooks.use_persistent_pre_run = true
c.persistent_hooks.persistent_pre_run = h_cfg.function
}
.persistent_post_run {
c.persistent_hooks.use_persistent_post_run = true
c.persistent_hooks.persistent_post_run = h_cfg.function
}
}
}
// get_persistent_hooks parses the persistent hooks if it is defined in the command
// otherwise, it uses the parent's
fn (c &Commander) get_persistent_hooks(parent_phooks PersistentCmdHooks) PersistentCmdHooks {
mut tp_hooks := parent_phooks
// if persistent_pre_run is defined in cmd, use it
// otherwise use the defined one by the parent
if c.persistent_hooks.use_persistent_pre_run {
tp_hooks.use_persistent_pre_run = c.persistent_hooks.use_persistent_pre_run
tp_hooks.persistent_pre_run = c.persistent_hooks.persistent_pre_run
}
// if persistent_post_run is defined in cmd, use it
// otherwise use the defined one by the parent
if c.persistent_hooks.use_persistent_post_run {
tp_hooks.use_persistent_post_run = c.persistent_hooks.use_persistent_post_run
tp_hooks.persistent_post_run = c.persistent_hooks.persistent_post_run
}
return tp_hooks
}