Skip to content

Commit

Permalink
feat: add concurrency for list (#77)
Browse files Browse the repository at this point in the history
* feat: add concurrency for list

* feat: use go 1.16.9

* fix: comment out deletion of history file

* fix: dont crash when file deletion fail
  • Loading branch information
elhmn authored Nov 3, 2021
1 parent d87dddc commit 068ea34
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
uses:
actions/setup-go@v2
with:
go-version: 1.16
go-version: 1.16.9
id: go

- name: Checkout
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
uses:
actions/setup-go@v2
with:
go-version: 1.16
go-version: 1.16.9
id: go

- name: Checkout
Expand All @@ -35,7 +35,7 @@ jobs:
uses:
actions/setup-go@v2
with:
go-version: 1.16
go-version: 1.16.9
id: go

- name: Checkout
Expand All @@ -57,7 +57,7 @@ jobs:
name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: 1.16.9
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
uses:
actions/setup-go@v2
with:
go-version: 1.16
go-version: 1.16.9
id: go

- name: Checkout
Expand Down
6 changes: 4 additions & 2 deletions cmd/add_history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd_test

import (
"bytes"
"fmt"
"testing"

"github.com/elhmn/ckp/cmd"
Expand Down Expand Up @@ -74,10 +75,11 @@ func TestAddHistoryCommand(t *testing.T) {

//Delete history tmp files
if err := files.DeleteFileFromHomeDirectory(history.BashHistoryFile); err != nil {
t.Error(err)
fmt.Printf("Failed to remove file: %s\n", err)
}

if err := files.DeleteFileFromHomeDirectory(history.ZshHistoryFile); err != nil {
t.Error(err)
fmt.Printf("Failed to remove file: %s\n", err)
}

//Restore history path
Expand Down
69 changes: 43 additions & 26 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cmd

import (
"fmt"
"strings"
"sync"
"time"

"github.com/elhmn/ckp/internal/config"
Expand Down Expand Up @@ -113,41 +115,56 @@ func getField(field, value string) string {
return ""
}

func listScripts(scripts []store.Script, isCode, isSolution, shouldListAll bool, limit int) string {
func sprintScript(wg *sync.WaitGroup, output []string, index int, s store.Script, isCode, isSolution bool) {
defer wg.Done()

list := ""
//if the script is a solution
if s.Solution.Content != "" {
if isCode {
return
}
list += getField("ID", s.ID)
list += getField("CreationTime", s.CreationTime.Format(time.RFC1123))
list += getField("UpdateTime", s.UpdateTime.Format(time.RFC1123))
list += " Type: Solution\n"
list += getField(" Comment", s.Comment)
list += getField(" Solution", s.Solution.Content)
} else {
if isSolution {
return
}
list += getField("ID", s.ID)
list += getField("CreationTime", s.CreationTime.Format(time.RFC1123))
list += getField("UpdateTime", s.UpdateTime.Format(time.RFC1123))
list += " Type: Code\n"
list += getField(" Alias", s.Code.Alias)
list += getField(" Comment", s.Comment)
list += getField(" Code", s.Code.Content)
}
list += "\n"

output[index] = list
}

func listScripts(scripts []store.Script, isCode, isSolution, shouldListAll bool, limit int) string {
size := len(scripts)
wg := sync.WaitGroup{}

//if --all was specified set the limit to the size of the list of scripts
if shouldListAll {
limit = size
}

output := make([]string, limit)

//Buffer channel
for i := 0; i < limit && i < size; i++ {
wg.Add(1)
s := scripts[i]
//if the script is a solution
if s.Solution.Content != "" {
if isCode {
continue
}
list += getField("ID", s.ID)
list += getField("CreationTime", s.CreationTime.Format(time.RFC1123))
list += getField("UpdateTime", s.UpdateTime.Format(time.RFC1123))
list += " Type: Solution\n"
list += getField(" Comment", s.Comment)
list += getField(" Solution", s.Solution.Content)
} else {
if isSolution {
continue
}
list += getField("ID", s.ID)
list += getField("CreationTime", s.CreationTime.Format(time.RFC1123))
list += getField("UpdateTime", s.UpdateTime.Format(time.RFC1123))
list += " Type: Code\n"
list += getField(" Alias", s.Code.Alias)
list += getField(" Comment", s.Comment)
list += getField(" Code", s.Code.Content)
}
list += "\n"
go sprintScript(&wg, output, i, s, isCode, isSolution)
}
return list
wg.Wait()

return strings.Join(output, "")
}
29 changes: 29 additions & 0 deletions cmd/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package cmd_test

import (
"bytes"
"fmt"
"testing"

"github.com/elhmn/ckp/cmd"
"github.com/elhmn/ckp/internal/config"
)

func TestListCommand(t *testing.T) {
Expand Down Expand Up @@ -86,3 +88,30 @@ func TestListCommand(t *testing.T) {
}
})
}

func BenchmarkListFromHistory(b *testing.B) {
conf := config.NewDefaultConfig(config.Options{Version: "0.0.0+dev"})
writer := &bytes.Buffer{}
conf.OutWriter = writer
conf.CKPDir = ".ckp_test"

if err := setupFolder(conf); err != nil {
fmt.Printf("Error: failed with %s", err)
}

command := cmd.NewListCommand(conf)
//Set writer
command.SetOutput(conf.OutWriter)

//Set args
command.SetArgs([]string{"--all", "--from-history"})

err := command.Execute()
if err != nil {
fmt.Printf("Error: failed with %s", err)
}

if err := deleteFolder(conf); err != nil {
fmt.Printf("Error: failed with %s", err)
}
}

0 comments on commit 068ea34

Please sign in to comment.