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 mainmenu |
| 18 | |
| 19 | import ( |
| 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" |
Scott Baker | 555307d | 2019-11-04 08:58:01 -0800 | [diff] [blame] | 28 | "github.com/opencord/voltha-protos/v2/go/voltha" |
Don Newton | 2bdfd3f | 2019-04-08 17:06:33 -0400 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | func doDevice(enterPressed bool) { |
| 32 | fmt.Print(" ") |
| 33 | client := voltha.NewVolthaServiceClient(Conn) |
| 34 | devices, err := client.ListDevices(context.Background(), &empty.Empty{}) |
| 35 | items := devices.GetItems() |
| 36 | if err != nil { |
| 37 | fmt.Println(err) |
| 38 | } |
| 39 | deviceIds := []string{"exit", "quit"} |
| 40 | for i := 0; i < len(items); i++ { |
| 41 | deviceIds = append(deviceIds, items[i].Id) |
| 42 | } |
| 43 | var b = make([]byte, 1) |
| 44 | input := "" |
| 45 | |
| 46 | for { |
| 47 | os.Stdin.Read(b) |
| 48 | char := string(b) |
| 49 | if char == "\t" || char == "\n" || char == "?" { |
| 50 | fmt.Println("") |
| 51 | ret, prompt := util.Test(input, deviceIds) |
| 52 | if len(ret) == 1 { |
| 53 | input = ret[0] |
| 54 | if input == "quit" { |
| 55 | util.Exit(true) |
| 56 | } else if input == "exit" { |
| 57 | return |
| 58 | } |
| 59 | |
| 60 | devicemenu.MainLoop(Conn, input) |
| 61 | return |
| 62 | } else if len(ret) == 0 { |
| 63 | input = "" |
| 64 | fmt.Print("Invalid Input \ninput:") |
| 65 | } else { |
| 66 | |
| 67 | fmt.Println(ret) |
| 68 | input = prompt |
| 69 | fmt.Print("input: " + prompt) |
| 70 | } |
| 71 | } else if b[0] == 127 || char == "\b" { |
| 72 | |
| 73 | sz := len(input) |
| 74 | if sz > 0 { |
| 75 | fmt.Print("\b \b") |
| 76 | input = input[:sz-1] |
| 77 | } |
| 78 | if !(strings.HasPrefix(input, "device")) { |
| 79 | return |
| 80 | } |
| 81 | } else { |
| 82 | fmt.Print(char) |
| 83 | input += char |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | } |