2019-10-30 03:04:39 -04:00
|
|
|
/*
|
|
|
|
* MinIO Cloud Storage, (C) 2019 MinIO, Inc.
|
|
|
|
*
|
|
|
|
* 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 madmin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2019-11-13 20:38:05 -05:00
|
|
|
"unicode"
|
2019-10-30 03:04:39 -04:00
|
|
|
)
|
|
|
|
|
2019-11-20 18:10:24 -05:00
|
|
|
// KV - is a shorthand of each key value.
|
|
|
|
type KV struct {
|
|
|
|
Key string `json:"key"`
|
|
|
|
Value string `json:"value"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// KVS - is a shorthand for some wrapper functions
|
|
|
|
// to operate on list of key values.
|
|
|
|
type KVS []KV
|
|
|
|
|
|
|
|
// Empty - return if kv is empty
|
|
|
|
func (kvs KVS) Empty() bool {
|
|
|
|
return len(kvs) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get - returns the value of a key, if not found returns empty.
|
|
|
|
func (kvs KVS) Get(key string) string {
|
|
|
|
v, ok := kvs.Lookup(key)
|
|
|
|
if ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup - lookup a key in a list of KVS
|
|
|
|
func (kvs KVS) Lookup(key string) (string, bool) {
|
|
|
|
for _, kv := range kvs {
|
|
|
|
if kv.Key == key {
|
|
|
|
return kv.Value, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
2019-10-30 03:04:39 -04:00
|
|
|
|
2019-12-03 13:50:20 -05:00
|
|
|
// Target signifies an individual target
|
|
|
|
type Target struct {
|
|
|
|
SubSystem string `json:"subSys"`
|
|
|
|
KVS KVS `json:"kvs"`
|
|
|
|
}
|
|
|
|
|
2019-10-30 03:04:39 -04:00
|
|
|
// Targets sub-system targets
|
2019-12-03 13:50:20 -05:00
|
|
|
type Targets []Target
|
2019-10-30 03:04:39 -04:00
|
|
|
|
2019-12-03 13:50:20 -05:00
|
|
|
// Standard config keys and values.
|
2019-10-30 03:04:39 -04:00
|
|
|
const (
|
2019-12-03 13:50:20 -05:00
|
|
|
StateKey = "state"
|
|
|
|
CommentKey = "comment"
|
|
|
|
|
|
|
|
// State values
|
|
|
|
StateOn = "on"
|
|
|
|
StateOff = "off"
|
2019-10-30 03:04:39 -04:00
|
|
|
)
|
|
|
|
|
2019-11-13 20:38:05 -05:00
|
|
|
func (kvs KVS) String() string {
|
|
|
|
var s strings.Builder
|
2019-11-20 18:10:24 -05:00
|
|
|
for _, kv := range kvs {
|
2019-12-03 13:50:20 -05:00
|
|
|
// Do not need to print state which is on.
|
|
|
|
if kv.Key == StateKey && kv.Value == StateOn {
|
2019-11-13 20:38:05 -05:00
|
|
|
continue
|
|
|
|
}
|
2019-12-03 13:50:20 -05:00
|
|
|
if kv.Key == CommentKey && kv.Value == "" {
|
2019-11-16 17:57:36 -05:00
|
|
|
continue
|
|
|
|
}
|
2019-11-20 18:10:24 -05:00
|
|
|
s.WriteString(kv.Key)
|
2019-11-13 20:38:05 -05:00
|
|
|
s.WriteString(KvSeparator)
|
2019-11-20 18:10:24 -05:00
|
|
|
spc := hasSpace(kv.Value)
|
2019-11-13 20:38:05 -05:00
|
|
|
if spc {
|
|
|
|
s.WriteString(KvDoubleQuote)
|
|
|
|
}
|
2019-11-20 18:10:24 -05:00
|
|
|
s.WriteString(kv.Value)
|
2019-11-13 20:38:05 -05:00
|
|
|
if spc {
|
|
|
|
s.WriteString(KvDoubleQuote)
|
|
|
|
}
|
|
|
|
s.WriteString(KvSpaceSeparator)
|
|
|
|
}
|
|
|
|
return s.String()
|
|
|
|
}
|
|
|
|
|
2019-11-12 06:16:25 -05:00
|
|
|
// Count - returns total numbers of target
|
|
|
|
func (t Targets) Count() int {
|
2019-12-03 13:50:20 -05:00
|
|
|
return len(t)
|
2019-11-12 06:16:25 -05:00
|
|
|
}
|
|
|
|
|
2019-11-13 20:38:05 -05:00
|
|
|
func hasSpace(s string) bool {
|
|
|
|
for _, r := range s {
|
|
|
|
if unicode.IsSpace(r) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-10-30 03:04:39 -04:00
|
|
|
func (t Targets) String() string {
|
|
|
|
var s strings.Builder
|
2019-11-12 06:16:25 -05:00
|
|
|
count := t.Count()
|
2019-12-03 13:50:20 -05:00
|
|
|
// Print all "on" states entries
|
|
|
|
for _, targetKV := range t {
|
|
|
|
kv := targetKV.KVS
|
|
|
|
count--
|
|
|
|
s.WriteString(targetKV.SubSystem)
|
|
|
|
s.WriteString(KvSpaceSeparator)
|
|
|
|
s.WriteString(kv.String())
|
|
|
|
if len(t) > 1 && count > 0 {
|
|
|
|
s.WriteString(KvNewline)
|
2019-10-30 03:04:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return s.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constant separators
|
|
|
|
const (
|
|
|
|
SubSystemSeparator = `:`
|
|
|
|
KvSeparator = `=`
|
2019-12-03 13:50:20 -05:00
|
|
|
KvComment = `#`
|
2019-10-30 03:04:39 -04:00
|
|
|
KvSpaceSeparator = ` `
|
2019-11-05 09:18:26 -05:00
|
|
|
KvNewline = "\n"
|
2019-10-30 03:04:39 -04:00
|
|
|
KvDoubleQuote = `"`
|
|
|
|
KvSingleQuote = `'`
|
|
|
|
|
2019-11-05 09:18:26 -05:00
|
|
|
Default = `_`
|
2019-10-30 03:04:39 -04:00
|
|
|
)
|
|
|
|
|
2019-12-03 13:50:20 -05:00
|
|
|
// SanitizeValue - this function is needed, to trim off single or double quotes, creeping into the values.
|
|
|
|
func SanitizeValue(v string) string {
|
2019-10-30 03:04:39 -04:00
|
|
|
v = strings.TrimSuffix(strings.TrimPrefix(strings.TrimSpace(v), KvDoubleQuote), KvDoubleQuote)
|
|
|
|
return strings.TrimSuffix(strings.TrimPrefix(v, KvSingleQuote), KvSingleQuote)
|
|
|
|
}
|
|
|
|
|
2019-12-03 13:50:20 -05:00
|
|
|
// AddTarget - adds new targets, by parsing the input string s.
|
|
|
|
func (t *Targets) AddTarget(s string) error {
|
2019-10-30 03:04:39 -04:00
|
|
|
inputs := strings.SplitN(s, KvSpaceSeparator, 2)
|
|
|
|
if len(inputs) <= 1 {
|
|
|
|
return fmt.Errorf("invalid number of arguments '%s'", s)
|
|
|
|
}
|
|
|
|
subSystemValue := strings.SplitN(inputs[0], SubSystemSeparator, 2)
|
|
|
|
if len(subSystemValue) == 0 {
|
|
|
|
return fmt.Errorf("invalid number of arguments %s", s)
|
|
|
|
}
|
|
|
|
var kvs = KVS{}
|
|
|
|
var prevK string
|
|
|
|
for _, v := range strings.Fields(inputs[1]) {
|
|
|
|
kv := strings.SplitN(v, KvSeparator, 2)
|
|
|
|
if len(kv) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(kv) == 1 && prevK != "" {
|
2019-11-20 18:10:24 -05:00
|
|
|
kvs = append(kvs, KV{
|
|
|
|
Key: prevK,
|
2019-12-03 13:50:20 -05:00
|
|
|
Value: strings.Join([]string{kvs.Get(prevK), SanitizeValue(kv[0])}, KvSpaceSeparator),
|
2019-11-20 18:10:24 -05:00
|
|
|
})
|
2019-10-30 03:04:39 -04:00
|
|
|
continue
|
|
|
|
}
|
2019-11-12 06:16:25 -05:00
|
|
|
if len(kv) == 1 {
|
2019-10-30 03:04:39 -04:00
|
|
|
return fmt.Errorf("value for key '%s' cannot be empty", kv[0])
|
|
|
|
}
|
|
|
|
prevK = kv[0]
|
2019-11-20 18:10:24 -05:00
|
|
|
kvs = append(kvs, KV{
|
|
|
|
Key: kv[0],
|
2019-12-03 13:50:20 -05:00
|
|
|
Value: SanitizeValue(kv[1]),
|
2019-11-20 18:10:24 -05:00
|
|
|
})
|
2019-10-30 03:04:39 -04:00
|
|
|
}
|
|
|
|
|
2019-12-03 13:50:20 -05:00
|
|
|
*t = append(*t, Target{
|
|
|
|
SubSystem: inputs[0],
|
|
|
|
KVS: kvs,
|
|
|
|
})
|
|
|
|
|
2019-10-30 03:04:39 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-05 09:18:26 -05:00
|
|
|
// ParseSubSysTarget - parse sub-system target
|
|
|
|
func ParseSubSysTarget(buf []byte) (Targets, error) {
|
2019-12-03 13:50:20 -05:00
|
|
|
var targets Targets
|
2019-10-30 03:04:39 -04:00
|
|
|
bio := bufio.NewScanner(bytes.NewReader(buf))
|
|
|
|
for bio.Scan() {
|
2019-12-03 13:50:20 -05:00
|
|
|
if err := targets.AddTarget(bio.Text()); err != nil {
|
2019-10-30 03:04:39 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := bio.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return targets, nil
|
|
|
|
}
|