github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/testhelpers/terminal/ui.go (about) 1 package terminal 2 3 import ( 4 "fmt" 5 "strings" 6 "sync" 7 "time" 8 9 "github.com/cloudfoundry/cli/cf" 10 "github.com/cloudfoundry/cli/cf/configuration/core_config" 11 term "github.com/cloudfoundry/cli/cf/terminal" 12 ) 13 14 const QuietPanic = "I should not print anything" 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 PanickedQuietly bool 26 ShowConfigurationCalled bool 27 28 sayMutex sync.Mutex 29 } 30 31 func (ui *FakeUI) PrintPaginator(rows []string, err error) { 32 if err != nil { 33 ui.Failed(err.Error()) 34 return 35 } 36 37 for _, row := range rows { 38 ui.Say(row) 39 } 40 } 41 42 func (ui *FakeUI) PrintCapturingNoOutput(message string, args ...interface{}) { 43 ui.sayMutex.Lock() 44 defer ui.sayMutex.Unlock() 45 46 message = fmt.Sprintf(message, args...) 47 ui.UncapturedOutput = append(ui.UncapturedOutput, strings.Split(message, "\n")...) 48 return 49 } 50 51 func (ui *FakeUI) Say(message string, args ...interface{}) { 52 ui.sayMutex.Lock() 53 defer ui.sayMutex.Unlock() 54 55 message = fmt.Sprintf(message, args...) 56 ui.Outputs = append(ui.Outputs, strings.Split(message, "\n")...) 57 return 58 } 59 60 func (ui *FakeUI) Warn(message string, args ...interface{}) { 61 message = fmt.Sprintf(message, args...) 62 ui.WarnOutputs = append(ui.WarnOutputs, strings.Split(message, "\n")...) 63 ui.Say(message, args...) 64 return 65 } 66 67 func (ui *FakeUI) Ask(prompt string, args ...interface{}) (answer string) { 68 ui.Prompts = append(ui.Prompts, fmt.Sprintf(prompt, args...)) 69 if len(ui.Inputs) == 0 { 70 panic("No input provided to Fake UI for prompt: " + fmt.Sprintf(prompt, args...)) 71 } 72 73 answer = ui.Inputs[0] 74 ui.Inputs = ui.Inputs[1:] 75 return 76 } 77 78 func (ui *FakeUI) ConfirmDelete(modelType, modelName string) bool { 79 return ui.Confirm( 80 "Really delete the %s %s?%s", 81 modelType, 82 term.EntityNameColor(modelName), 83 term.PromptColor(">")) 84 } 85 86 func (ui *FakeUI) ConfirmDeleteWithAssociations(modelType, modelName string) bool { 87 return ui.ConfirmDelete(modelType, modelName) 88 } 89 90 func (ui *FakeUI) Confirm(prompt string, args ...interface{}) bool { 91 response := ui.Ask(prompt, args...) 92 switch strings.ToLower(response) { 93 case "y", "yes": 94 return true 95 } 96 return false 97 } 98 99 func (ui *FakeUI) AskForPassword(prompt string, args ...interface{}) (answer string) { 100 ui.PasswordPrompts = append(ui.PasswordPrompts, fmt.Sprintf(prompt, args...)) 101 if len(ui.Inputs) == 0 { 102 println("__________________PANIC__________________") 103 println(ui.DumpOutputs()) 104 println(ui.DumpPrompts()) 105 println("_________________________________________") 106 panic("No input provided to Fake UI for prompt: " + fmt.Sprintf(prompt, args...)) 107 } 108 109 answer = ui.Inputs[0] 110 ui.Inputs = ui.Inputs[1:] 111 return 112 } 113 114 func (ui *FakeUI) Ok() { 115 ui.Say("OK") 116 } 117 118 func (ui *FakeUI) Failed(message string, args ...interface{}) { 119 ui.Say("FAILED") 120 ui.Say(message, args...) 121 panic(QuietPanic) 122 return 123 } 124 125 func (ui *FakeUI) PanicQuietly() { 126 ui.PanickedQuietly = true 127 } 128 129 func (ui *FakeUI) DumpWarnOutputs() string { 130 return "****************************\n" + strings.Join(ui.WarnOutputs, "\n") 131 } 132 133 func (ui *FakeUI) DumpOutputs() string { 134 return "****************************\n" + strings.Join(ui.Outputs, "\n") 135 } 136 137 func (ui *FakeUI) DumpPrompts() string { 138 return "****************************\n" + strings.Join(ui.Prompts, "\n") 139 } 140 141 func (ui *FakeUI) ClearOutputs() { 142 ui.Outputs = []string{} 143 } 144 145 func (ui *FakeUI) ShowConfiguration(config core_config.Reader) { 146 ui.ShowConfigurationCalled = true 147 } 148 149 func (ui FakeUI) LoadingIndication() { 150 } 151 152 func (c FakeUI) Wait(duration time.Duration) { 153 time.Sleep(duration) 154 } 155 156 func (ui *FakeUI) Table(headers []string) term.Table { 157 return term.NewTable(ui, headers) 158 } 159 160 func (ui *FakeUI) NotifyUpdateIfNeeded(config core_config.Reader) { 161 if !config.IsMinCliVersion(cf.Version) { 162 ui.Say("Cloud Foundry API version {{.ApiVer}} requires CLI version " + config.MinCliVersion() + " You are currently on version {{.CliVer}}. To upgrade your CLI, please visit: https://github.com/cloudfoundry/cli#downloads") 163 } 164 }