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" |
npujar | 16d6d5c | 2019-10-30 16:45:06 +0530 | [diff] [blame] | 28 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
Scott Baker | 555307d | 2019-11-04 08:58:01 -0800 | [diff] [blame] | 29 | "github.com/opencord/voltha-protos/v2/go/voltha" |
Don Newton | 2bdfd3f | 2019-04-08 17:06:33 -0400 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | func 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 | } |
npujar | 16d6d5c | 2019-10-30 16:45:06 +0530 | [diff] [blame] | 40 | deviceIDs := []string{"exit", "quit"} |
Don Newton | 2bdfd3f | 2019-04-08 17:06:33 -0400 | [diff] [blame] | 41 | for i := 0; i < len(items); i++ { |
npujar | 16d6d5c | 2019-10-30 16:45:06 +0530 | [diff] [blame] | 42 | deviceIDs = append(deviceIDs, items[i].Id) |
Don Newton | 2bdfd3f | 2019-04-08 17:06:33 -0400 | [diff] [blame] | 43 | } |
| 44 | var b = make([]byte, 1) |
| 45 | input := "" |
| 46 | |
| 47 | for { |
npujar | 16d6d5c | 2019-10-30 16:45:06 +0530 | [diff] [blame] | 48 | _, err := os.Stdin.Read(b) |
| 49 | if err != nil { |
| 50 | log.Errorw("unable-to-read-from-stdin-file", log.Fields{"error": err}) |
| 51 | } |
Don Newton | 2bdfd3f | 2019-04-08 17:06:33 -0400 | [diff] [blame] | 52 | char := string(b) |
| 53 | if char == "\t" || char == "\n" || char == "?" { |
| 54 | fmt.Println("") |
npujar | 16d6d5c | 2019-10-30 16:45:06 +0530 | [diff] [blame] | 55 | ret, prompt := util.Test(input, deviceIDs) |
Don Newton | 2bdfd3f | 2019-04-08 17:06:33 -0400 | [diff] [blame] | 56 | 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 | } |