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 util |
| 18 | |
| 19 | import ( |
| 20 | "os" |
| 21 | "strings" |
| 22 | ) |
| 23 | |
npujar | 16d6d5c | 2019-10-30 16:45:06 +0530 | [diff] [blame] | 24 | // Test makes a bunch on new commands available |
Don Newton | 2bdfd3f | 2019-04-08 17:06:33 -0400 | [diff] [blame] | 25 | func Test(chars string, values []string) ([]string, string) { |
| 26 | |
| 27 | var ret []string |
| 28 | for i := 0; i < len(values); i++ { |
| 29 | if strings.HasPrefix(values[i], chars) { |
| 30 | ret = append(ret, values[i]) |
| 31 | } |
| 32 | } |
| 33 | if len(ret) == 0 { |
| 34 | return ret, "" |
| 35 | } |
| 36 | shortIndex := 0 |
| 37 | if len(ret) > 1 { |
| 38 | for i := 0; i < len(ret); i++ { |
| 39 | if len(ret[i]) < len(ret[shortIndex]) { |
| 40 | shortIndex = i |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | for i := len(chars); i < len(ret[shortIndex]); i++ { |
| 45 | inAllWords := true |
| 46 | for j := 0; j < len(ret); j++ { |
| 47 | inAllWords = inAllWords && ret[j][i] == ret[shortIndex][i] |
| 48 | } |
| 49 | if inAllWords { |
| 50 | chars += string(ret[shortIndex][i]) |
| 51 | } else { |
| 52 | return ret, chars |
| 53 | } |
| 54 | |
| 55 | } |
| 56 | |
| 57 | return ret, chars |
| 58 | } |
| 59 | |
npujar | 16d6d5c | 2019-10-30 16:45:06 +0530 | [diff] [blame] | 60 | // Route will fetch information with respect to table fields |
Don Newton | 2bdfd3f | 2019-04-08 17:06:33 -0400 | [diff] [blame] | 61 | func Route(command string, table map[string]func(bool), enterPressed bool) { |
| 62 | cmd := table[command] |
| 63 | cmd(enterPressed) |
| 64 | |
| 65 | } |
| 66 | |
npujar | 16d6d5c | 2019-10-30 16:45:06 +0530 | [diff] [blame] | 67 | // Exit will come out of connected session |
Don Newton | 2bdfd3f | 2019-04-08 17:06:33 -0400 | [diff] [blame] | 68 | func Exit(notUsed bool) { |
| 69 | os.Exit(0) |
| 70 | } |