cli bones

This commit is contained in:
Sarah Jamie Lewis 2018-04-30 14:47:21 -07:00
parent 8721b8f455
commit c28af5f7ae
2 changed files with 105 additions and 0 deletions

44
app/app.go Normal file
View File

@ -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
}

61
app/cli/main.go Normal file
View File

@ -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")
}
}
}
}