aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: f09a5458b2572762b132ef5040aa663c8f1c33be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main

import (
	"fmt"
	"os"
	"path/filepath"
	"porg/fileop"
	"porg/pipe"
)

type SHAInfo struct {
	path   string
	sha256 string
}

type MoveCommand struct {
	src  string
	dest string
}

func Calculate(file string) *SHAInfo {
	sha, err := fileop.CalculateSHA256(file)
	if err != nil {
		fmt.Println(err)
		return nil
	} else {
		return &SHAInfo{path: file, sha256: sha}
	}
}

func Move(info *SHAInfo) *MoveCommand {

	base := filepath.Dir(info.path)
	ext := filepath.Ext(info.path)
	head := info.sha256[0:2]
	tail := info.sha256[2:]
	newPath := fmt.Sprintf("%s/%s/%s%s", base, head, tail, ext)
	return &MoveCommand{src: info.path, dest: newPath}

}

func Apply(move *MoveCommand) {
	dir := filepath.Dir(move.dest)
	os.Mkdir(dir, 0755)
	if err := os.Rename(move.src, move.dest); err != nil {
		fmt.Println(err)
	}
}

func main() {
	files := fileop.WalkFolder("")
	shas := pipe.Proc(files, 8, Calculate)
	cmds := pipe.Proc(shas, 1, Move)
	pipe.TailProc(cmds, 4, Apply)
}