Don Newton | 2bdfd3f | 2019-04-08 17:06:33 -0400 | [diff] [blame] | 1 | /* |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Don Newton | 2bdfd3f | 2019-04-08 17:06:33 -0400 | [diff] [blame] | 3 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 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 |
Don Newton | 2bdfd3f | 2019-04-08 17:06:33 -0400 | [diff] [blame] | 7 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Don Newton | 2bdfd3f | 2019-04-08 17:06:33 -0400 | [diff] [blame] | 9 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 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 | */ |
Don Newton | 2bdfd3f | 2019-04-08 17:06:33 -0400 | [diff] [blame] | 16 | |
| 17 | package devicemenu |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "os" |
| 22 | "strings" |
| 23 | |
| 24 | "github.com/opencord/voltha-go/cli/util" |
| 25 | ) |
| 26 | |
| 27 | func doHelp(enterPressed bool) { |
| 28 | input := "" |
| 29 | var b = make([]byte, 1) |
| 30 | inputPrompt := *InputPrompt + "help " |
| 31 | for { |
| 32 | os.Stdin.Read(b) |
| 33 | char := string(b) |
| 34 | if char == "\t" || char == "\n" || char == "?" { |
| 35 | if enterPressed { |
| 36 | baseHelp() |
| 37 | return |
| 38 | } |
| 39 | |
| 40 | fmt.Println("") |
| 41 | ret, prompt := util.Test(input, *Commands) |
| 42 | if len(ret) == 1 { |
| 43 | input = ret[0] |
| 44 | if input == "quit" { |
| 45 | util.Exit(true) |
| 46 | } else if input == "exit" { |
| 47 | return |
| 48 | } |
| 49 | |
| 50 | MainLoop(Conn, input) |
| 51 | return |
| 52 | } else if len(ret) == 0 { |
| 53 | input = "" |
| 54 | fmt.Print("Invalid Input \n" + inputPrompt) |
| 55 | } else { |
| 56 | |
| 57 | fmt.Println(ret) |
| 58 | input = prompt |
| 59 | fmt.Print(prompt + inputPrompt) |
| 60 | } |
| 61 | } else if b[0] == 127 || char == "\b" { |
| 62 | |
| 63 | sz := len(input) |
| 64 | if sz > 0 { |
| 65 | fmt.Print("\b \b") |
| 66 | input = input[:sz-1] |
| 67 | } |
| 68 | if !(strings.HasPrefix(input, "device")) { |
| 69 | return |
| 70 | } |
| 71 | } else { |
| 72 | fmt.Print(char) |
| 73 | input += char |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | func baseHelp() { |
| 78 | message := ` |
| 79 | Documented commands (type help <topic>): |
| 80 | ======================================== |
| 81 | edit help img_dnld_cancel img_revert ports set |
| 82 | eof history img_dnld_list list py shell |
| 83 | exit images img_dnld_request pause run shortcuts |
| 84 | flows img_activate img_dnld_status perf_config save show |
| 85 | |
| 86 | Miscellaneous help topics: |
| 87 | ========================== |
| 88 | load |
| 89 | |
| 90 | Undocumented commands: |
| 91 | ====================== |
| 92 | quit |
| 93 | |
| 94 | ` |
| 95 | fmt.Println(message) |
| 96 | } |