-
Notifications
You must be signed in to change notification settings - Fork 162
/
os_version_slug.go
68 lines (52 loc) · 1.34 KB
/
os_version_slug.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
package director
import (
"fmt"
"strings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type OSVersionSlug struct {
os string
version string
}
func NewOSVersionSlug(os, version string) OSVersionSlug {
if len(os) == 0 {
panic("Expected stemcell to specify non-empty OS")
}
if len(version) == 0 {
panic("Expected stemcell to specify non-empty version")
}
return OSVersionSlug{os: os, version: version}
}
func (s OSVersionSlug) OS() string { return s.os }
func (s OSVersionSlug) Version() string { return s.version }
func (s OSVersionSlug) String() string {
return fmt.Sprintf("%s/%s", s.os, s.version)
}
func (s *OSVersionSlug) UnmarshalFlag(data string) error {
slug, err := parseOSVersionSlug(data)
if err != nil {
return err
}
*s = slug
return nil
}
func parseOSVersionSlug(str string) (OSVersionSlug, error) {
pieces := strings.Split(str, "/")
if len(pieces) != 2 {
return OSVersionSlug{}, bosherr.Errorf(
"Expected OS '%s' to be in format 'name/version'", str)
}
if len(pieces[0]) == 0 {
return OSVersionSlug{}, bosherr.Errorf(
"Expected OS '%s' to specify non-empty name", str)
}
if len(pieces[1]) == 0 {
return OSVersionSlug{}, bosherr.Errorf(
"Expected OS '%s' to specify non-empty version", str)
}
slug := OSVersionSlug{
os: pieces[0],
version: pieces[1],
}
return slug, nil
}