forked from concourse/baggageclaim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategerizer.go
54 lines (43 loc) · 1.12 KB
/
strategerizer.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
package volume
import (
"encoding/json"
"errors"
"fmt"
"github.com/concourse/baggageclaim"
)
type Strategerizer interface {
StrategyFor(baggageclaim.VolumeRequest) (Strategy, error)
}
const (
StrategyEmpty = "empty"
StrategyCopyOnWrite = "cow"
StrategyImport = "import"
)
var ErrNoStrategy = errors.New("no strategy given")
var ErrUnknownStrategy = errors.New("unknown strategy")
type strategerizer struct{}
func NewStrategerizer() Strategerizer {
return &strategerizer{}
}
func (s *strategerizer) StrategyFor(request baggageclaim.VolumeRequest) (Strategy, error) {
if request.Strategy == nil {
return nil, ErrNoStrategy
}
var strategyInfo map[string]string
err := json.Unmarshal(*request.Strategy, &strategyInfo)
if err != nil {
return nil, fmt.Errorf("malformed strategy: %s", err)
}
var strategy Strategy
switch strategyInfo["type"] {
case StrategyEmpty:
strategy = EmptyStrategy{}
case StrategyCopyOnWrite:
strategy = COWStrategy{strategyInfo["volume"]}
case StrategyImport:
strategy = ImportStrategy{strategyInfo["path"]}
default:
return nil, ErrUnknownStrategy
}
return strategy, nil
}