aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go55
1 files changed, 55 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..f09a545
--- /dev/null
+++ b/main.go
@@ -0,0 +1,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)
+}