diff options
author | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2022-08-28 17:03:38 +0200 |
---|---|---|
committer | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2022-08-28 17:07:40 +0200 |
commit | 544bbeeaf836436305cbed87ae1019511de62535 (patch) | |
tree | 8c7b020bba75d4d850d4a1d538e7bc7db2f01c33 /main.go | |
download | porg-544bbeeaf836436305cbed87ae1019511de62535.tar.gz porg-544bbeeaf836436305cbed87ae1019511de62535.tar.bz2 porg-544bbeeaf836436305cbed87ae1019511de62535.zip |
feat: Add init source code
For now only `pipe` is feature complete. The order module needs to be
changed a lot.
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 55 |
1 files changed, 55 insertions, 0 deletions
@@ -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) +} |