storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/config/bool-flag.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2017-2019 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package config
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"strconv"
    23  	"strings"
    24  )
    25  
    26  // BoolFlag - wrapper bool type.
    27  type BoolFlag bool
    28  
    29  // String - returns string of BoolFlag.
    30  func (bf BoolFlag) String() string {
    31  	if bf {
    32  		return "on"
    33  	}
    34  
    35  	return "off"
    36  }
    37  
    38  // MarshalJSON - converts BoolFlag into JSON data.
    39  func (bf BoolFlag) MarshalJSON() ([]byte, error) {
    40  	return json.Marshal(bf.String())
    41  }
    42  
    43  // UnmarshalJSON - parses given data into BoolFlag.
    44  func (bf *BoolFlag) UnmarshalJSON(data []byte) (err error) {
    45  	var s string
    46  	if err = json.Unmarshal(data, &s); err == nil {
    47  		b := BoolFlag(true)
    48  		if s == "" {
    49  			// Empty string is treated as valid.
    50  			*bf = b
    51  		} else if b, err = ParseBoolFlag(s); err == nil {
    52  			*bf = b
    53  		}
    54  	}
    55  
    56  	return err
    57  }
    58  
    59  // FormatBool prints stringified version of boolean.
    60  func FormatBool(b bool) string {
    61  	if b {
    62  		return "on"
    63  	}
    64  	return "off"
    65  }
    66  
    67  // ParseBool returns the boolean value represented by the string.
    68  // It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False.
    69  // Any other value returns an error.
    70  func ParseBool(str string) (bool, error) {
    71  	switch str {
    72  	case "1", "t", "T", "true", "TRUE", "True", "on", "ON", "On":
    73  		return true, nil
    74  	case "0", "f", "F", "false", "FALSE", "False", "off", "OFF", "Off":
    75  		return false, nil
    76  	}
    77  	if strings.EqualFold(str, "enabled") {
    78  		return true, nil
    79  	}
    80  	if strings.EqualFold(str, "disabled") {
    81  		return false, nil
    82  	}
    83  	return false, fmt.Errorf("ParseBool: parsing '%s': %s", str, strconv.ErrSyntax)
    84  }
    85  
    86  // ParseBoolFlag - parses string into BoolFlag.
    87  func ParseBoolFlag(s string) (bf BoolFlag, err error) {
    88  	b, err := ParseBool(s)
    89  	return BoolFlag(b), err
    90  }