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. --- fileop/fileop.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 fileop/fileop.go (limited to 'fileop/fileop.go') diff --git a/fileop/fileop.go b/fileop/fileop.go new file mode 100644 index 0000000..d08cb82 --- /dev/null +++ b/fileop/fileop.go @@ -0,0 +1,66 @@ +package fileop + +import ( + "crypto/sha256" + "fmt" + "io" + "os" + "path/filepath" +) + +func WalkFolder(folder string) <-chan string { + c := make(chan string) + + go func(folder string, c chan string) { + filepath.Walk(folder, func(path string, info os.FileInfo, err error) error { + file, _ := os.Open(path) + defer file.Close() + fileInfo, _ := file.Stat() + if !fileInfo.IsDir() { + c <- path + } + return nil + }) + close(c) + + }(folder, c) + + return c +} + +func CalculateSHA256(file string) (string, error) { + f, err := os.Open(file) + if err != nil { + return "", err + } + defer f.Close() + + h := sha256.New() + if _, err := io.Copy(h, f); err != nil { + return "", err + } + + shaString := fmt.Sprintf("%x", h.Sum(nil)) + + return shaString, nil +} + +type MoveCommand struct { + Source string + Destination string +} + +func Move() chan<- *MoveCommand { + c := make(chan *MoveCommand) + go func(chan *MoveCommand) { + for cmd := range c { + // TODO: add error handling + err := os.Rename(cmd.Source, cmd.Destination) + if err != nil { + fmt.Println(err.Error()) + } + } + }(c) + + return c +} -- cgit v1.2.3