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" |
npujar | 16d6d5c | 2019-10-30 16:45:06 +0530 | [diff] [blame] | 25 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
Don Newton | 2bdfd3f | 2019-04-08 17:06:33 -0400 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | func doHelp(enterPressed bool) { |
| 29 | input := "" |
| 30 | var b = make([]byte, 1) |
| 31 | inputPrompt := *InputPrompt + "help " |
| 32 | for { |
npujar | 16d6d5c | 2019-10-30 16:45:06 +0530 | [diff] [blame] | 33 | _, err := os.Stdin.Read(b) |
| 34 | if err != nil { |
| 35 | log.Errorw("unable-to-read-from-stdin-file", log.Fields{"error": err}) |
| 36 | } |
Don Newton | 2bdfd3f | 2019-04-08 17:06:33 -0400 | [diff] [blame] | 37 | char := string(b) |
| 38 | if char == "\t" || char == "\n" || char == "?" { |
| 39 | if enterPressed { |
| 40 | baseHelp() |
| 41 | return |
| 42 | } |
| 43 | |
| 44 | fmt.Println("") |
| 45 | ret, prompt := util.Test(input, *Commands) |
| 46 | if len(ret) == 1 { |
| 47 | input = ret[0] |
| 48 | if input == "quit" { |
| 49 | util.Exit(true) |
| 50 | } else if input == "exit" { |
| 51 | return |
| 52 | } |
| 53 | |
| 54 | MainLoop(Conn, input) |
| 55 | return |
| 56 | } else if len(ret) == 0 { |
| 57 | input = "" |
| 58 | fmt.Print("Invalid Input \n" + inputPrompt) |
| 59 | } else { |
| 60 | |
| 61 | fmt.Println(ret) |
| 62 | input = prompt |
| 63 | fmt.Print(prompt + inputPrompt) |
| 64 | } |
| 65 | } else if b[0] == 127 || char == "\b" { |
| 66 | |
| 67 | sz := len(input) |
| 68 | if sz > 0 { |
| 69 | fmt.Print("\b \b") |
| 70 | input = input[:sz-1] |
| 71 | } |
| 72 | if !(strings.HasPrefix(input, "device")) { |
| 73 | return |
| 74 | } |
| 75 | } else { |
| 76 | fmt.Print(char) |
| 77 | input += char |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | func baseHelp() { |
| 82 | message := ` |
| 83 | Documented commands (type help <topic>): |
| 84 | ======================================== |
| 85 | edit help img_dnld_cancel img_revert ports set |
| 86 | eof history img_dnld_list list py shell |
| 87 | exit images img_dnld_request pause run shortcuts |
| 88 | flows img_activate img_dnld_status perf_config save show |
| 89 | |
| 90 | Miscellaneous help topics: |
| 91 | ========================== |
| 92 | load |
| 93 | |
| 94 | Undocumented commands: |
| 95 | ====================== |
| 96 | quit |
| 97 | |
| 98 | ` |
| 99 | fmt.Println(message) |
| 100 | } |