blob: aa7858981077dc62be68e1c9a8139226b58bb598 [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 util
18
19import (
20 "os"
21 "strings"
22)
23
npujar16d6d5c2019-10-30 16:45:06 +053024// Test makes a bunch on new commands available
Don Newton2bdfd3f2019-04-08 17:06:33 -040025func 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
npujar16d6d5c2019-10-30 16:45:06 +053060// Route will fetch information with respect to table fields
Don Newton2bdfd3f2019-04-08 17:06:33 -040061func Route(command string, table map[string]func(bool), enterPressed bool) {
62 cmd := table[command]
63 cmd(enterPressed)
64
65}
66
npujar16d6d5c2019-10-30 16:45:06 +053067// Exit will come out of connected session
Don Newton2bdfd3f2019-04-08 17:06:33 -040068func Exit(notUsed bool) {
69 os.Exit(0)
70}