app/cli export-group properly plugged into autocomplete; autocomplete doesnt seg fault when peer isn't initialized

This commit is contained in:
Dan Ballard 2018-08-01 19:58:00 -05:00
parent 16d21f582e
commit 0f6c0edc46
1 changed files with 8 additions and 2 deletions

View File

@ -29,6 +29,7 @@ var suggestions = []prompt.Suggest{
{Text: "peers", Description: "retrieve a list of peers and their connection status"}, {Text: "peers", Description: "retrieve a list of peers and their connection status"},
{Text: "contacts", Description: "retrieve a list of contacts"}, {Text: "contacts", Description: "retrieve a list of contacts"},
{Text: "groups", Description: "retrieve a list of groups"}, {Text: "groups", Description: "retrieve a list of groups"},
{Text: "export-group", Description: "export a group invite: prints as a string"},
{Text: "send", Description: "send a message to a group"}, {Text: "send", Description: "send a message to a group"},
{Text: "timeline", Description: "read the timeline of a given group"}, {Text: "timeline", Description: "read the timeline of a given group"},
{Text: "accept-invite", Description: "accept the invite of a group"}, {Text: "accept-invite", Description: "accept the invite of a group"},
@ -48,6 +49,7 @@ var usages = map[string]string{
"peers": "", "peers": "",
"contacts": "", "contacts": "",
"groups": "", "groups": "",
"export-group": "export-group [groupid]",
"info": "", "info": "",
"send": "send [groupid] [message]", "send": "send [groupid] [message]",
"timeline": "timeline [groupid]", "timeline": "timeline [groupid]",
@ -64,12 +66,16 @@ func completer(d prompt.Document) []prompt.Suggest {
var s []prompt.Suggest var s []prompt.Suggest
if app.Peer == nil {
return s
}
if d.FindStartOfPreviousWord() == 0 { if d.FindStartOfPreviousWord() == 0 {
return prompt.FilterHasPrefix(suggestions, d.GetWordBeforeCursor(), true) return prompt.FilterHasPrefix(suggestions, d.GetWordBeforeCursor(), true)
} }
w := d.CurrentLine() w := d.CurrentLine()
if strings.HasPrefix(w, "send") || strings.HasPrefix(w, "timeline") { if strings.HasPrefix(w, "send") || strings.HasPrefix(w, "timeline") || strings.HasPrefix(w, "export-group") {
s = []prompt.Suggest{} s = []prompt.Suggest{}
groups := app.Peer.GetGroups() groups := app.Peer.GetGroups()
for _, groupID := range groups { for _, groupID := range groups {
@ -390,7 +396,7 @@ func main() {
fmt.Printf("Invite: %v\n", invite) fmt.Printf("Invite: %v\n", invite)
} }
} else { } else {
fmt.Printf("Error reading timeline from group, usage: %s\n", usages["timeline"]) fmt.Printf("Error exporting group, usage: %s\n", usages["export-group"])
} }
case "save": case "save":
app.Peer.Save() app.Peer.Save()