forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stemcell_slug.go
87 lines (66 loc) · 1.66 KB
/
stemcell_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package director
import (
"encoding/json"
"fmt"
"strings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type StemcellSlug struct {
name string
version string
}
func NewStemcellSlug(name, version string) StemcellSlug {
if len(name) == 0 {
panic("Expected stemcell to specify non-empty name")
}
if len(version) == 0 {
panic("Expected stemcell to specify non-empty version")
}
return StemcellSlug{name: name, version: version}
}
func (s StemcellSlug) Name() string { return s.name }
func (s StemcellSlug) Version() string { return s.version }
func (s StemcellSlug) String() string {
return fmt.Sprintf("%s/%s", s.name, s.version)
}
func (s *StemcellSlug) UnmarshalFlag(data string) error {
slug, err := parseStemcellSlug(data)
if err != nil {
return err
}
*s = slug
return nil
}
func (s *StemcellSlug) UnmarshalJSON(data []byte) error {
var str string
err := json.Unmarshal(data, &str)
if err != nil {
return bosherr.Errorf("Expected '%s' to be a string", data)
}
slug, err := parseStemcellSlug(str)
if err != nil {
return err
}
*s = slug
return nil
}
func parseStemcellSlug(str string) (StemcellSlug, error) {
pieces := strings.Split(str, "/")
if len(pieces) != 2 {
return StemcellSlug{}, bosherr.Errorf(
"Expected stemcell '%s' to be in format 'name/version'", str)
}
if len(pieces[0]) == 0 {
return StemcellSlug{}, bosherr.Errorf(
"Expected stemcell '%s' to specify non-empty name", str)
}
if len(pieces[1]) == 0 {
return StemcellSlug{}, bosherr.Errorf(
"Expected stemcell '%s' to specify non-empty version", str)
}
slug := StemcellSlug{
name: pieces[0],
version: pieces[1],
}
return slug, nil
}