github.com/openshift-online/ocm-sdk-go@v0.1.473/logging/list.go (about) 1 /* 2 Copyright (c) 2021 Red Hat, 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 // This file contains functions useful for printing lists in a format that is easy to read for 18 // humans in log files. 19 20 package logging 21 22 import ( 23 "fmt" 24 "strings" 25 ) 26 27 // All generates a human readable representation of the given list of strings, for use in a log 28 // file. It puts quotes around each item, separates the first items with commas and the last with 29 // the word 'and'. 30 func All(items []string) string { 31 return list(items, "and") 32 } 33 34 // any generates a human readable representation of the given list of strings, for use in a log 35 // file. It puts quotes around each item, separates the first items with commas and the last with 36 // the word 'or'. 37 func Any(items []string) string { 38 return list(items, "or") 39 } 40 41 func list(items []string, conjunction string) string { 42 count := len(items) 43 if count == 0 { 44 return "" 45 } 46 quoted := make([]string, len(items)) 47 for i, item := range items { 48 quoted[i] = fmt.Sprintf("'%s'", item) 49 } 50 if count == 1 { 51 return quoted[0] 52 } 53 head := quoted[0 : count-1] 54 tail := quoted[count-1] 55 return fmt.Sprintf("%s %s %s", strings.Join(head, ", "), conjunction, tail) 56 }