From 544bbeeaf836436305cbed87ae1019511de62535 Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Sun, 28 Aug 2022 17:03:38 +0200 Subject: feat: Add init source code For now only `pipe` is feature complete. The order module needs to be changed a lot. --- main.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 main.go (limited to 'main.go') 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) +} -- cgit v1.2.3