VOL-1552 - inital stub
remove extra file
Change-Id: I9f5c93eefbde51c64a7c1b13244eca3a16de8a41
diff --git a/cli/util/menuProcessor.go b/cli/util/menuProcessor.go
new file mode 100644
index 0000000..c9ba837
--- /dev/null
+++ b/cli/util/menuProcessor.go
@@ -0,0 +1,102 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+
+ * http://www.apache.org/licenses/LICENSE-2.0
+
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+*/
+
+package util
+
+import (
+ "fmt"
+ "os"
+)
+
+/*
+ProcessTable parses table structure and executes functions
+*/
+func ProcessTable(functionTable map[string]func(bool), inputPrompt string) {
+ keys := []string{}
+ for k := range functionTable {
+ keys = append(keys, k)
+ }
+ var b = make([]byte, 1)
+ input := ""
+ fmt.Print(inputPrompt)
+ for {
+ os.Stdin.Read(b)
+ char := string(b)
+ if char == "\t" || char == "\n" || char == "?" {
+ fmt.Println("")
+ ret, prompt := Test(input, keys)
+ if len(ret) == 1 {
+ input = ret[0]
+ if input == "exit" {
+ return
+ }
+ if char == "\n" {
+ Route(input, functionTable, true)
+ } else {
+ Route(input, functionTable, false)
+ }
+ input = ""
+ fmt.Print(inputPrompt)
+ } else if len(ret) == 0 {
+ input = ""
+ fmt.Println("\nInvalid Input ")
+ fmt.Print(inputPrompt)
+ } else if len(ret) == 0 {
+ } else {
+
+ fmt.Println(ret)
+ input = prompt
+ fmt.Print(inputPrompt)
+ fmt.Print(prompt)
+ }
+ } else if char == " " {
+ _, ok := functionTable[input]
+ if ok {
+ Route(input, functionTable, false)
+ fmt.Print(inputPrompt)
+ input = ""
+ } else {
+ ret, prompt := Test(input, keys)
+ if len(ret) == 1 {
+ input = ret[0]
+ Route(input, functionTable, false)
+ input = ""
+ fmt.Print(inputPrompt)
+ } else if len(ret) == 0 {
+ input = ""
+ fmt.Println("\nInvalid Input ")
+ fmt.Print(inputPrompt)
+ } else {
+
+ fmt.Println(ret)
+ input = prompt
+ fmt.Print(inputPrompt + input)
+ }
+ }
+
+ } else if b[0] == 127 || char == "\b" {
+ sz := len(input)
+
+ if sz > 0 {
+ fmt.Print("\b \b")
+ input = input[:sz-1]
+ }
+ } else {
+ fmt.Print(char)
+ input += char
+ }
+ }
+}
diff --git a/cli/util/parseCmd.go b/cli/util/parseCmd.go
new file mode 100644
index 0000000..06ad8b8
--- /dev/null
+++ b/cli/util/parseCmd.go
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+
+ * http://www.apache.org/licenses/LICENSE-2.0
+
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+*/
+
+package util
+
+import (
+ "os"
+ "strings"
+)
+
+func Test(chars string, values []string) ([]string, string) {
+
+ var ret []string
+ for i := 0; i < len(values); i++ {
+ if strings.HasPrefix(values[i], chars) {
+ ret = append(ret, values[i])
+ }
+ }
+ if len(ret) == 0 {
+ return ret, ""
+ }
+ shortIndex := 0
+ if len(ret) > 1 {
+ for i := 0; i < len(ret); i++ {
+ if len(ret[i]) < len(ret[shortIndex]) {
+ shortIndex = i
+ }
+ }
+ }
+ for i := len(chars); i < len(ret[shortIndex]); i++ {
+ inAllWords := true
+ for j := 0; j < len(ret); j++ {
+ inAllWords = inAllWords && ret[j][i] == ret[shortIndex][i]
+ }
+ if inAllWords {
+ chars += string(ret[shortIndex][i])
+ } else {
+ return ret, chars
+ }
+
+ }
+
+ return ret, chars
+}
+
+func Route(command string, table map[string]func(bool), enterPressed bool) {
+ cmd := table[command]
+ cmd(enterPressed)
+
+}
+
+func Exit(notUsed bool) {
+ os.Exit(0)
+}
diff --git a/cli/util/tableGen.go b/cli/util/tableGen.go
new file mode 100644
index 0000000..3b07ef0
--- /dev/null
+++ b/cli/util/tableGen.go
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+
+ * http://www.apache.org/licenses/LICENSE-2.0
+
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+*/
+
+package util
+
+import (
+ "fmt"
+ "strings"
+)
+
+func BuildTable(keys []string, rows []map[string]string) (string, error) {
+ var returnString string
+ fieldSizes := make(map[string]int)
+
+ for i := 0; i < len(rows); i++ {
+ for key, value := range rows[i] {
+ currentSize := len(value)
+ if currentSize > fieldSizes[key] {
+ fieldSizes[key] = currentSize
+ }
+
+ }
+ }
+ for i := 0; i < len(keys); i++ {
+ currentSize := len(keys[i])
+ if currentSize > fieldSizes[keys[i]] {
+ fieldSizes[keys[i]] = currentSize
+ }
+ }
+ bottom := "+"
+
+ for i := 0; i < len(rows); i++ {
+ header := "|"
+ line := "|"
+ for j := 0; j < len(keys); j++ {
+ key := keys[j]
+ value := rows[i][key]
+ if i == 0 {
+ pad := 2 + fieldSizes[key] - len(key)
+ field := fmt.Sprintf("%s%s|", strings.Repeat(" ", pad), key)
+ spacer := fmt.Sprintf("%s+", strings.Repeat("-", fieldSizes[key]+2))
+ header = header + field
+ bottom = bottom + spacer
+ }
+ pad := 2 + fieldSizes[key] - len(value)
+ field := fmt.Sprintf("%s%s|", strings.Repeat(" ", pad), value)
+ line = line + field
+
+ }
+ if i == 0 {
+ returnString = bottom + "\n" + header + "\n" + bottom + "\n"
+ }
+
+ returnString = returnString + line + "\n"
+ }
+ returnString = returnString + bottom + "\n"
+
+ return returnString, nil
+
+}