Skip to content

Commit

Permalink
Commonized processing (#182)
Browse files Browse the repository at this point in the history
* colorizeSpleader共通化

* defaultSpreaderの共通化

* 場所がイマイチに感じたため

* growerの共通化

* 場所がイマイチに感じたため

* mkdirerの共通化

* go fmt

* nits
  • Loading branch information
ddddddO authored Jun 24, 2023
1 parent e75b79d commit ce9a27f
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 300 deletions.
92 changes: 7 additions & 85 deletions pipeline_tree_grower.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,12 @@ func newGrowerPipeline(
enabledValidation bool,
) growerPipeline {
return &defaultGrowerPipeline{
lastNodeFormat: lastNodeFormat,
intermedialNodeFormat: intermedialNodeFormat,
enabledValidation: enabledValidation,
defaultGrowerSimple: newGrowerSimple(lastNodeFormat, intermedialNodeFormat, enabledValidation).(*defaultGrowerSimple),
}
}

func newNopGrowerPipeline() growerPipeline {
return &nopGrowerPipeline{}
}

type defaultGrowerPipeline struct {
lastNodeFormat branchFormat
intermedialNodeFormat branchFormat
enabledValidation bool
*defaultGrowerSimple
}

const workerGrowNum = 10
Expand Down Expand Up @@ -70,84 +62,16 @@ func (dg *defaultGrowerPipeline) worker(ctx context.Context, wg *sync.WaitGroup,
}
}

func (dg *defaultGrowerPipeline) assemble(current *Node) error {
if err := dg.assembleBranch(current); err != nil {
return err
}

for _, child := range current.children {
if err := dg.assemble(child); err != nil {
return err
}
}
return nil
}

func (dg *defaultGrowerPipeline) assembleBranch(current *Node) error {
current.clean() // 例えば、MkdirProgrammably funcでrootノードを使いまわすと、前回func実行時に形成されたノードの枝が残ったまま追記されてしまうため。

dg.assembleBranchDirectly(current)

// go back to the root to form a branch.
tmpParent := current.parent
if tmpParent != nil {
for ; !tmpParent.isRoot(); tmpParent = tmpParent.parent {
dg.assembleBranchIndirectly(current, tmpParent)
}
}

dg.assembleBranchFinally(current, tmpParent)

if dg.enabledValidation {
return current.validatePath()
}
return nil
}

func (dg *defaultGrowerPipeline) assembleBranchDirectly(current *Node) {
if current == nil || current.isRoot() {
return
}

current.setPath(current.name)

if current.isLastOfHierarchy() {
current.setBranch(current.branch(), dg.lastNodeFormat.directly)
} else {
current.setBranch(current.branch(), dg.intermedialNodeFormat.directly)
}
}

func (dg *defaultGrowerPipeline) assembleBranchIndirectly(current, parent *Node) {
if current == nil || parent == nil || current.isRoot() {
return
}

current.setPath(parent.name, current.path())

if parent.isLastOfHierarchy() {
current.setBranch(dg.lastNodeFormat.indirectly, current.branch())
} else {
current.setBranch(dg.intermedialNodeFormat.indirectly, current.branch())
}
}

func (*defaultGrowerPipeline) assembleBranchFinally(current, root *Node) {
if current == nil {
return
}

if root != nil {
current.setPath(root.path(), current.path())
func newNopGrowerPipeline() growerPipeline {
return &nopGrowerPipeline{
nopGrowerSimple: newNopGrowerSimple().(*nopGrowerSimple),
}
}

func (dg *defaultGrowerPipeline) enableValidation() {
dg.enabledValidation = true
type nopGrowerPipeline struct {
*nopGrowerSimple
}

type nopGrowerPipeline struct{}

func (*nopGrowerPipeline) grow(ctx context.Context, roots <-chan *Node) (<-chan *Node, <-chan error) {
nodes := make(chan *Node)
errc := make(chan error, 1)
Expand Down Expand Up @@ -175,8 +99,6 @@ func (*nopGrowerPipeline) grow(ctx context.Context, roots <-chan *Node) (<-chan
return nodes, errc
}

func (*nopGrowerPipeline) enableValidation() {}

var (
_ growerPipeline = (*defaultGrowerPipeline)(nil)
_ growerPipeline = (*nopGrowerPipeline)(nil)
Expand Down
50 changes: 3 additions & 47 deletions pipeline_tree_mkdirer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ package gtree

import (
"context"
"os"
"strings"
"sync"
)

func newMkdirerPipeline(fileExtensions []string) mkdirerPipeline {
return &defaultMkdirerPipeline{
fileConsiderer: newFileConsiderer(fileExtensions),
defaultMkdirerSimple: newMkdirerSimple(fileExtensions).(*defaultMkdirerSimple),
}
}

type defaultMkdirerPipeline struct {
fileConsiderer *fileConsiderer
*defaultMkdirerSimple
}

const workerMkdirNum = 10
Expand Down Expand Up @@ -48,7 +46,7 @@ func (dm *defaultMkdirerPipeline) worker(ctx context.Context, wg *sync.WaitGroup
if !ok {
return
}
if dm.isExistRoot(root) {
if dm.isExistRoot([]*Node{root}) {
errc <- ErrExistPath
return
}
Expand All @@ -60,46 +58,4 @@ func (dm *defaultMkdirerPipeline) worker(ctx context.Context, wg *sync.WaitGroup
}
}

func (*defaultMkdirerPipeline) isExistRoot(root *Node) bool {
if _, err := os.Stat(root.path()); !os.IsNotExist(err) {
return true
}
return false
}

func (dm *defaultMkdirerPipeline) makeDirectoriesAndFiles(current *Node) error {
if dm.fileConsiderer.isFile(current) {
dir := strings.TrimSuffix(current.path(), current.name)
if err := dm.mkdirAll(dir); err != nil {
return err
}
return dm.mkfile(current.path())
}

if !current.hasChild() {
return dm.mkdirAll(current.path())
}

for _, child := range current.children {
if err := dm.makeDirectoriesAndFiles(child); err != nil {
return err
}
}
return nil
}

const permission = 0o755

func (*defaultMkdirerPipeline) mkdirAll(dir string) error {
return os.MkdirAll(dir, permission)
}

func (*defaultMkdirerPipeline) mkfile(path string) error {
f, err := os.Create(path)
if err != nil {
return err
}
return f.Close()
}

var _ mkdirerPipeline = (*defaultMkdirerPipeline)(nil)
Loading

0 comments on commit ce9a27f

Please sign in to comment.