You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.6 KiB
69 lines
1.6 KiB
8 years ago
|
/*
|
||
7 years ago
|
* Minio Cloud Storage, (C) 2017, 2018 Minio, Inc.
|
||
8 years ago
|
*
|
||
|
* 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 cmd
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
)
|
||
|
|
||
7 years ago
|
// BoolFlag - wrapper bool type.
|
||
|
type BoolFlag bool
|
||
8 years ago
|
|
||
7 years ago
|
// String - returns string of BoolFlag.
|
||
|
func (bf BoolFlag) String() string {
|
||
8 years ago
|
if bf {
|
||
|
return "on"
|
||
|
}
|
||
|
|
||
|
return "off"
|
||
|
}
|
||
|
|
||
7 years ago
|
// MarshalJSON - converts BoolFlag into JSON data.
|
||
|
func (bf BoolFlag) MarshalJSON() ([]byte, error) {
|
||
8 years ago
|
return json.Marshal(bf.String())
|
||
|
}
|
||
|
|
||
7 years ago
|
// UnmarshalJSON - parses given data into BoolFlag.
|
||
|
func (bf *BoolFlag) UnmarshalJSON(data []byte) (err error) {
|
||
8 years ago
|
var s string
|
||
|
if err = json.Unmarshal(data, &s); err == nil {
|
||
7 years ago
|
b := BoolFlag(true)
|
||
8 years ago
|
if s == "" {
|
||
|
// Empty string is treated as valid.
|
||
|
*bf = b
|
||
7 years ago
|
} else if b, err = ParseBoolFlag(s); err == nil {
|
||
8 years ago
|
*bf = b
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return err
|
||
|
}
|
||
|
|
||
7 years ago
|
// ParseBoolFlag - parses string into BoolFlag.
|
||
|
func ParseBoolFlag(s string) (bf BoolFlag, err error) {
|
||
8 years ago
|
if s == "on" {
|
||
|
bf = true
|
||
|
} else if s == "off" {
|
||
|
bf = false
|
||
|
} else {
|
||
7 years ago
|
err = fmt.Errorf("invalid value ‘%s’ for BoolFlag", s)
|
||
8 years ago
|
}
|
||
|
|
||
|
return bf, err
|
||
|
}
|