~pedronis/snappy-hub/assert-fetcher

« back to all changes in this revision

Viewing changes to vendor/github.com/snapcore/snapd/snap/restartcond.go

  • Committer: Samuele Pedroni (Canonical Services Ltd.)
  • Date: 2018-04-11 14:53:33 UTC
  • Revision ID: samuele.pedroni@canonical.com-20180411145333-7d2jj3oo53s3baa1
snapd 2.32.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        RestartAlways     RestartCondition = "always"
 
37
)
 
38
 
 
39
var RestartMap = map[string]RestartCondition{
 
40
        "no":          RestartNever,
 
41
        "never":       RestartNever,
 
42
        "on-success":  RestartOnSuccess,
 
43
        "on-failure":  RestartOnFailure,
 
44
        "on-abnormal": RestartOnAbnormal,
 
45
        "on-abort":    RestartOnAbort,
 
46
        "always":      RestartAlways,
 
47
}
 
48
 
 
49
// ErrUnknownRestartCondition is returned when trying to unmarshal an unknown restart condition
 
50
var ErrUnknownRestartCondition = errors.New("invalid restart condition")
 
51
 
 
52
func (rc RestartCondition) String() string {
 
53
        if rc == "never" {
 
54
                return "no"
 
55
        }
 
56
        return string(rc)
 
57
}
 
58
 
 
59
// UnmarshalYAML so RestartCondition implements yaml's Unmarshaler interface
 
60
func (rc *RestartCondition) UnmarshalYAML(unmarshal func(interface{}) error) error {
 
61
        var v string
 
62
 
 
63
        if err := unmarshal(&v); err != nil {
 
64
                return err
 
65
        }
 
66
 
 
67
        nrc, ok := RestartMap[v]
 
68
        if !ok {
 
69
                return ErrUnknownRestartCondition
 
70
        }
 
71
 
 
72
        *rc = nrc
 
73
 
 
74
        return nil
 
75
}