github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/snap/hooktypes.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package snap 21 22 import ( 23 "regexp" 24 ) 25 26 var supportedHooks = []*HookType{ 27 NewHookType(regexp.MustCompile("^prepare-device$")), 28 NewHookType(regexp.MustCompile("^configure$")), 29 NewHookType(regexp.MustCompile("^install$")), 30 NewHookType(regexp.MustCompile("^pre-refresh$")), 31 NewHookType(regexp.MustCompile("^post-refresh$")), 32 NewHookType(regexp.MustCompile("^remove$")), 33 NewHookType(regexp.MustCompile("^prepare-(?:plug|slot)-[-a-z0-9]+$")), 34 NewHookType(regexp.MustCompile("^unprepare-(?:plug|slot)-[-a-z0-9]+$")), 35 NewHookType(regexp.MustCompile("^connect-(?:plug|slot)-[-a-z0-9]+$")), 36 NewHookType(regexp.MustCompile("^disconnect-(?:plug|slot)-[-a-z0-9]+$")), 37 NewHookType(regexp.MustCompile("^check-health$")), 38 } 39 40 // HookType represents a pattern of supported hook names. 41 type HookType struct { 42 pattern *regexp.Regexp 43 } 44 45 // NewHookType returns a new HookType with the given pattern. 46 func NewHookType(pattern *regexp.Regexp) *HookType { 47 return &HookType{ 48 pattern: pattern, 49 } 50 } 51 52 // Match returns true if the given hook name matches this hook type. 53 func (hookType HookType) Match(hookName string) bool { 54 return hookType.pattern.MatchString(hookName) 55 } 56 57 // IsHookSupported returns true if the given hook name matches one of the 58 // supported hooks. 59 func IsHookSupported(hookName string) bool { 60 for _, hookType := range supportedHooks { 61 if hookType.Match(hookName) { 62 return true 63 } 64 } 65 66 return false 67 } 68 69 func MockSupportedHookTypes(hookTypes []*HookType) (restore func()) { 70 old := supportedHooks 71 supportedHooks = hookTypes 72 return func() { supportedHooks = old } 73 } 74 75 func MockAppendSupportedHookTypes(hookTypes []*HookType) (restore func()) { 76 old := supportedHooks 77 supportedHooks = append(supportedHooks, hookTypes...) 78 return func() { supportedHooks = old } 79 }