Polish the cli interface

This commit is contained in:
2020-03-02 16:46:01 +11:00
parent 271acbde73
commit 1582b482b3
6 changed files with 69 additions and 52 deletions
+1 -1
View File
@@ -2,5 +2,5 @@ package index
type SearchIndex interface {
ListSearchableFields() []string
Search(field, value string)
Search(field, value string) interface{}
}
+5 -5
View File
@@ -1,7 +1,6 @@
package index
import (
"github.com/davecgh/go-spew/spew"
"github.com/wahyd4/zendesk/model"
)
@@ -17,8 +16,9 @@ func (index OrganisationIndex) ListSearchableFields() []string {
return result
}
func (index OrganisationIndex) Search(field, value string) {
organisations := index.Data[field][value]
spew.Dump(organisations)
func (index OrganisationIndex) Search(field, value string) interface{} {
if len(index.Data[field][value]) == 0 {
return nil
}
return index.Data[field][value]
}
+5 -6
View File
@@ -1,7 +1,6 @@
package index
import (
"github.com/davecgh/go-spew/spew"
"github.com/wahyd4/zendesk/model"
)
@@ -17,9 +16,9 @@ func (index TicketIndex) ListSearchableFields() []string {
return result
}
func (index TicketIndex) Search(field, value string) {
tickets := index.Data[field][value]
spew.Dump(tickets)
func (index TicketIndex) Search(field, value string) interface{} {
if len(index.Data[field][value]) == 0 {
return nil
}
return index.Data[field][value]
}
+5 -5
View File
@@ -1,7 +1,6 @@
package index
import (
"github.com/davecgh/go-spew/spew"
"github.com/wahyd4/zendesk/model"
)
@@ -17,8 +16,9 @@ func (index UserIndex) ListSearchableFields() []string {
return result
}
func (index UserIndex) Search(field, value string) {
users := index.Data[field][value]
spew.Dump(users)
func (index UserIndex) Search(field, value string) interface{} {
if len(index.Data[field][value]) == 0 {
return nil
}
return index.Data[field][value]
}
+39 -34
View File
@@ -2,6 +2,7 @@ package main
import (
"fmt"
"os"
"github.com/manifoldco/promptui"
"github.com/wahyd4/zendesk/search"
@@ -10,46 +11,50 @@ import (
func main() {
app := search.InitAPP("data/organizations.json", "data/users.json", "data/tickets.json")
if err := app.Parse(); err != nil {
panic("cannot load data: " + err.Error())
}
if err := app.BuildIndexes(); err != nil {
panic("failed to build indexes: " + err.Error())
}
for {
prompt := promptui.Select{
Label: "Select a type of data to search with",
Items: []string{search.OrganisationsKey, search.UsersKey, search.TicketsKey},
prompt := promptui.Select{
Label: "Select a type of data to search with",
Items: []string{search.OrganisationsKey, search.UsersKey, search.TicketsKey},
}
_, result, err := prompt.Run()
if err != nil {
fmt.Printf("Failed to process your input %v\n", err)
os.Exit(1)
}
app.UpdateDataType(result)
prompt = promptui.Select{
Label: "Please select a field to search with",
Items: app.ListSearchableFields(),
Size: 20,
}
_, fieldName, err := prompt.Run()
if err != nil {
fmt.Printf("Failed to process your input %v\n", err)
os.Exit(1)
}
app.UpdateFieldName(fieldName)
input := promptui.Prompt{
Label: "Please search by typing value",
}
value, _ := input.Run()
fmt.Printf("----- The value you typed is: %s ----- \n\n", value)
app.Search(value)
}
_, result, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}
app.UpdateDataType(result)
prompt = promptui.Select{
Label: "Select a field to search with",
Items: app.ListSearchableFields(),
Size: 20,
}
_, fieldName, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}
app.UpdateFieldName(fieldName)
input := promptui.Prompt{
Label: "Please search by typing value",
}
value, _ := input.Run()
fmt.Println("The value you typed is: " + value)
app.Search(value)
}
+14 -1
View File
@@ -1,5 +1,11 @@
package search
import (
"fmt"
"github.com/davecgh/go-spew/spew"
)
// SearchContext holds the information of current search context
type SearchContext struct {
dataType string
@@ -30,5 +36,12 @@ func (app *APP) Search(value string) {
dataType := app.searchContext.dataType
field := app.searchContext.fieldName
app.indexes[dataType].Search(field, value)
searchResult := app.indexes[dataType].Search(field, value)
if searchResult == nil {
fmt.Printf("Cannot find any matched result from %s with %s:%s \n\n", dataType, field, value)
return
}
spew.Dump(searchResult)
}