2017-03-03 19:22:09 +01:00
|
|
|
/*
|
|
|
|
* Quick - Quick key value store for config files and persistent state files
|
|
|
|
*
|
2019-04-09 20:39:42 +02:00
|
|
|
* Quick (C) 2017 MinIO, Inc.
|
2017-03-03 19:22:09 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package quick
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-04-23 20:05:56 +02:00
|
|
|
"context"
|
2017-03-03 19:22:09 +01:00
|
|
|
"encoding/json"
|
2017-05-06 19:16:59 +02:00
|
|
|
"fmt"
|
2017-03-03 19:22:09 +01:00
|
|
|
"io/ioutil"
|
2017-10-13 12:01:15 +02:00
|
|
|
"os"
|
2017-03-03 19:22:09 +01:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
2018-04-23 20:05:56 +02:00
|
|
|
"time"
|
2017-03-03 19:22:09 +01:00
|
|
|
|
2018-07-12 23:12:40 +02:00
|
|
|
etcd "github.com/coreos/etcd/clientv3"
|
2017-03-03 19:22:09 +01:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ConfigEncoding is a generic interface which
|
|
|
|
// marshal/unmarshal configuration.
|
|
|
|
type ConfigEncoding interface {
|
|
|
|
Unmarshal([]byte, interface{}) error
|
|
|
|
Marshal(interface{}) ([]byte, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// YAML encoding implements ConfigEncoding
|
|
|
|
type yamlEncoding struct{}
|
|
|
|
|
|
|
|
func (y yamlEncoding) Unmarshal(b []byte, v interface{}) error {
|
|
|
|
return yaml.Unmarshal(b, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (y yamlEncoding) Marshal(v interface{}) ([]byte, error) {
|
|
|
|
return yaml.Marshal(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// JSON encoding implements ConfigEncoding
|
|
|
|
type jsonEncoding struct{}
|
|
|
|
|
|
|
|
func (j jsonEncoding) Unmarshal(b []byte, v interface{}) error {
|
|
|
|
err := json.Unmarshal(b, v)
|
|
|
|
if err != nil {
|
|
|
|
// Try to return a sophisticated json error message if possible
|
2017-05-06 19:16:59 +02:00
|
|
|
switch jerr := err.(type) {
|
2017-03-03 19:22:09 +01:00
|
|
|
case *json.SyntaxError:
|
2017-05-06 19:16:59 +02:00
|
|
|
return fmt.Errorf("Unable to parse JSON schema due to a syntax error at '%s'",
|
|
|
|
FormatJSONSyntaxError(bytes.NewReader(b), jerr.Offset))
|
|
|
|
case *json.UnmarshalTypeError:
|
|
|
|
return fmt.Errorf("Unable to parse JSON, type '%v' cannot be converted into the Go '%v' type",
|
|
|
|
jerr.Value, jerr.Type)
|
2017-03-03 19:22:09 +01:00
|
|
|
}
|
2017-05-06 19:16:59 +02:00
|
|
|
return err
|
2017-03-03 19:22:09 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j jsonEncoding) Marshal(v interface{}) ([]byte, error) {
|
|
|
|
return json.MarshalIndent(v, "", "\t")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert a file extension to the appropriate struct capable
|
|
|
|
// to marshal/unmarshal data
|
|
|
|
func ext2EncFormat(fileExtension string) ConfigEncoding {
|
|
|
|
// Lower the file extension
|
|
|
|
ext := strings.ToLower(fileExtension)
|
|
|
|
ext = strings.TrimPrefix(ext, ".")
|
|
|
|
// Return the appropriate encoder/decoder according
|
|
|
|
// to the extension
|
|
|
|
switch ext {
|
|
|
|
case "yml", "yaml":
|
|
|
|
// YAML
|
|
|
|
return yamlEncoding{}
|
|
|
|
default:
|
|
|
|
// JSON
|
|
|
|
return jsonEncoding{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// toMarshaller returns the right marshal function according
|
|
|
|
// to the given file extension
|
|
|
|
func toMarshaller(ext string) func(interface{}) ([]byte, error) {
|
|
|
|
return ext2EncFormat(ext).Marshal
|
|
|
|
}
|
|
|
|
|
|
|
|
// toUnmarshaller returns the right marshal function according
|
|
|
|
// to the given file extension
|
|
|
|
func toUnmarshaller(ext string) func([]byte, interface{}) error {
|
|
|
|
return ext2EncFormat(ext).Unmarshal
|
|
|
|
}
|
|
|
|
|
|
|
|
// saveFileConfig marshals with the right encoding format
|
|
|
|
// according to the filename extension, if no extension is
|
|
|
|
// provided, json will be selected.
|
|
|
|
func saveFileConfig(filename string, v interface{}) error {
|
|
|
|
// Fetch filename's extension
|
|
|
|
ext := filepath.Ext(filename)
|
|
|
|
// Marshal data
|
|
|
|
dataBytes, err := toMarshaller(ext)(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
dataBytes = []byte(strings.Replace(string(dataBytes), "\n", "\r\n", -1))
|
|
|
|
}
|
|
|
|
// Save data.
|
|
|
|
return writeFile(filename, dataBytes)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-07-12 23:12:40 +02:00
|
|
|
func saveFileConfigEtcd(filename string, clnt *etcd.Client, v interface{}) error {
|
2018-04-23 20:05:56 +02:00
|
|
|
// Fetch filename's extension
|
|
|
|
ext := filepath.Ext(filename)
|
|
|
|
// Marshal data
|
|
|
|
dataBytes, err := toMarshaller(ext)(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
dataBytes = []byte(strings.Replace(string(dataBytes), "\n", "\r\n", -1))
|
|
|
|
}
|
|
|
|
|
2019-01-18 18:36:45 +01:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
2018-07-12 23:12:40 +02:00
|
|
|
defer cancel()
|
2019-01-18 18:36:45 +01:00
|
|
|
_, err = clnt.Put(ctx, filename, string(dataBytes))
|
|
|
|
if err == context.DeadlineExceeded {
|
|
|
|
return fmt.Errorf("etcd setup is unreachable, please check your endpoints %s", clnt.Endpoints())
|
|
|
|
} else if err != nil {
|
2019-12-02 18:28:01 +01:00
|
|
|
return fmt.Errorf("unexpected error %w returned by etcd setup, please check your endpoints %s", err, clnt.Endpoints())
|
2019-01-18 18:36:45 +01:00
|
|
|
}
|
|
|
|
return nil
|
2018-04-23 20:05:56 +02:00
|
|
|
}
|
|
|
|
|
2018-07-12 23:12:40 +02:00
|
|
|
func loadFileConfigEtcd(filename string, clnt *etcd.Client, v interface{}) error {
|
2019-01-18 18:36:45 +01:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
2018-07-12 23:12:40 +02:00
|
|
|
defer cancel()
|
2019-01-18 18:36:45 +01:00
|
|
|
resp, err := clnt.Get(ctx, filename)
|
2018-04-23 20:05:56 +02:00
|
|
|
if err != nil {
|
2019-01-18 18:36:45 +01:00
|
|
|
if err == context.DeadlineExceeded {
|
|
|
|
return fmt.Errorf("etcd setup is unreachable, please check your endpoints %s", clnt.Endpoints())
|
|
|
|
}
|
2019-12-02 18:28:01 +01:00
|
|
|
return fmt.Errorf("unexpected error %w returned by etcd setup, please check your endpoints %s", err, clnt.Endpoints())
|
2018-04-23 20:05:56 +02:00
|
|
|
}
|
2018-07-12 23:12:40 +02:00
|
|
|
if resp.Count == 0 {
|
2019-01-23 20:10:59 +01:00
|
|
|
return os.ErrNotExist
|
2018-07-12 23:12:40 +02:00
|
|
|
}
|
2018-04-23 20:05:56 +02:00
|
|
|
|
2018-07-12 23:12:40 +02:00
|
|
|
for _, ev := range resp.Kvs {
|
|
|
|
if string(ev.Key) == filename {
|
|
|
|
fileData := ev.Value
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
fileData = bytes.Replace(fileData, []byte("\r\n"), []byte("\n"), -1)
|
2018-04-23 20:05:56 +02:00
|
|
|
}
|
2018-07-12 23:12:40 +02:00
|
|
|
// Unmarshal file's content
|
|
|
|
return toUnmarshaller(filepath.Ext(filename))(fileData, v)
|
2018-04-23 20:05:56 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-23 20:10:59 +01:00
|
|
|
return os.ErrNotExist
|
2018-04-23 20:05:56 +02:00
|
|
|
}
|
|
|
|
|
2017-03-03 19:22:09 +01:00
|
|
|
// loadFileConfig unmarshals the file's content with the right
|
|
|
|
// decoder format according to the filename extension. If no
|
|
|
|
// extension is provided, json will be selected by default.
|
|
|
|
func loadFileConfig(filename string, v interface{}) error {
|
|
|
|
fileData, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
fileData = []byte(strings.Replace(string(fileData), "\r\n", "\n", -1))
|
|
|
|
}
|
2018-04-14 00:14:19 +02:00
|
|
|
|
2017-03-03 19:22:09 +01:00
|
|
|
// Unmarshal file's content
|
2017-03-09 19:13:30 +01:00
|
|
|
return toUnmarshaller(filepath.Ext(filename))(fileData, v)
|
2017-03-03 19:22:09 +01:00
|
|
|
}
|