diff --git a/client.go b/client.go index 7947e38..3547374 100644 --- a/client.go +++ b/client.go @@ -18,12 +18,12 @@ import ( ) type Client struct { - address string - port int - list bool - send bool - receive bool - authToken string + address string + port int + list bool + send bool + receiveNum int + authToken string } func (c *Client) Connect() error { @@ -73,8 +73,8 @@ func (c *Client) Connect() error { conn.Close() log.Debugf("done listing") - } else if c.receive { - log.Debugf("receiving a file") + } else if c.receiveNum >= 0 { + log.Debugf("receiving file %d", c.receiveNum) err := c.connectToServer(secure.OperationTypeReceive, enc, dec) if err != nil { @@ -82,7 +82,7 @@ func (c *Client) Connect() error { } req := secure.PacketReceiveDataStartRequest{ - Id: 0, // 0 means last? Change to do a fetch? + Id: uint32(c.receiveNum), } err = enc.Encode(req) if err != nil { diff --git a/main.go b/main.go index 855817f..ca48ab9 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os" + "strconv" log "github.com/sirupsen/logrus" @@ -11,15 +12,47 @@ import ( "github.com/spf13/viper" ) -var CurrentVersion = "v0.0.1" +var CurrentVersion = "v0.0.2" + +type PasteValue struct { + PasteRequired bool + PasteNumber uint +} + +func (v *PasteValue) String() string { + if v.PasteRequired { + return fmt.Sprintf("YES: %d", v.PasteNumber) + } + return "0" +} + +func (v *PasteValue) Set(s string) error { + v.PasteRequired = true + num, err := strconv.ParseUint(s, 10, 64) + if err != nil { + return err + } + + v.PasteNumber = uint(num) + return nil +} + +func (v *PasteValue) Type() string { + return "int" + +} func main() { isServer := flag.Bool("server", false, "Run netgiv in server mode") // client mode flags - isList := flag.BoolP("list", "l", false, "Set if requesting a list") + isList := flag.BoolP("list", "l", false, "Returns a list of current items on the server") isSend := flag.BoolP("copy", "c", false, "sending stdin to netgiv server (copy)") - isReceive := flag.BoolP("paste", "p", false, "receive file from netgiv server to stdout (paste)") + + pasteFlag := PasteValue{} + flag.VarP(&pasteFlag, "paste", "p", "receive from netgiv server to stdout (paste), with optional number (see --list)") + flag.Lookup("paste").NoOptDefVal = "0" + debug := flag.Bool("debug", false, "turn on debug logging") flag.String("address", "", "IP address/hostname of the netgiv server") @@ -31,6 +64,11 @@ func main() { flag.Parse() + receiveNum := int(pasteFlag.PasteNumber) + if !pasteFlag.PasteRequired { + receiveNum = -1 + } + viper.AddConfigPath("$HOME/.netgiv/") // call multiple times to add many search paths viper.SetConfigType("yaml") @@ -57,13 +95,6 @@ func main() { address := viper.GetString("address") - // flag.Usage = func() { - // fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0]) - // flag.PrintDefaults() - // fmt.Printf("\nIf stdin or stdout is a pipe, %s will automatically choose an appropriate\n", os.Args[0]) - // fmt.Printf("copy (-c) or paste (-p) mode\n") - // } - if *helpConfig { fmt.Print( `netgiv can be configured by command line parameters (see --help) but it will @@ -108,15 +139,14 @@ environment variable. This may be preferable in some environments. s := Server{port: port, authToken: authtoken} s.Run() } else { - - if !*isList && !*isSend && !*isReceive { + if !*isList && !*isSend && receiveNum == -1 { // 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 + receiveNum = 0 } else if !stdinTTY && stdoutTTY { *isSend = true } else if !stdinTTY && !stdoutTTY { @@ -128,7 +158,7 @@ environment variable. This may be preferable in some environments. } - c := Client{port: port, address: address, list: *isList, send: *isSend, receive: *isReceive, authToken: authtoken} + c := Client{port: port, address: address, list: *isList, send: *isSend, receiveNum: receiveNum, authToken: authtoken} err := c.Connect() if err != nil { fmt.Print(err) diff --git a/server.go b/server.go index f741756..2198ac1 100644 --- a/server.go +++ b/server.go @@ -201,7 +201,7 @@ func (s *Server) handleConnection(conn *net.TCPConn) { file.Close() ngfs = append(ngfs, ngf) - log.Printf("done receiving file") + log.Printf("done receiving file: %v", ngf) return } else if start.OperationType == secure.OperationTypeReceive { @@ -214,25 +214,27 @@ func (s *Server) handleConnection(conn *net.TCPConn) { return } - log.Printf("The asked for %v", req) + log.Debugf("The asked for %v", req) // do we have this ngf by id? - var requestedNGF *NGF + var requestedNGF NGF if len(ngfs) > 0 { if req.Id == 0 { // they want the most recent one - requestedNGF = &ngfs[len(ngfs)-1] + requestedNGF = ngfs[len(ngfs)-1] } else { for _, ngf := range ngfs { if ngf.Id == req.Id { - requestedNGF = &ngf + requestedNGF = ngf } } } } - if requestedNGF == nil { + log.Debugf("going to deliver %v", requestedNGF) + + if requestedNGF.Id == 0 { // not found log.Errorf("user requested %d, not found", req.Id) res := secure.PacketReceiveDataStartResponse{