From c28af5f7aed412f2dc45f3d6c7bcff9affe7dfd9 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Mon, 30 Apr 2018 14:47:21 -0700 Subject: [PATCH] cli bones --- app/app.go | 44 +++++++++++++++++++++++++++++++++++ app/cli/main.go | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 app/app.go create mode 100644 app/cli/main.go diff --git a/app/app.go b/app/app.go new file mode 100644 index 0000000..c2ee00b --- /dev/null +++ b/app/app.go @@ -0,0 +1,44 @@ +package app + +import ( + "git.mascherari.press/cwtch/model" + "git.mascherari.press/cwtch/peer" +) + +type Application struct { + Peer *peer.CwtchPeer +} + +func (app *Application) NewProfile(name string, filename string) error { + profile := peer.NewCwtchPeer(name) + app.Peer = profile + return profile.Save(filename) +} + + +func (app *Application) SetProfile(filename string) error { + profile,err := peer.LoadCwtchPeer(filename) + app.Peer = profile + if err == nil { + app.Peer.Listen() + } + return err +} + +func (app *Application) PeerRequest(onion string) { + app.Peer.PeerWithOnion(onion) +} + +func (app *Application) SendMessageToPeer(onion string) { + +} + +func (app *Application) GetPeers() []model.PublicProfile { + + return nil +} + +func (app *Application) GetNewMessages() []model.Message { + return nil +} + diff --git a/app/cli/main.go b/app/cli/main.go new file mode 100644 index 0000000..f990244 --- /dev/null +++ b/app/cli/main.go @@ -0,0 +1,61 @@ +package main + +import ( + "fmt" + "bufio" + "os" + "strings" + app2 "git.mascherari.press/cwtch/app" +) + +func main() { + + quit := false + app := app2.Application{} + for !quit { + reader := bufio.NewReader(os.Stdin) + + profile := "unset" + if app.Peer != nil { + profile = app.Peer.Profile.Name + } + fmt.Printf("cwtch [%v]> ", profile) + text, _ := reader.ReadString('\n') + commands := strings.Split(text[0:len(text)-1], " ") + + switch commands[0] { + case "quit": + quit = true + case "newprofile": + if len(commands) == 3 { + err := app.NewProfile(commands[1], commands[2]) + if err == nil { + fmt.Printf("New profile created for %v\n", commands[1]) + } else { + fmt.Printf("Error creating profile for %v: %v\n", commands[1], err) + } + } else { + fmt.Printf("Error creating NewProfile, usage: newprofile [name] [filename]\n") + } + case "loadprofile": + if len(commands) == 2 { + err := app.SetProfile(commands[1]) + if err == nil { + fmt.Printf("Loaded profile for %v\n", commands[1]) + } else { + fmt.Printf("Error loading profile for %v: %v\n", commands[1], err) + } + } else { + fmt.Printf("Error Loading profile, usage: loadprofile [filename]\n") + } + + case "info": + if app.Peer != nil { + fmt.Printf("Address cwtch:%v\n", app.Peer.Profile.Onion) + } else { + fmt.Printf("Profile needs to be set") + } + } + } + +} \ No newline at end of file