github.com/lukasheimann/cloudfoundrycli@v7.1.0+incompatible/cf/net/warnings_collector.go (about) 1 package net 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 8 "code.cloudfoundry.org/cli/cf/terminal" 9 ) 10 11 const DeprecatedEndpointWarning = "Endpoint deprecated" 12 13 type WarningsCollector struct { 14 ui terminal.UI 15 warningProducers []WarningProducer 16 } 17 18 //go:generate counterfeiter . WarningProducer 19 20 type WarningProducer interface { 21 Warnings() []string 22 } 23 24 func NewWarningsCollector(ui terminal.UI, warningsProducers ...WarningProducer) WarningsCollector { 25 return WarningsCollector{ 26 ui: ui, 27 warningProducers: warningsProducers, 28 } 29 } 30 31 func (warningsCollector WarningsCollector) PrintWarnings() error { 32 warnings := []string{} 33 for _, warningProducer := range warningsCollector.warningProducers { 34 for _, warning := range warningProducer.Warnings() { 35 if warning == DeprecatedEndpointWarning { 36 continue 37 } 38 warnings = append(warnings, warning) 39 } 40 } 41 42 if os.Getenv("CF_RAISE_ERROR_ON_WARNINGS") != "" { 43 if len(warnings) > 0 { 44 return fmt.Errorf(strings.Join(warnings, "\n")) 45 } 46 } 47 48 warnings = warningsCollector.removeDuplicates(warnings) 49 50 for _, warning := range warnings { 51 warningsCollector.ui.Warn(warning) 52 } 53 54 return nil 55 } 56 57 func (warningsCollector WarningsCollector) removeDuplicates(stringArray []string) []string { 58 length := len(stringArray) - 1 59 for i := 0; i < length; i++ { 60 for j := i + 1; j <= length; j++ { 61 if stringArray[i] == stringArray[j] { 62 stringArray[j] = stringArray[length] 63 stringArray = stringArray[0:length] 64 length-- 65 j-- 66 } 67 } 68 } 69 return stringArray 70 }