netgiv/main.go

57 lines
1.3 KiB
Go
Raw Normal View History

package main
import (
"flag"
"fmt"
"log"
2022-01-15 18:20:31 +10:30
"os"
"github.com/mattn/go-isatty"
)
func main() {
log.SetFlags(log.Lshortfile)
2022-01-15 14:04:13 +10:30
port := flag.Int("port", 9000, "Port to run server/client on.")
addr := flag.String("a", "127.0.0.1", "address to connect to.")
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
flag.Parse()
if *isServer {
2022-01-15 14:04:13 +10:30
log.Printf("Server running on %d\n", *port)
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
} 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}
err := c.Connect()
if err != nil {
fmt.Print(err)
}
}
}