github.com/containerd/nerdctl@v1.7.7/pkg/maputil/maputil.go (about) 1 /* 2 Copyright The containerd Authors. 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 maputil 18 19 import ( 20 "fmt" 21 "strconv" 22 ) 23 24 // MapBoolValueAsOpt will parse key as a command-line option. 25 // If only key is specified will be treated as true, 26 // otherwise, the value will be parsed and returned. 27 // This is useful when command line flags have options. 28 // Following examples illustrate this: 29 // --security-opt xxx returns true 30 // --security-opt xxx=true returns true 31 // --security-opt xxx=false returns false 32 // --security-opt xxx=invalid returns false and error 33 func MapBoolValueAsOpt(m map[string]string, key string) (bool, error) { 34 if str, ok := m[key]; ok { 35 if str == "" { 36 return true, nil 37 } 38 b, err := strconv.ParseBool(str) 39 if err != nil { 40 return false, fmt.Errorf("invalid \"%s\" value: %q: %w", key, str, err) 41 } 42 return b, nil 43 } 44 45 return false, nil 46 }