Skip to content

Commit

Permalink
implement some trzsz functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnywong committed May 15, 2022
1 parent ef6d4ea commit f4b0c76
Show file tree
Hide file tree
Showing 5 changed files with 311 additions and 30 deletions.
43 changes: 38 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,58 @@
# trzsz-go
trzsz is a simple file transfer tools, similar to lrzsz ( rz / sz ), and compatible with tmux.

*The `Go` version is under development. Please use the `Python` version instead. GitHub: https://github.com/trzsz/trzsz*


## Installation

### Ubuntu
### on Ubuntu
*Not released yet, please download the latest [release](https://github.com/trzsz/trzsz-go/releases) from GitHub.*

```
```sh
sudo add-apt-repository ppa:trzsz/ppa
sudo apt update
sudo apt install trzsz
```

### Windows / macOS / Other

### on Windows / macOS / Other

Please download the latest [release](https://github.com/trzsz/trzsz-go/releases) from GitHub.


### install from Source Code

```sh
git clone https://github.com/trzsz/trzsz-go.git
cd trzsz-go
make
sudo make install
```


## Usage

### on Local

Add `trzsz` before the shell, e.g.:

```sh
trzsz tmux
trzsz /bin/bash
trzsz ssh x.x.x.x
trzsz.exe cmd
trzsz.exe ssh x.x.x.x
```


### on Server

*The `Go` version is under development. Please use the `Python` version instead. GitHub: https://github.com/trzsz/trzsz*

Similar to lrzsz ( rz / sz ), command `trz` to upload files, command `tsz /path/to/file` to download files.

For more information, see the website of trzsz: [https://trzsz.github.io](https://trzsz.github.io/).


## Contact

Feel free to email me <[email protected]>.
46 changes: 46 additions & 0 deletions trzsz/comm.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,54 @@ SOFTWARE.

package trzsz

import (
"bytes"
"compress/zlib"
"encoding/base64"
"io/ioutil"
)

type PtyIO interface {
Read(b []byte) (n int, err error)
Write(p []byte) (n int, err error)
Close() error
}

type ProgressCallback interface {
// TODO
}

func encodeBytes(buf []byte) string {
var b bytes.Buffer
z := zlib.NewWriter(&b)
z.Write([]byte(buf))
z.Close()
return base64.StdEncoding.EncodeToString(b.Bytes())
}

func encodeString(str string) string {
return encodeBytes([]byte(str))
}

func decodeString(str string) ([]byte, error) {
b, err := base64.StdEncoding.DecodeString(str)
if err != nil {
return nil, err
}
z, err := zlib.NewReader(bytes.NewReader(b))
if err != nil {
return nil, err
}
defer z.Close()
return ioutil.ReadAll(z)
}

func checkPathWritable(path string) error {
// TODO
return nil
}

func checkFilesReadable(files []string) error {
// TODO
return nil
}
42 changes: 42 additions & 0 deletions trzsz/progress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
MIT License
Copyright (c) 2022 Lonny Wong
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package trzsz

import (
"os"
)

type TextProgressBar struct {
// TODO
}

func NewTextProgressBar(stdout *os.File, columns int, tmuxPaneColumns int) *TextProgressBar {
// TODO
return nil
}

func (p *TextProgressBar) setTerminalColumns(columns int) {
// TODO
}
80 changes: 77 additions & 3 deletions trzsz/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,95 @@ SOFTWARE.

package trzsz

import (
"encoding/json"
"fmt"
)

type TrzszTransfer struct {
ptyIn PtyIO
ptyOut PtyIO
stopped bool
}

func NewTransfer(ptyIn PtyIO, ptyOut PtyIO) *TrzszTransfer {
return nil
return &TrzszTransfer{ptyIn, ptyOut, false}
}

func (t *TrzszTransfer) addReceivedData(buf []byte) {
// TODO
}

func (t *TrzszTransfer) stopTransferringFiles() {
// TODO
t.stopped = true
}

func (t *TrzszTransfer) cleanInput(timeoutInMilliseconds int64) {
}

func (t *TrzszTransfer) sendLine(typ string, buf string) error {
_, err := t.ptyOut.Write([]byte(fmt.Sprintf("#%s:%s\n", typ, buf)))
return err
}

func (t *TrzszTransfer) recvLine(expectType string, mayHasJunk bool) (string, error) {
if t.stopped {
return "", fmt.Errorf("Stopped")
}
// TODO
return "", nil
}

func (t *TrzszTransfer) sendString(typ string, str string) error {
return t.sendLine(typ, encodeString(str))
}

func (t *TrzszTransfer) sendAction(confirm bool) error {
actMap := map[string]interface{}{
"lang": "go",
"confirm": confirm,
"version": TrzszVersion,
}
actStr, err := json.Marshal(actMap)
if err != nil {
return err
}
return t.sendString("ACT", string(actStr))
}

func (t *TrzszTransfer) recvAction() error {
// TODO
return nil
}

func (t *TrzszTransfer) sendConfig() error {
// TODO
return nil
}

func (t *TrzszTransfer) recvConfig() (map[string]interface{}, error) {
// TODO
return nil, nil
}

func (t *TrzszTransfer) handleClientError(err error) {
// TODO
}

func (t *TrzszTransfer) sendExit(msg string) error {
t.cleanInput(200)
return t.sendString("EXIT", msg)
}

func (t *TrzszTransfer) sendFiles() {
func (t *TrzszTransfer) sendFiles(files []string, progress ProgressCallback) ([]string, error) {
// TODO
t.sendExit("Under development")
return nil, nil
}

func (t *TrzszTransfer) recvFiles() {
func (t *TrzszTransfer) recvFiles(path string, progress ProgressCallback) ([]string, error) {
// TODO
t.sendExit("Under development")
return nil, nil
}
Loading

0 comments on commit f4b0c76

Please sign in to comment.