2022-01-09 13:05:36 +10:30
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2022-01-13 14:26:16 +10:30
|
|
|
"log"
|
2022-01-15 18:20:31 +10:30
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/mattn/go-isatty"
|
2022-01-09 13:05:36 +10:30
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2022-01-13 14:26:16 +10:30
|
|
|
log.SetFlags(log.Lshortfile)
|
2022-01-15 14:04:13 +10:30
|
|
|
port := flag.Int("port", 9000, "Port to run server/client on.")
|
2022-01-15 23:11:05 +10:30
|
|
|
addr := flag.String("a", "127.0.0.1", "address to connect to.")
|
2022-01-09 13:05:36 +10:30
|
|
|
isServer := flag.Bool("s", false, "Set if running the server.")
|
2022-01-14 23:30:14 +10:30
|
|
|
isList := flag.Bool("l", false, "Set if requesting a list")
|
2022-01-15 18:20:31 +10:30
|
|
|
isSend := flag.Bool("c", false, "Set if sending a file (copy)")
|
|
|
|
isReceive := flag.Bool("p", false, "Set if receiving a file (paste)")
|
2022-01-14 23:30:14 +10:30
|
|
|
|
2022-01-09 13:05:36 +10:30
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if *isServer {
|
2022-01-15 14:04:13 +10:30
|
|
|
log.Printf("Server running on %d\n", *port)
|
2022-01-09 13:05:36 +10:30
|
|
|
s := Server{port: *port}
|
|
|
|
s.Run()
|
|
|
|
} else {
|
2022-01-15 18:20:31 +10:30
|
|
|
if !*isList && !*isSend && !*isReceive {
|
|
|
|
// try to work out the intent based on whether or not stdin/stdout
|
|
|
|
// are ttys
|
|
|
|
stdinTTY := isatty.IsTerminal(os.Stdin.Fd())
|
|
|
|
stdoutTTY := isatty.IsTerminal(os.Stdout.Fd())
|
|
|
|
|
|
|
|
if stdinTTY && !stdoutTTY {
|
|
|
|
*isReceive = true
|
|
|
|
|
|
|
|
} else if !stdinTTY && stdoutTTY {
|
|
|
|
*isSend = true
|
2022-01-15 23:11:21 +10:30
|
|
|
} else if !stdinTTY && !stdoutTTY {
|
|
|
|
log.Fatal("I can't cope with both stdin and stdout being pipes")
|
2022-01-15 18:20:31 +10:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if !*isList && !*isSend && !*isReceive {
|
|
|
|
// could default to list?
|
|
|
|
*isList = true
|
|
|
|
}
|
|
|
|
|
|
|
|
c := Client{port: *port, address: *addr, list: *isList, send: *isSend, receive: *isReceive}
|
2022-01-09 13:05:36 +10:30
|
|
|
err := c.Connect()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Print(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|