forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrevision.go
123 lines (108 loc) · 2.79 KB
/
revision.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-2016 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package snap
import (
"fmt"
"strconv"
)
// Keep this in sync between snap and client packages.
type Revision struct {
N int
}
func (r Revision) String() string {
if r.N == 0 {
return "unset"
}
if r.N < 0 {
return fmt.Sprintf("x%d", -r.N)
}
return strconv.Itoa(int(r.N))
}
func (r Revision) Unset() bool {
return r.N == 0
}
func (r Revision) Local() bool {
return r.N < 0
}
func (r Revision) Store() bool {
return r.N > 0
}
func (r Revision) MarshalJSON() ([]byte, error) {
return []byte(`"` + r.String() + `"`), nil
}
func (r *Revision) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string
if err := unmarshal(&s); err != nil {
return err
}
return r.UnmarshalJSON([]byte(`"` + s + `"`))
}
func (r Revision) MarshalYAML() (interface{}, error) {
return r.String(), nil
}
func (r *Revision) UnmarshalJSON(data []byte) error {
if len(data) > 0 && data[0] == '"' && data[len(data)-1] == '"' {
parsed, err := ParseRevision(string(data[1 : len(data)-1]))
if err == nil {
*r = parsed
return nil
}
} else {
n, err := strconv.ParseInt(string(data), 10, 64)
if err == nil {
r.N = int(n)
return nil
}
}
return fmt.Errorf("invalid snap revision: %q", data)
}
// ParseRevisions returns the representation in r as a revision.
// See R for a function more suitable for hardcoded revisions.
func ParseRevision(s string) (Revision, error) {
if s == "unset" {
return Revision{}, nil
}
if s != "" && s[0] == 'x' {
i, err := strconv.Atoi(s[1:])
if err == nil && i > 0 {
return Revision{-i}, nil
}
}
i, err := strconv.Atoi(s)
if err == nil && i > 0 {
return Revision{i}, nil
}
return Revision{}, fmt.Errorf("invalid snap revision: %#v", s)
}
// R returns a Revision given an int or a string.
// Providing an invalid revision type or value causes a runtime panic.
// See ParseRevision for a polite function that does not panic.
func R(r interface{}) Revision {
switch r := r.(type) {
case string:
revision, err := ParseRevision(r)
if err != nil {
panic(err)
}
return revision
case int:
return Revision{r}
default:
panic(fmt.Errorf("cannot use %v (%T) as a snap revision", r, r))
}
}