Skip to content

Commit

Permalink
feat(nef): add path calc (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Nov 3, 2024
1 parent 1232aa1 commit b48e5b8
Show file tree
Hide file tree
Showing 19 changed files with 911 additions and 217 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"Fastward",
"fieldalignment",
"figo",
"foobarbaz",
"fortytw",
"fsys",
"Fugazi",
Expand Down
1 change: 1 addition & 0 deletions base-op.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nef

type baseOp[F ExistsInFS] struct {
fS F
calc PathCalc
root string
}

Expand Down
48 changes: 0 additions & 48 deletions ensure-path-at.go

This file was deleted.

83 changes: 0 additions & 83 deletions ensure-path-at_test.go

This file was deleted.

45 changes: 38 additions & 7 deletions fs-absolute.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,43 @@ package nef
import (
"io/fs"
"os"
"strings"
)

type absoluteFS struct{}
type absoluteFS struct {
calc PathCalc
}

// NewUniversalABS creates an absolute universal file system
func NewUniversalABS() UniversalFS {
return &absoluteFS{}
return &absoluteFS{
calc: &AbsoluteCalc{},
}
}

// NewTraverseABS creates an absolute traverse file system
func NewTraverseABS() TraverseFS {
return &absoluteFS{}
return &absoluteFS{
calc: &AbsoluteCalc{},
}
}

// NewReaderABS creates an absolute reader file system
func NewReaderABS() ReaderFS {
return &absoluteFS{}
return &absoluteFS{
calc: &AbsoluteCalc{},
}
}

// NewWriterABS creates an absolute writer file system
func NewWriterABS() WriterFS {
return &absoluteFS{}
return &absoluteFS{
calc: &AbsoluteCalc{},
}
}

func (f *absoluteFS) Calc() PathCalc {
return f.calc
}

// FileExists does file exist at the path specified
Expand Down Expand Up @@ -103,8 +118,24 @@ func (f *absoluteFS) MakeDirAll(name string, perm os.FileMode) error {
}

// Ensure is not currently implemented on absoluteFS
func (f *absoluteFS) Ensure(_ PathAs) (string, error) {
panic("NOT-IMPL: absoluteFS.Ensure")
func (f *absoluteFS) Ensure(as PathAs) (at string, err error) {
var (
directory, file string
)
calc := f.calc

if strings.HasSuffix(as.Name, string(os.PathSeparator)) {
directory = as.Name
file = as.Default
} else {
directory, file = calc.Split(as.Name)
}

if !f.DirectoryExists(directory) {
err = f.MakeDirAll(directory, as.Perm)
}

return calc.Clean(calc.Join(directory, file)), err
}

// Move is not currently implemented on absoluteFS
Expand Down
15 changes: 8 additions & 7 deletions fs-change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package nef_test

import (
"fmt"
"path/filepath"

. "github.com/onsi/ginkgo/v2" //nolint:revive // ok
. "github.com/onsi/gomega" //nolint:revive // ok
Expand Down Expand Up @@ -65,7 +64,7 @@ var _ = Describe("op: change", Ordered, func() {
Expect(require(root, entry.from)).To(Succeed())
},
action: func(entry fsTE[nef.UniversalFS], fS nef.UniversalFS) {
destination := filepath.Base(entry.to)
destination := fS.Calc().Base(entry.to)
Expect(fS.Change(entry.from, destination)).To(Succeed(),
fmt.Sprintf("OVERWRITE: %v", entry.overwrite),
)
Expand All @@ -88,7 +87,8 @@ var _ = Describe("op: change", Ordered, func() {
Expect(fS.Change(entry.from, entry.to)).To(Succeed(),
fmt.Sprintf("OVERWRITE: %v", entry.overwrite),
)
Expect(luna.AsFile(luna.Yoke(lab.Static.FS.Scratch, entry.to))).To(luna.ExistInFS(fS))
calc := fS.Calc()
Expect(luna.AsFile(calc.Join(lab.Static.FS.Scratch, entry.to))).To(luna.ExistInFS(fS))
Expect(luna.AsDirectory(entry.to)).NotTo(luna.ExistInFS(fS))
},
}),
Expand All @@ -107,7 +107,8 @@ var _ = Describe("op: change", Ordered, func() {
Expect(fS.Change(entry.from, entry.to)).To(Succeed(),
fmt.Sprintf("OVERWRITE: %v", entry.overwrite),
)
file := luna.Yoke(lab.Static.FS.Scratch, entry.to)
calc := fS.Calc()
file := calc.Join(lab.Static.FS.Scratch, entry.to)
Expect(luna.AsFile(file)).To(luna.ExistInFS(fS))
Expect(luna.AsDirectory(entry.to)).NotTo(luna.ExistInFS(fS))
},
Expand All @@ -125,7 +126,7 @@ var _ = Describe("op: change", Ordered, func() {
Expect(require(root, entry.to)).To(Succeed())
},
action: func(entry fsTE[nef.UniversalFS], fS nef.UniversalFS) {
destination := filepath.Base(entry.to)
destination := fS.Calc().Base(entry.to)
Expect(fS.Change(entry.from, destination)).NotTo(Succeed(),
fmt.Sprintf("OVERWRITE: %v", entry.overwrite),
)
Expand All @@ -141,14 +142,14 @@ var _ = Describe("op: change", Ordered, func() {
require: lab.Static.FS.Scratch,
from: lab.Static.FS.Change.From.File,
to: lab.Static.FS.Change.To.File,
arrange: func(entry fsTE[nef.UniversalFS], _ nef.UniversalFS) {
arrange: func(entry fsTE[nef.UniversalFS], fS nef.UniversalFS) {
Expect(require(root,
entry.require,
entry.from,
)).To(Succeed())
Expect(require(root,
entry.require,
luna.Yoke(lab.Static.FS.Scratch, entry.to),
fS.Calc().Join(lab.Static.FS.Scratch, entry.to),
)).To(Succeed())
},
action: func(entry fsTE[nef.UniversalFS], fS nef.UniversalFS) {
Expand Down
9 changes: 6 additions & 3 deletions fs-changer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package nef

import (
"os"
"path/filepath"
"strings"
"sync"

Expand Down Expand Up @@ -81,8 +80,8 @@ func (m *baseChanger) rename(from, to string) error {
}

return os.Rename(
filepath.Join(m.root, from),
filepath.Join(m.root, destination),
m.calc.Join(m.root, from),
m.calc.Join(m.root, destination),
)
}

Expand All @@ -102,12 +101,15 @@ func (l *lazyChanger) instance(root string, overwrite bool, fS ChangerFS) change
func (l *lazyChanger) create(root string, overwrite bool, fS ChangerFS) changer {
// create an interface for this function
//
calc := fS.Calc()

return lo.TernaryF(overwrite,
func() changer {
return &overwriteChanger{
baseChanger: baseChanger{
baseOp: baseOp[ChangerFS]{
fS: fS,
calc: calc,
root: root,
},
},
Expand All @@ -118,6 +120,7 @@ func (l *lazyChanger) create(root string, overwrite bool, fS ChangerFS) changer
baseChanger: baseChanger{
baseOp: baseOp[ChangerFS]{
fS: fS,
calc: calc,
root: root,
},
},
Expand Down
Loading

0 comments on commit b48e5b8

Please sign in to comment.