You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.3 KiB
Go
58 lines
1.3 KiB
Go
1 year ago
|
package backup
|
||
1 year ago
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"io/ioutil"
|
||
|
"os"
|
||
|
|
||
1 year ago
|
"github.com/rqlite/rqlite/auto"
|
||
1 year ago
|
"github.com/rqlite/rqlite/aws"
|
||
|
)
|
||
|
|
||
|
// Config is the config file format for the upload service
|
||
|
type Config struct {
|
||
1 year ago
|
Version int `json:"version"`
|
||
|
Type auto.StorageType `json:"type"`
|
||
|
NoCompress bool `json:"no_compress,omitempty"`
|
||
|
Interval auto.Duration `json:"interval"`
|
||
|
Sub json.RawMessage `json:"sub"`
|
||
1 year ago
|
}
|
||
|
|
||
|
// Unmarshal unmarshals the config file and returns the config and subconfig
|
||
|
func Unmarshal(data []byte) (*Config, *aws.S3Config, error) {
|
||
|
cfg := &Config{}
|
||
|
err := json.Unmarshal(data, cfg)
|
||
|
if err != nil {
|
||
|
return nil, nil, err
|
||
|
}
|
||
|
|
||
1 year ago
|
if cfg.Version > auto.Version {
|
||
|
return nil, nil, auto.ErrInvalidVersion
|
||
1 year ago
|
}
|
||
|
|
||
|
s3cfg := &aws.S3Config{}
|
||
|
err = json.Unmarshal(cfg.Sub, s3cfg)
|
||
|
if err != nil {
|
||
|
return nil, nil, err
|
||
|
}
|
||
|
return cfg, s3cfg, nil
|
||
|
}
|
||
|
|
||
|
// ReadConfigFile reads the config file and returns the data. It also expands
|
||
|
// any environment variables in the config file.
|
||
|
func ReadConfigFile(filename string) ([]byte, error) {
|
||
|
f, err := os.Open(filename)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
defer f.Close()
|
||
|
|
||
|
data, err := ioutil.ReadAll(f)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
data = []byte(os.ExpandEnv(string(data)))
|
||
|
return data, nil
|
||
|
}
|