-
Notifications
You must be signed in to change notification settings - Fork 59
/
channel.go
179 lines (157 loc) · 3.98 KB
/
channel.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright 2020 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package charm
import (
"fmt"
"strings"
"github.com/juju/errors"
)
// Risk describes the type of risk in a current channel.
type Risk string
const (
Stable Risk = "stable"
Candidate Risk = "candidate"
Beta Risk = "beta"
Edge Risk = "edge"
)
// Risks is a list of the available channel risks.
var Risks = []Risk{
Stable,
Candidate,
Beta,
Edge,
}
func isRisk(potential string) bool {
for _, risk := range Risks {
if potential == string(risk) {
return true
}
}
return false
}
// Channel identifies and describes completely a store channel.
//
// A channel consists of, and is subdivided by, tracks, risk-levels and
// branches:
// - Tracks enable snap developers to publish multiple supported releases of
// their application under the same snap name.
// - Risk-levels represent a progressive potential trade-off between stability
// and new features.
// - Branches are _optional_ and hold temporary releases intended to help with
// bug-fixing.
//
// The complete channel name can be structured as three distinct parts separated
// by slashes:
//
// <track>/<risk>/<branch>
//
type Channel struct {
Track string `json:"track,omitempty"`
Risk Risk `json:"risk,omitempty"`
Branch string `json:"branch,omitempty"`
}
// MakeChannel creates a core charm Channel from a set of component parts.
func MakeChannel(track, risk, branch string) (Channel, error) {
if !isRisk(risk) {
return Channel{}, errors.NotValidf("risk %q", risk)
}
return Channel{
Track: track,
Risk: Risk(risk),
Branch: branch,
}, nil
}
// MakePermissiveChannel creates a normalized core charm channel which
// never fails. It assumes that the risk has been prechecked.
func MakePermissiveChannel(track, risk, branch string) Channel {
ch := Channel{
Track: track,
Risk: Risk(risk),
Branch: branch,
}
return ch.Normalize()
}
// ParseChannel parses a string representing a store channel.
func ParseChannel(s string) (Channel, error) {
if s == "" {
return Channel{}, errors.NotValidf("empty channel")
}
p := strings.Split(s, "/")
var risk, track, branch *string
switch len(p) {
case 1:
if isRisk(p[0]) {
risk = &p[0]
} else {
track = &p[0]
}
case 2:
if isRisk(p[0]) {
risk, branch = &p[0], &p[1]
} else {
track, risk = &p[0], &p[1]
}
case 3:
track, risk, branch = &p[0], &p[1], &p[2]
default:
return Channel{}, errors.Errorf("channel is malformed and has too many components %q", s)
}
ch := Channel{}
if risk != nil {
if !isRisk(*risk) {
return Channel{}, errors.NotValidf("risk in channel %q", s)
}
// We can lift this into a risk, as we've validated prior to this to
// ensure it's a valid risk.
ch.Risk = Risk(*risk)
}
if track != nil {
if *track == "" {
return Channel{}, errors.NotValidf("track in channel %q", s)
}
ch.Track = *track
}
if branch != nil {
if *branch == "" {
return Channel{}, errors.NotValidf("branch in channel %q", s)
}
ch.Branch = *branch
}
return ch, nil
}
// ParseChannelNormalize parses a string representing a store channel.
// The returned channel's track, risk and name are normalized.
func ParseChannelNormalize(s string) (Channel, error) {
ch, err := ParseChannel(s)
if err != nil {
return Channel{}, errors.Trace(err)
}
return ch.Normalize(), nil
}
// Normalize the channel with normalized track, risk and names.
func (ch Channel) Normalize() Channel {
track := ch.Track
risk := ch.Risk
if risk == "" {
risk = "stable"
}
return Channel{
Track: track,
Risk: risk,
Branch: ch.Branch,
}
}
// Empty returns true if all it's components are empty.
func (ch Channel) Empty() bool {
return ch.Track == "" && ch.Risk == "" && ch.Branch == ""
}
func (ch Channel) String() string {
path := string(ch.Risk)
if track := ch.Track; track != "" {
path = fmt.Sprintf("%s/%s", track, path)
}
if branch := ch.Branch; branch != "" {
path = fmt.Sprintf("%s/%s", path, branch)
}
return path
}