lf/client.go

161 lines
2.7 KiB
Go
Raw Normal View History

2016-08-13 12:49:04 +00:00
package main
import (
"bufio"
"fmt"
"io"
2016-08-13 12:49:04 +00:00
"log"
"net"
"os"
2016-10-29 23:20:35 +00:00
"strings"
2016-08-13 12:49:04 +00:00
"github.com/nsf/termbox-go"
)
2017-11-19 18:55:13 +00:00
func run() {
2018-05-20 17:30:41 +00:00
f, err := os.Create(gLogPath)
2016-08-13 12:49:04 +00:00
if err != nil {
panic(err)
}
defer os.Remove(gLogPath)
2018-05-20 17:30:41 +00:00
defer f.Close()
log.SetOutput(f)
2016-08-13 12:49:04 +00:00
log.Print("hi!")
if err := termbox.Init(); err != nil {
log.Fatalf("initializing termbox: %s", err)
}
defer termbox.Close()
termbox.SetOutputMode(termbox.Output256)
app := newApp()
2016-08-13 12:49:04 +00:00
for _, path := range gConfigPaths {
if _, err := os.Stat(path); !os.IsNotExist(err) {
app.readFile(path)
}
}
2017-11-19 18:55:13 +00:00
app.loop()
2016-08-13 12:49:04 +00:00
}
2017-11-19 18:55:13 +00:00
func readExpr() <-chan expr {
2016-12-17 21:47:37 +00:00
ch := make(chan expr)
2016-10-29 23:20:35 +00:00
go func() {
2017-11-19 18:55:13 +00:00
c, err := net.Dial(gSocketProt, gSocketPath)
if err != nil {
log.Printf(fmt.Sprintf("connecting server: %s", err))
return
}
2016-12-17 21:47:37 +00:00
fmt.Fprintf(c, "conn %d\n", gClientID)
2016-10-29 23:20:35 +00:00
ch <- &callExpr{"sync", nil, 1}
2017-11-19 18:55:13 +00:00
2016-10-29 23:20:35 +00:00
s := bufio.NewScanner(c)
for s.Scan() {
log.Printf("recv: %s", s.Text())
2016-10-29 23:20:35 +00:00
p := newParser(strings.NewReader(s.Text()))
if p.parse() {
ch <- p.expr
}
}
c.Close()
}()
return ch
}
func saveFiles(list []string, cp bool) error {
c, err := net.Dial(gSocketProt, gSocketPath)
2016-08-13 12:49:04 +00:00
if err != nil {
return fmt.Errorf("dialing to save files: %s", err)
}
defer c.Close()
log.Printf("saving files: %v", list)
fmt.Fprintln(c, "save")
2016-08-13 12:49:04 +00:00
if cp {
fmt.Fprintln(c, "copy")
2016-08-13 12:49:04 +00:00
} else {
fmt.Fprintln(c, "move")
2016-08-13 12:49:04 +00:00
}
for _, f := range list {
fmt.Fprintln(c, f)
}
fmt.Fprintln(c)
2016-08-13 12:49:04 +00:00
return nil
}
func loadFiles() (list []string, cp bool, err error) {
c, e := net.Dial(gSocketProt, gSocketPath)
2016-08-13 12:49:04 +00:00
if e != nil {
err = fmt.Errorf("dialing to load files: %s", e)
return
}
defer c.Close()
fmt.Fprintln(c, "load")
s := bufio.NewScanner(c)
s.Scan()
switch s.Text() {
case "copy":
cp = true
2016-08-13 12:49:04 +00:00
case "move":
cp = false
2016-08-13 12:49:04 +00:00
default:
err = fmt.Errorf("unexpected option to copy file(s): %s", s.Text())
2016-08-13 12:49:04 +00:00
return
}
for s.Scan() && s.Text() != "" {
list = append(list, s.Text())
}
2016-08-13 12:49:04 +00:00
if s.Err() != nil {
err = fmt.Errorf("scanning file list: %s", s.Err())
return
}
log.Printf("loading files: %v", list)
return
}
func sendRemote(cmd string) error {
c, err := net.Dial(gSocketProt, gSocketPath)
if err != nil {
return fmt.Errorf("dialing to send server: %s", err)
}
fmt.Fprintln(c, cmd)
// XXX: Standard net.Conn interface does not include a CloseWrite method
// but net.UnixConn and net.TCPConn implement it so the following should be
// safe as long as we do not use other types of connections. We need
// CloseWrite to notify the server that this is not a persistent connection
// and it should be closed after the response.
if v, ok := c.(interface {
CloseWrite() error
}); ok {
v.CloseWrite()
}
io.Copy(os.Stdout, c)
c.Close()
return nil
}