Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: ♻️ refactor with afero #184

Merged
merged 7 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.21.1
go-version: 1.22.3

- name: Build linux 386
run: CGO_ENABLED=0 GOOS=linux GOARCH=386 go build -v ./...
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ If you find an issue to work on, you are welcome to open a PR with a fix.
```shell
git clone git@github.com:yourname/g.git
```
2. Make sure you have installed **go**, and go version >= 1.21.
2. Make sure you have installed **go**, and go version >= 1.22.0 .
```shell
go version
```
Expand Down
2 changes: 1 addition & 1 deletion docs/ReleaseWorkflow.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ software/toolchain:

| name | how to install | remark |
|---------------|------------------------------|-----------------------------------------------------------------------------------|
| go >1.21.0 | `brew install go` | or use [go.dev](https://go.dev/dl/) / [goup](https://github.com/owenthereal/goup) |
| go >1.22.0 | `brew install go` | or use [go.dev](https://go.dev/dl/) / [goup](https://github.com/owenthereal/goup) |
| git | `brew install git` | or use xcode version |
| upx | `brew install upx` | |
| dpkg-deb | `brew install dpkg` | |
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ require (
github.com/rivo/uniseg v0.4.7 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ github.com/skeema/knownhosts v1.2.2 h1:Iug2P4fLmDw9f41PB6thxUkNUkJzB5i+1/exaj40L
github.com/skeema/knownhosts v1.2.2/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
Expand Down
2 changes: 1 addition & 1 deletion internal/cached/cached.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cached

import (
constval "github.com/Equationzhao/g/internal/const"
constval "github.com/Equationzhao/g/internal/global"
"github.com/alphadose/haxmap"
)

Expand Down
24 changes: 10 additions & 14 deletions internal/cli/g.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (

"github.com/Equationzhao/g/internal/align"
"github.com/Equationzhao/g/internal/config"
"github.com/Equationzhao/g/internal/const"
contents "github.com/Equationzhao/g/internal/content"
"github.com/Equationzhao/g/internal/display"
"github.com/Equationzhao/g/internal/filter"
"github.com/Equationzhao/g/internal/global"
"github.com/Equationzhao/g/internal/index"
"github.com/Equationzhao/g/internal/item"
"github.com/Equationzhao/g/internal/render"
Expand All @@ -29,6 +29,7 @@ import (
"github.com/hako/durafmt"
"github.com/panjf2000/ants/v2"
"github.com/savioxavier/termlink"
"github.com/spf13/afero"
"github.com/urfave/cli/v2"
"github.com/xrash/smetrics"
"go.szostok.io/version/upgrade"
Expand Down Expand Up @@ -185,23 +186,18 @@ func init() {
// for generating file tree
func dive(
parent string, depth, limit int, infos *util.Slice[*item.FileInfo], errSlice *util.Slice[error],
wg *sync.WaitGroup, itemFilter *filter.ItemFilter,
wg *sync.WaitGroup, itemFilter *filter.ItemFilter, fs afero.Fs,
) {
defer wg.Done()
if limit > 0 && depth > limit {
return
}
dir, err := os.ReadDir(parent)
dir, err := afero.ReadDir(fs, parent)
if err != nil {
errSlice.AppendTo(err)
return
}
for _, entry := range dir {
f, err := entry.Info()
if err != nil {
errSlice.AppendTo(err)
continue
}
for _, f := range dir {
nowAbs := filepath.Join(parent, f.Name())
info, _ := item.NewFileInfoWithOption(item.WithAbsPath(nowAbs), item.WithFileInfo(f))
// check filter
Expand All @@ -212,11 +208,11 @@ func dive(
info.Cache["parent"] = []byte(parent)
info.Cache["level"] = []byte(strconv.Itoa(depth))
infos.AppendTo(info)
if entry.IsDir() {
if f.IsDir() {
wg.Add(1)
err := pool.Submit(
func() {
dive(info.FullPath, depth+1, limit, infos, errSlice, wg, itemFilter)
dive(info.FullPath, depth+1, limit, infos, errSlice, wg, itemFilter, fs)
},
)
if err != nil {
Expand Down Expand Up @@ -318,7 +314,7 @@ func initVersionHelpFlags() {
}

func MakeErrorStr(msg string) string {
return fmt.Sprintf("%s × %s %s", constval.Error, msg, constval.Reset)
return fmt.Sprintf("%s × %s %s", global.Error, msg, global.Reset)
}

func checkErr(err error, start string) {
Expand All @@ -342,7 +338,7 @@ func suggestFlag(flags []cli.Flag, provided string) string {
for _, flag := range flags {
flagNames := flag.Names()
for _, name := range flagNames {
newDistance := smetrics.JaroWinkler(name, provided, constval.BoostThreshold, constval.PrefixSize)
newDistance := smetrics.JaroWinkler(name, provided, global.BoostThreshold, global.PrefixSize)
if newDistance > distance {
distance = newDistance
suggestion = name
Expand Down Expand Up @@ -679,7 +675,7 @@ var logic = func(context *cli.Context) error {
pool.Tune(ants.DefaultAntsPoolSize)
dive(
path[i], 1, depth, infoSlice, errSlice, &wg,
itemFilter,
itemFilter, global.Fs,
)
wg.Wait()
infos = append(infos, *infoSlice.GetRaw()...)
Expand Down
47 changes: 47 additions & 0 deletions internal/cli/g_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cli

import (
"sync"
"testing"

"github.com/Equationzhao/g/internal/filter"
"github.com/Equationzhao/g/internal/item"
"github.com/Equationzhao/g/internal/util"
"github.com/panjf2000/ants/v2"
"github.com/spf13/afero"
"github.com/zeebo/assert"
)

func Test_dive(t *testing.T) {
afs := afero.NewMemMapFs()
// create files and dirs
_ = afs.MkdirAll("a/b/c", 0o755)
_ = afero.WriteFile(afs, "a/b/c/d", util.GenRandomData(10), 0o644)
_ = afero.WriteFile(afs, "a/b/e", util.GenRandomData(10), 0o644)
_ = afero.WriteFile(afs, "a/f", util.GenRandomData(10), 0o644)
_ = afero.WriteFile(afs, "a/g", util.GenRandomData(10), 0o644)
_ = afero.WriteFile(afs, "a/h", util.GenRandomData(10), 0o644)
_ = afero.WriteFile(afs, "i", util.GenRandomData(10), 0o644)

var err error
pool, err = ants.NewPool(ants.DefaultAntsPoolSize)
assert.NoError(t, err)

// test
uslice := util.NewSlice[*item.FileInfo](10)
errSlice := util.NewSlice[error](1)
wg := sync.WaitGroup{}
wg.Add(1)
filters := filter.NewItemFilter()
dive(".", 1, -1, uslice, errSlice, &wg, filters, afs)
wg.Wait()
if uslice.Len() != 9 {
t.Errorf("expect 7, got %d", uslice.Len())
for _, info := range *uslice.GetRaw() {
t.Logf("%s", info.FullPath)
}
}
if errSlice.Len() != 0 {
t.Errorf("expect 0, got %d", errSlice.Len())
}
}
2 changes: 1 addition & 1 deletion internal/content/charset.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"os"
"strings"

constval "github.com/Equationzhao/g/internal/const"
constval "github.com/Equationzhao/g/internal/global"
"github.com/Equationzhao/g/internal/item"
"github.com/Equationzhao/g/internal/render"
"github.com/Equationzhao/g/internal/util"
Expand Down
2 changes: 1 addition & 1 deletion internal/content/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strings"

"github.com/Equationzhao/g/internal/align"
constval "github.com/Equationzhao/g/internal/const"
"github.com/Equationzhao/g/internal/git"
constval "github.com/Equationzhao/g/internal/global"
"github.com/Equationzhao/g/internal/item"
"github.com/Equationzhao/g/internal/render"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/content/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/Equationzhao/g/internal/align"

constval "github.com/Equationzhao/g/internal/const"
constval "github.com/Equationzhao/g/internal/global"
"github.com/Equationzhao/g/internal/item"
"github.com/Equationzhao/g/internal/osbased"
"github.com/Equationzhao/g/internal/render"
Expand Down
2 changes: 1 addition & 1 deletion internal/content/index.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package content

import (
constval "github.com/Equationzhao/g/internal/const"
constval "github.com/Equationzhao/g/internal/global"
"github.com/Equationzhao/g/internal/item"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/content/inode.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package content

import (
"github.com/Equationzhao/g/internal/align"
constval "github.com/Equationzhao/g/internal/const"
constval "github.com/Equationzhao/g/internal/global"
"github.com/Equationzhao/g/internal/item"
"github.com/Equationzhao/g/internal/osbased"
"github.com/Equationzhao/g/internal/render"
Expand Down
2 changes: 1 addition & 1 deletion internal/content/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/Equationzhao/g/internal/align"

constval "github.com/Equationzhao/g/internal/const"
constval "github.com/Equationzhao/g/internal/global"
"github.com/Equationzhao/g/internal/item"
"github.com/Equationzhao/g/internal/osbased"
"github.com/Equationzhao/g/internal/render"
Expand Down
2 changes: 1 addition & 1 deletion internal/content/mimetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/Equationzhao/g/internal/align"

constval "github.com/Equationzhao/g/internal/const"
constval "github.com/Equationzhao/g/internal/global"
"github.com/Equationzhao/g/internal/item"
"github.com/Equationzhao/g/internal/render"
"github.com/Equationzhao/g/internal/util"
Expand Down
14 changes: 7 additions & 7 deletions internal/content/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

"github.com/Equationzhao/g/internal/display"

"github.com/Equationzhao/g/internal/const"
"github.com/Equationzhao/g/internal/global"
"github.com/shirou/gopsutil/v3/disk"

"github.com/Equationzhao/g/internal/item"
Expand Down Expand Up @@ -188,7 +188,7 @@ func NewNameEnable() *Name {
return &Name{}
}

const NameName = constval.NameOfName
const NameName = global.NameOfName

func makeLink(abs string, name string) string {
return util.MakeLink(abs, name)
Expand Down Expand Up @@ -459,19 +459,19 @@ func (n *Name) Enable(renderer *render.Renderer) ContentOption {

func checkNameDisplayEffect(style theme.Style, buffer *bytebufferpool.ByteBuffer) {
if style.Underline {
_, _ = buffer.WriteString(constval.Underline)
_, _ = buffer.WriteString(global.Underline)
}
if style.Bold {
_, _ = buffer.WriteString(constval.Bold)
_, _ = buffer.WriteString(global.Bold)
}
if style.Italics {
_, _ = buffer.WriteString(constval.Italics)
_, _ = buffer.WriteString(global.Italics)
}
if style.Faint {
_, _ = buffer.WriteString(constval.Faint)
_, _ = buffer.WriteString(global.Faint)
}
if style.Blink {
_, _ = buffer.WriteString(constval.Blink)
_, _ = buffer.WriteString(global.Blink)
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/content/owner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/Equationzhao/g/internal/align"

constval "github.com/Equationzhao/g/internal/const"
constval "github.com/Equationzhao/g/internal/global"
"github.com/Equationzhao/g/internal/item"
"github.com/Equationzhao/g/internal/osbased"
"github.com/Equationzhao/g/internal/render"
Expand Down
2 changes: 1 addition & 1 deletion internal/content/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"strconv"

"github.com/Equationzhao/g/internal/align"
constval "github.com/Equationzhao/g/internal/const"
constval "github.com/Equationzhao/g/internal/global"
"github.com/Equationzhao/g/internal/item"
"github.com/Equationzhao/g/internal/render"
"github.com/pkg/xattr"
Expand Down
2 changes: 1 addition & 1 deletion internal/content/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/Equationzhao/g/internal/align"

constval "github.com/Equationzhao/g/internal/const"
constval "github.com/Equationzhao/g/internal/global"
"github.com/Equationzhao/g/internal/item"
"github.com/Equationzhao/g/internal/osbased"
"github.com/Equationzhao/g/internal/render"
Expand Down
2 changes: 1 addition & 1 deletion internal/content/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"runtime"
"time"

constval "github.com/Equationzhao/g/internal/const"
constval "github.com/Equationzhao/g/internal/global"
"github.com/Equationzhao/g/internal/item"
"github.com/Equationzhao/g/internal/osbased"
"github.com/Equationzhao/g/internal/render"
Expand Down
2 changes: 1 addition & 1 deletion internal/display/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"

"github.com/Equationzhao/g/internal/align"
constval "github.com/Equationzhao/g/internal/const"
constval "github.com/Equationzhao/g/internal/global"
"github.com/Equationzhao/g/internal/item"
"github.com/valyala/bytebufferpool"
)
Expand Down
10 changes: 5 additions & 5 deletions internal/display/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"strings"

"github.com/Equationzhao/g/internal/config"
"github.com/Equationzhao/g/internal/const"
"github.com/Equationzhao/g/internal/display/tree"
"github.com/Equationzhao/g/internal/global"
"github.com/Equationzhao/g/internal/item"
"github.com/Equationzhao/g/internal/util"
"github.com/acarl005/stripansi"
Expand Down Expand Up @@ -58,8 +58,8 @@ func fire(h []func(Printer, ...*item.FileInfo), p Printer, i ...*item.FileInfo)

func newHook() *hook {
return &hook{
BeforePrint: make([]func(Printer, ...*item.FileInfo), 0, constval.DefaultHookLen),
AfterPrint: make([]func(Printer, ...*item.FileInfo), 0, constval.DefaultHookLen),
BeforePrint: make([]func(Printer, ...*item.FileInfo), 0, global.DefaultHookLen),
AfterPrint: make([]func(Printer, ...*item.FileInfo), 0, global.DefaultHookLen),
}
}

Expand Down Expand Up @@ -768,15 +768,15 @@ func (t *TreePrinter) Print(s ...*item.FileInfo) {
}
prefix, name := prefixAndName(node.Meta)
_, _ = t.WriteString(prefix)
_, _ = t.WriteString(constval.Faint)
_, _ = t.WriteString(global.Faint)
for _, c := range node.Connectors {
if c == "" {
_, _ = t.WriteString(Empty)
} else {
_, _ = t.WriteString(c)
}
}
_, _ = t.WriteString(constval.Reset)
_, _ = t.WriteString(global.Reset)
_, _ = t.WriteString(name)
_ = t.WriteByte('\n')
}
Expand Down
2 changes: 1 addition & 1 deletion internal/filter/itemfliter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"strings"
"time"

constval "github.com/Equationzhao/g/internal/const"
"github.com/Equationzhao/g/internal/git"
constval "github.com/Equationzhao/g/internal/global"
"github.com/Equationzhao/g/internal/item"
"github.com/Equationzhao/g/internal/osbased"
"github.com/gabriel-vasile/mimetype"
Expand Down
Loading
Loading