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