github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/preflight/util/util.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package util 21 22 import ( 23 "strconv" 24 25 "github.com/pkg/errors" 26 troubleshoot "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" 27 "github.com/replicatedhq/troubleshoot/pkg/multitype" 28 ) 29 30 // TitleOrDefault extracts titleName from metaInfo, and returns default if metaInfo is unhelpful 31 func TitleOrDefault[T troubleshoot.HostCollectorMeta | troubleshoot.AnalyzeMeta](meta T, defaultTitle string) string { 32 var title string 33 iMeta := (interface{})(meta) 34 switch tmp := iMeta.(type) { 35 case troubleshoot.HostCollectorMeta: 36 title = tmp.CollectorName 37 case troubleshoot.AnalyzeMeta: 38 title = tmp.CheckName 39 default: 40 } 41 if title == "" { 42 title = defaultTitle 43 } 44 return title 45 } 46 47 func IsExcluded(excludeVal *multitype.BoolOrString) (bool, error) { 48 if excludeVal == nil { 49 return false, nil 50 } 51 if excludeVal.Type == multitype.Bool { 52 return excludeVal.BoolVal, nil 53 } 54 if excludeVal.StrVal == "" { 55 return false, nil 56 } 57 parsed, err := strconv.ParseBool(excludeVal.StrVal) 58 if err != nil { 59 return false, errors.Wrap(err, "failed to parse bool string") 60 } 61 return parsed, nil 62 }