github.com/chipaca/snappy@v0.0.0-20210104084008-1f06296fe8ad/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 NewHookType(regexp.MustCompile("^fde-setup$")), 39 } 40 41 // HookType represents a pattern of supported hook names. 42 type HookType struct { 43 pattern *regexp.Regexp 44 } 45 46 // NewHookType returns a new HookType with the given pattern. 47 func NewHookType(pattern *regexp.Regexp) *HookType { 48 return &HookType{ 49 pattern: pattern, 50 } 51 } 52 53 // Match returns true if the given hook name matches this hook type. 54 func (hookType HookType) Match(hookName string) bool { 55 return hookType.pattern.MatchString(hookName) 56 } 57 58 // IsHookSupported returns true if the given hook name matches one of the 59 // supported hooks. 60 func IsHookSupported(hookName string) bool { 61 for _, hookType := range supportedHooks { 62 if hookType.Match(hookName) { 63 return true 64 } 65 } 66 67 return false 68 } 69 70 func MockSupportedHookTypes(hookTypes []*HookType) (restore func()) { 71 old := supportedHooks 72 supportedHooks = hookTypes 73 return func() { supportedHooks = old } 74 } 75 76 func MockAppendSupportedHookTypes(hookTypes []*HookType) (restore func()) { 77 old := supportedHooks 78 supportedHooks = append(supportedHooks, hookTypes...) 79 return func() { supportedHooks = old } 80 }