github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/snap/restartcond.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2014-2017 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  	"errors"
    24  )
    25  
    26  // RestartCondition encapsulates the different systemd 'restart' options
    27  type RestartCondition string
    28  
    29  // These are the supported restart conditions
    30  const (
    31  	RestartNever      RestartCondition = "never"
    32  	RestartOnSuccess  RestartCondition = "on-success"
    33  	RestartOnFailure  RestartCondition = "on-failure"
    34  	RestartOnAbnormal RestartCondition = "on-abnormal"
    35  	RestartOnAbort    RestartCondition = "on-abort"
    36  	RestartOnWatchdog RestartCondition = "on-watchdog"
    37  	RestartAlways     RestartCondition = "always"
    38  )
    39  
    40  var RestartMap = map[string]RestartCondition{
    41  	"no":          RestartNever,
    42  	"never":       RestartNever,
    43  	"on-success":  RestartOnSuccess,
    44  	"on-failure":  RestartOnFailure,
    45  	"on-abnormal": RestartOnAbnormal,
    46  	"on-abort":    RestartOnAbort,
    47  	"on-watchdog": RestartOnWatchdog,
    48  	"always":      RestartAlways,
    49  }
    50  
    51  // ErrUnknownRestartCondition is returned when trying to unmarshal an unknown restart condition
    52  var ErrUnknownRestartCondition = errors.New("invalid restart condition")
    53  
    54  func (rc RestartCondition) String() string {
    55  	if rc == "never" {
    56  		return "no"
    57  	}
    58  	return string(rc)
    59  }
    60  
    61  // UnmarshalYAML so RestartCondition implements yaml's Unmarshaler interface
    62  func (rc *RestartCondition) UnmarshalYAML(unmarshal func(interface{}) error) error {
    63  	var v string
    64  
    65  	if err := unmarshal(&v); err != nil {
    66  		return err
    67  	}
    68  
    69  	nrc, ok := RestartMap[v]
    70  	if !ok {
    71  		return ErrUnknownRestartCondition
    72  	}
    73  
    74  	*rc = nrc
    75  
    76  	return nil
    77  }