1
1
Fork 0
mirror of https://github.com/schollz/croc.git synced 2025-10-10 21:01:02 +02: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
}