blob: 23f71a56052ead686504faa91fcef4278df50858 [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 mainmenu
18
19import (
20 "context"
21 "fmt"
22 "os"
23 "strings"
24
25 "github.com/golang/protobuf/ptypes/empty"
26 "github.com/opencord/voltha-go/cli/menu/devicemenu"
27 "github.com/opencord/voltha-go/cli/util"
npujar16d6d5c2019-10-30 16:45:06 +053028 "github.com/opencord/voltha-lib-go/v2/pkg/log"
Scott Baker555307d2019-11-04 08:58:01 -080029 "github.com/opencord/voltha-protos/v2/go/voltha"
Don Newton2bdfd3f2019-04-08 17:06:33 -040030)
31
32func doDevice(enterPressed bool) {
33 fmt.Print(" ")
34 client := voltha.NewVolthaServiceClient(Conn)
35 devices, err := client.ListDevices(context.Background(), &empty.Empty{})
36 items := devices.GetItems()
37 if err != nil {
38 fmt.Println(err)
39 }
npujar16d6d5c2019-10-30 16:45:06 +053040 deviceIDs := []string{"exit", "quit"}
Don Newton2bdfd3f2019-04-08 17:06:33 -040041 for i := 0; i < len(items); i++ {
npujar16d6d5c2019-10-30 16:45:06 +053042 deviceIDs = append(deviceIDs, items[i].Id)
Don Newton2bdfd3f2019-04-08 17:06:33 -040043 }
44 var b = make([]byte, 1)
45 input := ""
46
47 for {
npujar16d6d5c2019-10-30 16:45:06 +053048 _, err := os.Stdin.Read(b)
49 if err != nil {
50 log.Errorw("unable-to-read-from-stdin-file", log.Fields{"error": err})
51 }
Don Newton2bdfd3f2019-04-08 17:06:33 -040052 char := string(b)
53 if char == "\t" || char == "\n" || char == "?" {
54 fmt.Println("")
npujar16d6d5c2019-10-30 16:45:06 +053055 ret, prompt := util.Test(input, deviceIDs)
Don Newton2bdfd3f2019-04-08 17:06:33 -040056 if len(ret) == 1 {
57 input = ret[0]
58 if input == "quit" {
59 util.Exit(true)
60 } else if input == "exit" {
61 return
62 }
63
64 devicemenu.MainLoop(Conn, input)
65 return
66 } else if len(ret) == 0 {
67 input = ""
68 fmt.Print("Invalid Input \ninput:")
69 } else {
70
71 fmt.Println(ret)
72 input = prompt
73 fmt.Print("input: " + prompt)
74 }
75 } else if b[0] == 127 || char == "\b" {
76
77 sz := len(input)
78 if sz > 0 {
79 fmt.Print("\b \b")
80 input = input[:sz-1]
81 }
82 if !(strings.HasPrefix(input, "device")) {
83 return
84 }
85 } else {
86 fmt.Print(char)
87 input += char
88 }
89 }
90
91}