diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 5c3f803..cfd1e02 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -12,13 +12,25 @@ jobs: steps: - name: Checkout the repository uses: actions/checkout@v2 - - name: Generate build files - uses: thatisuday/go-cross-build@v1 + - name: Setup Go + uses: actions/setup-go@v3 with: - platforms: 'linux/amd64, darwin/amd64, windows/amd64' - package: '' - name: './cmd/wsget' - compress: 'true' - dest: 'dist' - - \ No newline at end of file + go-version: '1.21.x' + - name: Build Linux + run: GOOS=linux GOARCH=amd64 go build -buildmode=exe -o ./wsget ./cmd/wsget + - name: Compress Linux + run: tar -czvf ./wsget-$(git describe --tags --abbrev=0).linux-amd64.tar.gz ./wsget ./LICENSE ./README.md + - name: Build MacOS (Intel) + run: GOOS=darwin GOARCH=amd64 go build -buildmode=exe -o ./wsget ./cmd/wsget + - name: Compress MacOS (Intel) + run: tar -czvf ./wsget-$(git describe --tags --abbrev=0).darwin-amd64.tar.gz ./wsget ./LICENSE ./README.md + - name: Build MacOS (M) + run: GOOS=darwin GOARCH=arm64 go build -buildmode=exe -o ./wsget ./cmd/wsget + - name: Compress MacOS (M) + run: tar -czvf ./wsget-$(git describe --tags --abbrev=0).darwin-arm64.tar.gz ./wsget ./LICENSE ./README.md + - name: Copy build-artifacts + uses: skx/github-action-publish-binaries@master + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + args: "./*.tar.gz" diff --git a/README.md b/README.md index 7a324fe..5040a75 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ wsget is a command-line tool for interacting with a WebSocket server. It support To install wsget, you can use `go install`: ``` -go install github.com/ksysoev/wsget/cmd/wsget +go install github.com/ksysoev/wsget/cmd/wsget@latest ``` ## Usage diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index 379987e..c331090 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -4,6 +4,8 @@ import ( "fmt" "log" "os" + "os/user" + "strings" "github.com/eiannone/keyboard" "github.com/ksysoev/wsget/pkg/formater" @@ -13,6 +15,9 @@ import ( const ( LINE_UP = "\033[1A" LINE_CLEAR = "\x1b[2K" + + HISTORY_FILE = ".wsget_history" + HISTORY_LIMIT = 100 ) type CLI struct { @@ -22,9 +27,15 @@ type CLI struct { } func NewCLI(wsConn *ws.WSConnection) *CLI { + currentUser, err := user.Current() + if err != nil { + log.Fatal(err) + } + homeDir := currentUser.HomeDir + return &CLI{ formater: formater.NewFormatter(), - history: NewHistory(), + history: NewHistory(homeDir+"/"+HISTORY_FILE, HISTORY_LIMIT), wsConn: wsConn, } } @@ -34,6 +45,7 @@ func (c *CLI) Run(outputFile *os.File) error { return err } defer keyboard.Close() + defer c.history.SaveToFile() keysEvents, err := keyboard.GetKeys(10) if err != nil { @@ -108,8 +120,9 @@ func (c *CLI) requestMode(keyStream <-chan keyboard.KeyEvent) (string, error) { if buffer == "" { return buffer, fmt.Errorf("cannot send empty request") } - c.history.AddRequest(buffer) - return buffer, nil + requet := strings.TrimSpace(buffer) + c.history.AddRequest(requet) + return requet, nil case keyboard.KeyEsc: return "", nil diff --git a/pkg/cli/history.go b/pkg/cli/history.go index d2e9121..314205e 100644 --- a/pkg/cli/history.go +++ b/pkg/cli/history.go @@ -1,16 +1,105 @@ package cli +import ( + "bufio" + "log" + "os" + "strings" +) + type History struct { + file string + limit uint requests []string } -func NewHistory() *History { - return &History{ +func NewHistory(file string, limit uint) *History { + h := &History{ + file: file, + limit: limit, requests: make([]string, 0), } + + h.loadFromFile() + + return h +} + +func (h *History) loadFromFile() error { + fileHandler, err := os.OpenFile(h.file, os.O_RDONLY|os.O_CREATE, 0644) + if err != nil { + log.Println("Error opening history file:", err) + return err + } + + defer fileHandler.Close() + + reader := bufio.NewReader(fileHandler) + + for { + line, err := reader.ReadString('\n') + if err != nil { + break + } + + line = strings.TrimSpace(line) + + if line == "" { + continue + } + + line = strings.ReplaceAll(line, "\\n", "\n") + + h.requests = append(h.requests, line) + } + + return nil +} + +func (h *History) SaveToFile() error { + fileHandler, err := os.OpenFile(h.file, os.O_WRONLY|os.O_CREATE, 0644) + if err != nil { + log.Println("Error opening history file:", err) + return err + } + + defer fileHandler.Close() + + writer := bufio.NewWriter(fileHandler) + + var pos int + if uint(len(h.requests)) < h.limit { + pos = 0 + } else { + pos = len(h.requests) - int(h.limit) + } + + for _, request := range h.requests[pos:] { + request = strings.TrimSpace(request) + if request == "" { + continue + } + request = strings.ReplaceAll(request, "\n", "\\n") + _, err := writer.WriteString(request + "\n") + if err != nil { + return err + } + } + + return writer.Flush() } func (h *History) AddRequest(request string) { + if request == "" { + return + } + + if len(h.requests) > 0 { + if h.requests[len(h.requests)-1] == request { + return + } + } + h.requests = append(h.requests, request) }