github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/util/testhelpers/terminal/ui.go (about) 1 package terminal 2 3 import ( 4 "fmt" 5 "io" 6 "strings" 7 "sync" 8 "time" 9 10 "os" 11 12 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 13 term "code.cloudfoundry.org/cli/cf/terminal" 14 ) 15 16 type FakeUI struct { 17 outputs []string 18 uncapturedOutput []string 19 WarnOutputs []string 20 Prompts []string 21 PasswordPrompts []string 22 Inputs []string 23 FailedWithUsage bool 24 FailedWithUsageCommandName string 25 ShowConfigurationCalled bool 26 NotifyUpdateIfNeededCallCount int 27 28 sayMutex sync.Mutex 29 } 30 31 func (ui *FakeUI) Outputs() []string { 32 ui.sayMutex.Lock() 33 defer ui.sayMutex.Unlock() 34 35 return ui.outputs 36 } 37 38 func (ui *FakeUI) UncapturedOutput() []string { 39 ui.sayMutex.Lock() 40 defer ui.sayMutex.Unlock() 41 42 return ui.uncapturedOutput 43 } 44 45 func (ui *FakeUI) PrintPaginator(rows []string, err error) { 46 if err != nil { 47 ui.Failed(err.Error()) 48 return 49 } 50 51 for _, row := range rows { 52 ui.Say(row) 53 } 54 } 55 56 func (ui *FakeUI) Writer() io.Writer { 57 return os.Stdout 58 } 59 60 func (ui *FakeUI) PrintCapturingNoOutput(message string, args ...interface{}) { 61 ui.sayMutex.Lock() 62 defer ui.sayMutex.Unlock() 63 64 message = fmt.Sprintf(message, args...) 65 ui.uncapturedOutput = append(ui.uncapturedOutput, strings.Split(message, "\n")...) 66 return 67 } 68 69 func (ui *FakeUI) Say(message string, args ...interface{}) { 70 ui.sayMutex.Lock() 71 defer ui.sayMutex.Unlock() 72 73 message = fmt.Sprintf(message, args...) 74 ui.outputs = append(ui.outputs, strings.Split(message, "\n")...) 75 return 76 } 77 78 func (ui *FakeUI) Warn(message string, args ...interface{}) { 79 message = fmt.Sprintf(message, args...) 80 ui.WarnOutputs = append(ui.WarnOutputs, strings.Split(message, "\n")...) 81 ui.Say(message, args...) 82 return 83 } 84 85 func (ui *FakeUI) Ask(prompt string) string { 86 ui.Prompts = append(ui.Prompts, prompt) 87 88 if len(ui.Inputs) == 0 { 89 panic("No input provided to Fake UI for prompt: " + prompt) 90 } 91 92 answer := ui.Inputs[0] 93 ui.Inputs = ui.Inputs[1:] 94 return answer 95 } 96 97 func (ui *FakeUI) ConfirmDelete(modelType, modelName string) bool { 98 return ui.Confirm(fmt.Sprintf( 99 "Really delete the %s %s?%s", 100 modelType, 101 term.EntityNameColor(modelName), 102 term.PromptColor(">"))) 103 } 104 105 func (ui *FakeUI) ConfirmDeleteWithAssociations(modelType, modelName string) bool { 106 return ui.ConfirmDelete(modelType, modelName) 107 } 108 109 func (ui *FakeUI) Confirm(prompt string) bool { 110 response := ui.Ask(prompt) 111 switch strings.ToLower(response) { 112 case "y", "yes": 113 return true 114 } 115 return false 116 } 117 118 func (ui *FakeUI) AskForPassword(prompt string) string { 119 ui.PasswordPrompts = append(ui.PasswordPrompts, prompt) 120 121 if len(ui.Inputs) == 0 { 122 panic("No input provided to Fake UI for prompt: " + prompt) 123 } 124 125 answer := ui.Inputs[0] 126 ui.Inputs = ui.Inputs[1:] 127 return answer 128 } 129 130 func (ui *FakeUI) Ok() { 131 ui.Say("OK") 132 } 133 134 func (ui *FakeUI) Failed(message string, args ...interface{}) { 135 ui.Say("FAILED") 136 ui.Say(message, args...) 137 } 138 139 func (ui *FakeUI) DumpWarnOutputs() string { 140 return "****************************\n" + strings.Join(ui.WarnOutputs, "\n") 141 } 142 143 func (ui *FakeUI) DumpOutputs() string { 144 return "****************************\n" + strings.Join(ui.Outputs(), "\n") 145 } 146 147 func (ui *FakeUI) DumpPrompts() string { 148 return "****************************\n" + strings.Join(ui.Prompts, "\n") 149 } 150 151 func (ui *FakeUI) ClearOutputs() { 152 ui.sayMutex.Lock() 153 defer ui.sayMutex.Unlock() 154 155 ui.outputs = []string{} 156 } 157 158 func (ui *FakeUI) ShowConfiguration(config coreconfig.Reader) error { 159 ui.ShowConfigurationCalled = true 160 return nil 161 } 162 163 func (ui *FakeUI) LoadingIndication() { 164 } 165 166 func (ui *FakeUI) Wait(duration time.Duration) { 167 time.Sleep(duration) 168 } 169 170 func (ui *FakeUI) Table(headers []string) *term.UITable { 171 return &term.UITable{ 172 UI: ui, 173 Table: term.NewTable(headers), 174 } 175 } 176 177 func (ui *FakeUI) NotifyUpdateIfNeeded(config coreconfig.Reader) { 178 ui.NotifyUpdateIfNeededCallCount++ 179 }