diff --git a/src/cli/cli.go b/src/cli/cli.go index f378ec7c..e5989cc4 100644 --- a/src/cli/cli.go +++ b/src/cli/cli.go @@ -50,6 +50,7 @@ func Run() (err error) { ArgsUsage: "[filename]", Flags: []cli.Flag{ cli.StringFlag{Name: "code, c", Usage: "codephrase used to connect to relay"}, + cli.StringFlag{Name: "text, t", Usage: "send some text"}, cli.BoolFlag{Name: "no-local", Usage: "disable local relay when sending"}, cli.BoolFlag{Name: "no-multi", Usage: "disable multiplexing"}, cli.StringFlag{Name: "ports", Value: "9009,9010,9011,9012,9013", Usage: "ports of the local relay (optional)"}, @@ -151,6 +152,7 @@ func send(c *cli.Context) (err error) { Ask: c.GlobalBool("ask"), NoMultiplexing: c.Bool("no-multi"), RelayPassword: c.GlobalString("pass"), + SendingText: c.String("text") != "", } if crocOptions.RelayAddress != models.DEFAULT_RELAY { crocOptions.RelayAddress6 = "" @@ -196,6 +198,18 @@ func send(c *cli.Context) (err error) { log.Error(err) } }() + } else if c.String("text") != "" { + fnames, err = makeTempFileWithString(c.String("text")) + if err != nil { + return + } + defer func() { + err = os.Remove(fnames[0]) + if err != nil { + log.Error(err) + } + }() + } else { fnames = append([]string{c.Args().First()}, c.Args().Tail()...) } @@ -246,6 +260,26 @@ func getStdin() (fnames []string, err error) { return } +func makeTempFileWithString(s string) (fnames []string, err error) { + f, err := ioutil.TempFile(".", "croc-stdin-") + if err != nil { + return + } + + _, err = f.WriteString(s) + if err != nil { + return + } + + err = f.Close() + if err != nil { + return + } + fnames = []string{f.Name()} + return + +} + func getPaths(fnames []string) (paths []string, haveFolder bool, err error) { haveFolder = false paths = []string{} diff --git a/src/croc/croc.go b/src/croc/croc.go index 51979a0a..10e5f111 100644 --- a/src/croc/croc.go +++ b/src/croc/croc.go @@ -61,6 +61,7 @@ type Options struct { NoMultiplexing bool DisableLocal bool Ask bool + SendingText bool } // Client holds the state of the croc transfer @@ -137,6 +138,7 @@ type SenderInfo struct { FilesToTransfer []FileInfo MachineID string Ask bool + SendingText bool } // New establishes a new connection for transferring files between two instances. @@ -667,6 +669,9 @@ func (c *Client) transfer(options TransferOptions) (err error) { c.FilesToTransfer[c.FilesToTransferCurrentNum].FolderRemote, c.FilesToTransfer[c.FilesToTransferCurrentNum].Name, ) + log.Debugf("pathToFile: %s", pathToFile) + // close if not closed already + c.CurrentFile.Close() if err := os.Remove(pathToFile); err != nil { log.Warnf("error removing %s: %v", pathToFile, err) } @@ -681,6 +686,10 @@ func (c *Client) processMessageFileInfo(m message.Message) (done bool, err error log.Error(err) return } + c.Options.SendingText = senderInfo.SendingText + if c.Options.SendingText { + c.Options.Stdout = true + } c.FilesToTransfer = senderInfo.FilesToTransfer fname := fmt.Sprintf("%d files", len(c.FilesToTransfer)) if len(c.FilesToTransfer) == 1 { @@ -937,6 +946,7 @@ func (c *Client) updateIfSenderChannelSecured() (err error) { FilesToTransfer: c.FilesToTransfer, MachineID: machID, Ask: c.Options.Ask, + SendingText: c.Options.SendingText, }) if err != nil { log.Error(err)