Skip to content

Commit

Permalink
added tidy command
Browse files Browse the repository at this point in the history
  • Loading branch information
herzrasen committed Jan 4, 2023
1 parent 0f8d765 commit b57e551
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
4 changes: 4 additions & 0 deletions args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ type ImportCmd struct {
Path string `arg:"positional"`
}

type TidyCmd struct {
}

type Args struct {
Record *RecordCmd `arg:"subcommand:record"`
Search *SearchCmd `arg:"subcommand:search"`
Get *GetCmd `arg:"subcommand:get"`
List *ListCmd `arg:"subcommand:list"`
Delete *DeleteCmd `arg:"subcommand:delete"`
Import *ImportCmd `arg:"subcommand:import"`
Tidy *TidyCmd `arg:"subcommand:tidy"`
Config string `arg:"--config" default:"~/.config/hist/config.yml"`
}
18 changes: 18 additions & 0 deletions client/tidy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package client

import "fmt"

func (c *Client) Tidy() error {
records, err := c.List(ListOptions{})
if err != nil {
return fmt.Errorf("client:Tidy: list: %w", err)
}
var idsToDelete []int64
for _, r := range records {
if c.Config.IsExcluded(r.Command) {
idsToDelete = append(idsToDelete, r.Id)
fmt.Printf("Deleting: %s\n", r.Command)
}
}
return c.Delete(DeleteOptions{Ids: idsToDelete})
}
56 changes: 56 additions & 0 deletions client/tidy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package client

import (
"errors"
"github.com/DATA-DOG/go-sqlmock"
"github.com/herzrasen/hist/config"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/require"
"testing"
"time"
)

func TestClient_Tidy(t *testing.T) {
t.Run("succeed", func(t *testing.T) {
db, mock, _ := sqlmock.New()
mock.ExpectQuery(`SELECT id, command, last_update, count
FROM hist
ORDER BY last_update, count DESC`).
WillReturnRows(sqlmock.NewRows([]string{"id", "command", "last_update", "count"}).
AddRow(1, "ls -alF", time.Now().Add(-5*time.Second), 100).
AddRow(2, "git push", time.Now(), 10))
mock.ExpectExec("DELETE FROM hist WHERE id IN (?)").
WithArgs(1).
WillReturnResult(sqlmock.NewResult(0, 1))
c := Client{
Db: sqlx.NewDb(db, "sqlite3"),
Config: &config.Config{
Patterns: config.Patterns{
Excludes: []string{
"^ls .*",
}},
},
}
err := c.Tidy()
require.NoError(t, err)
})

t.Run("list returns error", func(t *testing.T) {
db, mock, _ := sqlmock.New()
mock.ExpectQuery(`SELECT id, command, last_update, count
FROM hist
ORDER BY last_update, count DESC`).
WillReturnError(errors.New("some error"))
c := Client{
Db: sqlx.NewDb(db, "sqlite3"),
Config: &config.Config{
Patterns: config.Patterns{
Excludes: []string{
"^ls .*",
}},
},
}
err := c.Tidy()
require.Error(t, err)
})
}
6 changes: 6 additions & 0 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type HistClient interface {
Record(command string) error
Delete(options client.DeleteOptions) error
Import(file *os.File) error
Tidy() error
}

type Handler struct {
Expand Down Expand Up @@ -75,6 +76,11 @@ func (h *Handler) Handle(a args.Args) error {
if err != nil {
return fmt.Errorf("unable to import history: %w", err)
}
case a.Tidy != nil:
err := h.Client.Tidy()
if err != nil {
return fmt.Errorf("unable to tidy hist: %w", err)
}
}
return nil
}
14 changes: 14 additions & 0 deletions handler/mocks/HistClient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b57e551

Please sign in to comment.