xfts/cmd/xftc/main.go

69 lines
1.1 KiB
Go
Raw Normal View History

2024-03-18 16:47:24 +00:00
package main
import (
"encoding/json"
"errors"
"fmt"
"net"
"os"
"github.com/apprehensions/xfts/internal/server"
)
func main() {
if len(os.Args) != 3 {
fmt.Fprintln(os.Stderr, "usage: %s src dest", os.Args[0])
os.Exit(1)
}
fi, err := os.Stat(os.Args[1])
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// xfiles sends EVERYTHING and ALL files to xfilesthumb
if fi.IsDir() || fi.Size() < 1 {
fmt.Println("skipping")
os.Exit(1)
}
err = send(server.Request{
Source: os.Args[1],
Destination: os.Args[2],
})
if err != nil {
fmt.Fprintf(os.Stderr, "send: %s\n", err)
os.Exit(1)
}
}
func send(r server.Request) error {
if _, err := os.Stat(r.Source); err != nil {
return err
}
conn, err := net.Dial("unix", server.Socket)
if err != nil {
return err
}
defer conn.Close()
if err := json.NewEncoder(conn).Encode(r); err != nil {
return err
}
buf := make([]byte, 1024)
n, err := conn.Read(buf)
if err != nil {
return err
}
resp := string(buf[:n])
if _, err := os.Stat(r.Destination); err != nil {
return errors.New(resp)
}
return nil
}