github.com/ethanhsieh/snapd@v0.0.0-20210615102523-3db9b8e4edc5/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("^install-device$")),
    29  	NewHookType(regexp.MustCompile("^configure$")),
    30  	NewHookType(regexp.MustCompile("^install$")),
    31  	NewHookType(regexp.MustCompile("^pre-refresh$")),
    32  	NewHookType(regexp.MustCompile("^post-refresh$")),
    33  	NewHookType(regexp.MustCompile("^remove$")),
    34  	NewHookType(regexp.MustCompile("^prepare-(?:plug|slot)-[-a-z0-9]+$")),
    35  	NewHookType(regexp.MustCompile("^unprepare-(?:plug|slot)-[-a-z0-9]+$")),
    36  	NewHookType(regexp.MustCompile("^connect-(?:plug|slot)-[-a-z0-9]+$")),
    37  	NewHookType(regexp.MustCompile("^disconnect-(?:plug|slot)-[-a-z0-9]+$")),
    38  	NewHookType(regexp.MustCompile("^check-health$")),
    39  	NewHookType(regexp.MustCompile("^fde-setup$")),
    40  	NewHookType(regexp.MustCompile("^gate-auto-refresh$")),
    41  }
    42  
    43  // HookType represents a pattern of supported hook names.
    44  type HookType struct {
    45  	pattern *regexp.Regexp
    46  }
    47  
    48  // NewHookType returns a new HookType with the given pattern.
    49  func NewHookType(pattern *regexp.Regexp) *HookType {
    50  	return &HookType{
    51  		pattern: pattern,
    52  	}
    53  }
    54  
    55  // Match returns true if the given hook name matches this hook type.
    56  func (hookType HookType) Match(hookName string) bool {
    57  	return hookType.pattern.MatchString(hookName)
    58  }
    59  
    60  // IsHookSupported returns true if the given hook name matches one of the
    61  // supported hooks.
    62  func IsHookSupported(hookName string) bool {
    63  	for _, hookType := range supportedHooks {
    64  		if hookType.Match(hookName) {
    65  			return true
    66  		}
    67  	}
    68  
    69  	return false
    70  }
    71  
    72  func MockSupportedHookTypes(hookTypes []*HookType) (restore func()) {
    73  	old := supportedHooks
    74  	supportedHooks = hookTypes
    75  	return func() { supportedHooks = old }
    76  }
    77  
    78  func MockAppendSupportedHookTypes(hookTypes []*HookType) (restore func()) {
    79  	old := supportedHooks
    80  	supportedHooks = append(supportedHooks, hookTypes...)
    81  	return func() { supportedHooks = old }
    82  }