-
Notifications
You must be signed in to change notification settings - Fork 1
/
disc-spec.go
41 lines (36 loc) · 1.04 KB
/
disc-spec.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
package util
import (
"fmt"
"github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util/sizespec"
"github.com/BytemarkHosting/bytemark-client/lib/brain"
"strings"
)
// DiscSpecError represents an error during parse.
type DiscSpecError struct {
Position int
Character rune
}
func (e *DiscSpecError) Error() string {
return fmt.Sprintf("Disc spec error: Unexpected %c at character %d.", e.Character, e.Position)
}
// ParseDiscSpec reads the given string and attempts to interpret it as a disc spec.
func ParseDiscSpec(spec string) (*brain.Disc, error) {
bits := strings.Split(spec, ":")
size, err := sizespec.Parse(bits[len(bits)-1])
if err != nil {
return nil, err
}
switch {
case len(bits) >= 4:
return nil, &DiscSpecError{}
case len(bits) == 3:
return &brain.Disc{Label: bits[0], StorageGrade: bits[1], Size: size}, nil
case len(bits) == 2:
return &brain.Disc{StorageGrade: bits[0], Size: size}, nil
case len(bits) == 1:
return &brain.Disc{Size: size}, nil
case len(bits) == 0:
return nil, &DiscSpecError{}
}
return nil, nil
}