blob: 8df6a2095c6cd3949dae4cf069fc6153d0b89fa3 [file] [log] [blame]
Don Newton2bdfd3f2019-04-08 17:06:33 -04001/*
Kent Hagerman0ab4cb22019-04-24 13:13:35 -04002 * Copyright 2018-present Open Networking Foundation
Don Newton2bdfd3f2019-04-08 17:06:33 -04003
Kent Hagerman0ab4cb22019-04-24 13:13:35 -04004 * 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 Newton2bdfd3f2019-04-08 17:06:33 -04007
Kent Hagerman0ab4cb22019-04-24 13:13:35 -04008 * http://www.apache.org/licenses/LICENSE-2.0
Don Newton2bdfd3f2019-04-08 17:06:33 -04009
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040010 * 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 Newton2bdfd3f2019-04-08 17:06:33 -040016
17package devicemenu
18
19import (
20 "fmt"
21 "os"
22 "strings"
23
24 "github.com/opencord/voltha-go/cli/util"
npujar16d6d5c2019-10-30 16:45:06 +053025 "github.com/opencord/voltha-lib-go/v2/pkg/log"
Don Newton2bdfd3f2019-04-08 17:06:33 -040026)
27
28func doHelp(enterPressed bool) {
29 input := ""
30 var b = make([]byte, 1)
31 inputPrompt := *InputPrompt + "help "
32 for {
npujar16d6d5c2019-10-30 16:45:06 +053033 _, err := os.Stdin.Read(b)
34 if err != nil {
35 log.Errorw("unable-to-read-from-stdin-file", log.Fields{"error": err})
36 }
Don Newton2bdfd3f2019-04-08 17:06:33 -040037 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}
81func baseHelp() {
82 message := `
83Documented commands (type help <topic>):
84========================================
85edit help img_dnld_cancel img_revert ports set
86eof history img_dnld_list list py shell
87exit images img_dnld_request pause run shortcuts
88flows img_activate img_dnld_status perf_config save show
89
90Miscellaneous help topics:
91==========================
92load
93
94Undocumented commands:
95======================
96quit
97
98`
99 fmt.Println(message)
100}