github.com/orangenpresse/up@v0.6.0/config/hooks.go (about) 1 package config 2 3 import ( 4 "encoding/json" 5 "errors" 6 ) 7 8 // Hook is one or more commands. 9 type Hook []string 10 11 // Hooks for the project. 12 type Hooks struct { 13 Build Hook `json:"build"` 14 Clean Hook `json:"clean"` 15 PreBuild Hook `json:"prebuild"` 16 PostBuild Hook `json:"postbuild"` 17 PreDeploy Hook `json:"predeploy"` 18 PostDeploy Hook `json:"postdeploy"` 19 } 20 21 // Override config. 22 func (h *Hooks) Override(c *Config) { 23 if v := h.Build; v != nil { 24 c.Hooks.Build = v 25 } 26 27 if v := h.Clean; v != nil { 28 c.Hooks.Clean = v 29 } 30 31 if v := h.PreBuild; v != nil { 32 c.Hooks.PreBuild = v 33 } 34 35 if v := h.PostBuild; v != nil { 36 c.Hooks.PostBuild = v 37 } 38 39 if v := h.PreDeploy; v != nil { 40 c.Hooks.PreDeploy = v 41 } 42 43 if v := h.PostDeploy; v != nil { 44 c.Hooks.PostDeploy = v 45 } 46 } 47 48 // Get returns the hook by name or nil. 49 func (h *Hooks) Get(s string) Hook { 50 switch s { 51 case "build": 52 return h.Build 53 case "clean": 54 return h.Clean 55 case "prebuild": 56 return h.PreBuild 57 case "postbuild": 58 return h.PostBuild 59 case "predeploy": 60 return h.PreDeploy 61 case "postdeploy": 62 return h.PostDeploy 63 default: 64 return nil 65 } 66 } 67 68 // UnmarshalJSON implementation. 69 func (h *Hook) UnmarshalJSON(b []byte) error { 70 switch b[0] { 71 case '"': 72 var s string 73 if err := json.Unmarshal(b, &s); err != nil { 74 return err 75 } 76 *h = append(*h, s) 77 return nil 78 case '[': 79 return json.Unmarshal(b, (*[]string)(h)) 80 default: 81 return errors.New("hook must be a string or array of strings") 82 } 83 } 84 85 // IsEmpty returns true if the hook is empty. 86 func (h *Hook) IsEmpty() bool { 87 return h == nil || len(*h) == 0 88 }