From 9f54f2d08fd08e5e580252a0bf2f97014dea3ee9 Mon Sep 17 00:00:00 2001 From: "Khang. Nguyen Khac Nguyen" Date: Wed, 21 Oct 2020 15:16:42 +0700 Subject: [PATCH 1/3] chore(comm): cleanup comm Write func + better handling error. + use Send instead of Write since we do not need number of bytes sent. --- src/comm/comm.go | 22 +++++++++++----------- src/message/message.go | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/comm/comm.go b/src/comm/comm.go index 7afa12d..d1b77db 100644 --- a/src/comm/comm.go +++ b/src/comm/comm.go @@ -77,23 +77,23 @@ func (c *Comm) Close() { } } -func (c *Comm) Write(b []byte) (int, error) { +func (c *Comm) Write(b []byte) (n int, err error) { header := new(bytes.Buffer) - err := binary.Write(header, binary.LittleEndian, uint32(len(b))) + err = binary.Write(header, binary.LittleEndian, uint32(len(b))) if err != nil { fmt.Println("binary.Write failed:", err) } tmpCopy := append(header.Bytes(), b...) - n, err := c.connection.Write(tmpCopy) - if n != len(tmpCopy) { - if err != nil { - err = fmt.Errorf("wanted to write %d but wrote %d: %w", len(b), n, err) - } else { - err = fmt.Errorf("wanted to write %d but wrote %d", len(b), n) - } + n, err = c.connection.Write(tmpCopy) + if err != nil { + err = fmt.Errorf("connection.Write failed: %w", err) + return } - // log.Printf("wanted to write %d but wrote %d", n, len(b)) - return n, err + if n != len(tmpCopy) { + err = fmt.Errorf("wanted to write %d but wrote %d", len(b), n) + return + } + return } func (c *Comm) Read() (buf []byte, numBytes int, bs []byte, err error) { diff --git a/src/message/message.go b/src/message/message.go index ee3503b..efa3dd1 100644 --- a/src/message/message.go +++ b/src/message/message.go @@ -28,7 +28,7 @@ func Send(c *comm.Comm, key []byte, m Message) (err error) { if err != nil { return } - _, err = c.Write(mSend) + err = c.Send(mSend) return } From e85ac9f8bf06b1525f731bd3ca319c4ee22e1da3 Mon Sep 17 00:00:00 2001 From: "Khang. Nguyen Khac Nguyen" Date: Wed, 21 Oct 2020 15:52:04 +0700 Subject: [PATCH 2/3] chore(croc): remove redundant code + remove redundant return of func that does not return + remove func that is not used --- src/croc/croc.go | 7 ------- src/croc/croc_test.go | 6 ------ 2 files changed, 13 deletions(-) diff --git a/src/croc/croc.go b/src/croc/croc.go index bffa726..1a7c569 100644 --- a/src/croc/croc.go +++ b/src/croc/croc.go @@ -23,7 +23,6 @@ import ( "github.com/schollz/pake/v2" "github.com/schollz/peerdiscovery" "github.com/schollz/progressbar/v3" - "github.com/schollz/spinner" "github.com/tscholl2/siec" "github.com/schollz/croc/v8/src/comm" @@ -99,7 +98,6 @@ type Client struct { conn []*comm.Comm bar *progressbar.ProgressBar - spinner *spinner.Spinner longestFilename int firstSend bool @@ -1366,8 +1364,6 @@ func (c *Client) receiveData(i int) { } } } - - return } func (c *Client) sendData(i int) { @@ -1436,8 +1432,6 @@ func (c *Client) sendData(i int) { c.bar.Add(n) c.TotalSent += int64(n) // time.Sleep(100 * time.Millisecond) - } else { - // log.Debugf("skipping chunk %d", pos) } } @@ -1451,5 +1445,4 @@ func (c *Client) sendData(i int) { panic(errRead) } } - return } diff --git a/src/croc/croc_test.go b/src/croc/croc_test.go index f6fa58d..12cac9c 100644 --- a/src/croc/croc_test.go +++ b/src/croc/croc_test.go @@ -12,12 +12,6 @@ import ( "github.com/stretchr/testify/assert" ) -func must(err error) { - if err != nil { - panic(err) - } -} - func init() { log.SetLevel("trace") From 12e09020d1cc7c08a9cf906802c0ad8c25546e09 Mon Sep 17 00:00:00 2001 From: "Khang. Nguyen Khac Nguyen" Date: Wed, 21 Oct 2020 15:56:28 +0700 Subject: [PATCH 3/3] chore(mod): cleanup dependencies + cleanup dependencies since we do not use spinner anymore. --- go.mod | 3 --- go.sum | 13 ------------- 2 files changed, 16 deletions(-) diff --git a/go.mod b/go.mod index 9b3500a..03320a9 100644 --- a/go.mod +++ b/go.mod @@ -7,17 +7,14 @@ require ( github.com/cespare/xxhash v1.1.0 github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect github.com/denisbrodbeck/machineid v1.0.1 - github.com/fatih/color v1.9.0 // indirect github.com/kalafut/imohash v1.0.0 github.com/kr/pretty v0.1.0 // indirect - github.com/mattn/go-colorable v0.1.8 // indirect github.com/schollz/cli/v2 v2.2.1 github.com/schollz/logger v1.2.0 github.com/schollz/mnemonicode v1.0.1 github.com/schollz/pake/v2 v2.0.4 github.com/schollz/peerdiscovery v1.6.0 github.com/schollz/progressbar/v3 v3.6.1 - github.com/schollz/spinner v0.0.0-20180925172146-6bbc5f7804f9 github.com/spaolacci/murmur3 v1.1.0 // indirect github.com/stretchr/testify v1.4.0 github.com/tscholl2/siec v0.0.0-20191122224205-8da93652b094 diff --git a/go.sum b/go.sum index 6a5d72d..0ac3fcd 100644 --- a/go.sum +++ b/go.sum @@ -12,8 +12,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/denisbrodbeck/machineid v1.0.1 h1:geKr9qtkB876mXguW2X6TU4ZynleN6ezuMSRhl4D7AQ= github.com/denisbrodbeck/machineid v1.0.1/go.mod h1:dJUwb7PTidGDeYyUBmXZ2GphQBbjJCrnectwCyxcUSI= -github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s= -github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw= github.com/kalafut/imohash v1.0.0 h1:LgCJ+p/BwM2HKpOxFopkeddpzVCfm15EtXMroXD1SYE= github.com/kalafut/imohash v1.0.0/go.mod h1:c3RHT80ZAp5C/aYgQI92ZlrOymqkZnRDprU87kg75HI= @@ -22,11 +20,6 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= -github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8= -github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= -github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= -github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= @@ -51,8 +44,6 @@ github.com/schollz/progressbar/v2 v2.15.0 h1:dVzHQ8fHRmtPjD3K10jT3Qgn/+H+92jhPrh github.com/schollz/progressbar/v2 v2.15.0/go.mod h1:UdPq3prGkfQ7MOzZKlDRpYKcFqEMczbD7YmbPgpzKMI= github.com/schollz/progressbar/v3 v3.6.1 h1:3vtTlSk1IntZNdqt8JoGDYZ+vmbJ2fvm2+jLeB7QHbQ= github.com/schollz/progressbar/v3 v3.6.1/go.mod h1:3B25e7a0JCjz1joGNAk7E2TnSr0x+aYQ0sZPs8fPwC0= -github.com/schollz/spinner v0.0.0-20180925172146-6bbc5f7804f9 h1:y08o5oQ/slxXE/F0uh5dd8mdVvb+w4NLcNSDSq4c2F0= -github.com/schollz/spinner v0.0.0-20180925172146-6bbc5f7804f9/go.mod h1:kCMoQsqzx4MzGJWaALr6tKyCnlrY0kILGLkA1FOiLF4= github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= @@ -78,12 +69,8 @@ golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81R golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0 h1:5kGOVHlq0euqwzgTC9Vu15p6fV1Wi0ArVi8da2urnVg= golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8= -golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae h1:Ih9Yo4hSPImZOpfGuA4bR/ORKTAbhZo2AbWNRCnevdo=