-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathstorage.go
99 lines (85 loc) · 2.16 KB
/
storage.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
package config
import (
"fmt"
"github.com/monax/hoard/v8/stores"
"bytes"
"github.com/BurntSushi/toml"
)
const DefaultAddressEncodingName = stores.Base64EncodingName
func NewDefaultStorage() *Storage {
return NewStorage(Memory, DefaultAddressEncodingName)
}
type StorageType string
const (
Unspecified StorageType = ""
Memory StorageType = "memory"
Filesystem StorageType = "filesystem"
AWS StorageType = "aws"
Azure StorageType = "azure"
GCP StorageType = "gcp"
IPFS StorageType = "ipfs"
)
// Storage identifies the configured back-end
type Storage struct {
// Acts a string enum
StorageType StorageType
// Address encoding name
AddressEncoding string
// Embedding a pointer to each type of config struct allows us to access the
// relevant one, while at the same time those that are left as nil will be
// omitted from being serialised.
*FileSystemConfig
*Cloud
*IPFSConfig
}
func NewStorage(storageType StorageType, addressEncoding string) *Storage {
return &Storage{
StorageType: storageType,
AddressEncoding: addressEncoding,
}
}
func GetStorageTypes() []StorageType {
return []StorageType{
Memory,
Filesystem,
AWS,
Azure,
GCP,
IPFS,
}
}
func GetDefaultStorage(storageType StorageType) (*Storage, error) {
switch storageType {
case Memory, Unspecified:
return NewDefaultMemory(), nil
case Filesystem:
return NewDefaultFileSystemConfig(), nil
case IPFS:
return NewDefaultIPFSConfig(), nil
case AWS:
return NewDefaultCloud(storageType), nil
case Azure:
return NewDefaultCloud(storageType), nil
case GCP:
return NewDefaultCloud(storageType), nil
default:
return nil, fmt.Errorf("did not recognise storage type '%s'", storageType)
}
}
func ConfigFromString(tomlString string) (*Storage, error) {
storageConfig := new(Storage)
_, err := toml.Decode(tomlString, storageConfig)
if err != nil {
return nil, err
}
return storageConfig, nil
}
func (storageConfig *Storage) TOMLString() string {
buf := new(bytes.Buffer)
encoder := toml.NewEncoder(buf)
err := encoder.Encode(storageConfig)
if err != nil {
return fmt.Sprintf("<Could not serialise Storage>")
}
return buf.String()
}