1
0
mirror of https://github.com/schollz/croc synced 2024-07-05 09:08:53 +00:00

use stderr for output

This commit is contained in:
Zack Scholl 2018-09-21 22:25:01 -07:00
parent d555a81d30
commit ebac8beaaa
3 changed files with 39 additions and 0 deletions

37
main.go
View File

@ -4,8 +4,10 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"time"
humanize "github.com/dustin/go-humanize"
"github.com/schollz/croc/src/croc"
"github.com/schollz/utils"
"github.com/urfave/cli"
@ -112,6 +114,30 @@ func send(c *cli.Context) error {
// generate code phrase
codePhrase = utils.GetRandomName()
}
// print the text
finfo, err := os.Stat(fname)
if err != nil {
return err
}
_, filename := filepath.Split(fname)
fileOrFolder := "file"
fsize := finfo.Size()
if finfo.IsDir() {
fileOrFolder = "folder"
fsize, err = dirSize(fname)
if err != nil {
return err
}
}
fmt.Fprintf(os.Stderr,
"Sending %s %s name '%s'\nCode is: %s\nOn the other computer, please run:\n\ncroc %s",
humanize.Bytes(uint64(fsize)),
fileOrFolder,
filename,
codePhrase,
codePhrase,
)
return cr.Send(fname, codePhrase)
}
@ -130,3 +156,14 @@ func relay(c *cli.Context) error {
cr.CurveType = c.String("curve")
return cr.Relay()
}
func dirSize(path string) (int64, error) {
var size int64
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
if !info.IsDir() {
size += info.Size()
}
return err
})
return size, err
}

View File

@ -101,6 +101,7 @@ func receive(c *websocket.Conn, codephrase string) (err error) {
int(fstats.Size),
progressbar.OptionSetRenderBlankState(true),
progressbar.OptionSetBytes(int(fstats.Size)),
progressbar.OptionSetWriter(os.Stderr),
)
c.WriteMessage(websocket.BinaryMessage, []byte("ready"))
for {

View File

@ -136,6 +136,7 @@ func send(c *websocket.Conn, fname string, codephrase string) (err error) {
int(fstats.Size),
progressbar.OptionSetRenderBlankState(true),
progressbar.OptionSetBytes(int(fstats.Size)),
progressbar.OptionSetWriter(os.Stderr),
)
for {
bytesread, err := f.Read(buffer)