Skip to content

Commit

Permalink
Adds global history feature
Browse files Browse the repository at this point in the history
Updates readme

Updates release flow
  • Loading branch information
ksysoev committed Oct 14, 2023
1 parent a9ffba6 commit 15b0450
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 15 deletions.
30 changes: 21 additions & 9 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'


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"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 16 additions & 3 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"log"
"os"
"os/user"
"strings"

"github.com/eiannone/keyboard"
"github.com/ksysoev/wsget/pkg/formater"
Expand All @@ -13,6 +15,9 @@ import (
const (
LINE_UP = "\033[1A"
LINE_CLEAR = "\x1b[2K"

HISTORY_FILE = ".wsget_history"
HISTORY_LIMIT = 100
)

type CLI struct {
Expand All @@ -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,
}
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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

Expand Down
93 changes: 91 additions & 2 deletions pkg/cli/history.go
Original file line number Diff line number Diff line change
@@ -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)
}

Expand Down

0 comments on commit 15b0450

Please sign in to comment.