-
-
Notifications
You must be signed in to change notification settings - Fork 380
/
hooks.go
88 lines (76 loc) · 1.55 KB
/
hooks.go
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
78
79
80
81
82
83
84
85
86
87
88
package config
import (
"encoding/json"
"errors"
)
// Hook is one or more commands.
type Hook []string
// Hooks for the project.
type Hooks struct {
Build Hook `json:"build"`
Clean Hook `json:"clean"`
PreBuild Hook `json:"prebuild"`
PostBuild Hook `json:"postbuild"`
PreDeploy Hook `json:"predeploy"`
PostDeploy Hook `json:"postdeploy"`
}
// Override config.
func (h *Hooks) Override(c *Config) {
if v := h.Build; v != nil {
c.Hooks.Build = v
}
if v := h.Clean; v != nil {
c.Hooks.Clean = v
}
if v := h.PreBuild; v != nil {
c.Hooks.PreBuild = v
}
if v := h.PostBuild; v != nil {
c.Hooks.PostBuild = v
}
if v := h.PreDeploy; v != nil {
c.Hooks.PreDeploy = v
}
if v := h.PostDeploy; v != nil {
c.Hooks.PostDeploy = v
}
}
// Get returns the hook by name or nil.
func (h *Hooks) Get(s string) Hook {
switch s {
case "build":
return h.Build
case "clean":
return h.Clean
case "prebuild":
return h.PreBuild
case "postbuild":
return h.PostBuild
case "predeploy":
return h.PreDeploy
case "postdeploy":
return h.PostDeploy
default:
return nil
}
}
// UnmarshalJSON implementation.
func (h *Hook) UnmarshalJSON(b []byte) error {
switch b[0] {
case '"':
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
*h = append(*h, s)
return nil
case '[':
return json.Unmarshal(b, (*[]string)(h))
default:
return errors.New("hook must be a string or array of strings")
}
}
// IsEmpty returns true if the hook is empty.
func (h *Hook) IsEmpty() bool {
return h == nil || len(*h) == 0
}