From a872707481c128d5cc7ad5f0575a9e6aa0a3554b Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 4 May 2020 23:54:10 +0800 Subject: [PATCH 1/8] feat: tmpbin --- README.md | 24 ++++++ conf.yml | 7 ++ foldest.go | 205 ++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 5 ++ go.sum | 3 + utils/yaml.go | 13 ++++ 6 files changed, 257 insertions(+) create mode 100644 README.md create mode 100644 conf.yml create mode 100644 foldest.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 utils/yaml.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..eab2720 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +# Foldest-go + +Automatically manage your folder. + +## How to use + +1. Download binary file in release page +2. Download `conf.yml` and place it in the same folder with binary file +3. Configure settings in `conf.yml`: +```yml +verbose: # verbose output, false as default +targetdir: # the folder you want to manage, you can set it within the program. Last setted folder will be remembered. +tmpbin: + enable: # whether to use tmpbin, false as default + name: tmpbin/ # name of tmpbin, "tmpbin/" as default + treshday: 30 # files not modified for more than this long will be moved into tmpbin, 30 days as default + deleteday: 30 # files in tmpbin for more than this long will be deleted, 30 days as default +``` + +## Development progress + +- [ ] Automatic +- [ ] Temp trash bin +- [ ] Customize rules diff --git a/conf.yml b/conf.yml new file mode 100644 index 0000000..e17518b --- /dev/null +++ b/conf.yml @@ -0,0 +1,7 @@ +verbose: +targetdir: +tmpbin: + enable: + name: + treshday: + deleteday: diff --git a/foldest.go b/foldest.go new file mode 100644 index 0000000..756c60a --- /dev/null +++ b/foldest.go @@ -0,0 +1,205 @@ +package main + +import ( + "fmt" + "foldest-go/utils" + "io" + "io/ioutil" + "os" + "strings" + "time" + + yaml "gopkg.in/yaml.v2" +) + +func main() { + // Read conf.yml + fmt.Println("Reading conf.yml ...") + conf := new(utils.Yaml) + if _, err := os.Stat("conf.yml"); os.IsNotExist(err) { + fmt.Println("conf.yml not found, starting with default value ...") + } else { + yamlFile, err := ioutil.ReadFile("conf.yml") + if err != nil { + fmt.Printf("Error while reading conf.yml :\n") + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + } + err = yaml.Unmarshal(yamlFile, conf) + if err != nil { + fmt.Printf("Error while reading conf.yml :\n") + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + } + } + + // Set path + var path string + path = conf.Targetdir + isChanged := false + + for path == "" { + fmt.Println("Please input path of the target folder:") + fmt.Scanln(&path) + if !strings.HasSuffix(path, "/") { + path = path + "/" + } + fmt.Printf("Scanning %c[0;34m%s%c[0m ...\n", 0x1B, path, 0x1B) + info, err := os.Stat(path) + if err != nil || !info.IsDir() { + fmt.Printf("Error while scanning %c[0;34m%s%c[0m :", 0x1B, path, 0x1B) + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + path = "" + continue + } + conf.Targetdir = path + isChanged = true + } + + // Set default values + if conf.Tmpbin.Name == "" { + conf.Tmpbin.Name = "tmpbin/" + } + if !strings.HasSuffix(conf.Tmpbin.Name, "/") { + conf.Tmpbin.Name = conf.Tmpbin.Name + "/" + } + if conf.Tmpbin.Thresh == 0 { + conf.Tmpbin.Thresh = 30 + } + if conf.Tmpbin.Delete == 0 { + conf.Tmpbin.Delete = 30 + } + + // Scan target dir + fmt.Printf("Scanning %c[0;34m%s%c[0m ...\n", 0x1B, path, 0x1B) + if !strings.HasSuffix(path, "/") { + path = path + "/" + } + dir, err := ioutil.ReadDir(path) + if err != nil { + fmt.Printf("Error while scanning %c[0;34m%s%c[0m :", 0x1B, path, 0x1B) + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + return + } + + fmt.Println("Press any key to start...") + fmt.Scanln() + + // Make dir 'tmpbin/' + + _, err = os.Stat(path + conf.Tmpbin.Name) + if err != nil { + fmt.Printf("Making folder %c[0;34m%s%c[0m ...\n", 0x1B, conf.Tmpbin.Name, 0x1B) + err := os.Mkdir(path+conf.Tmpbin.Name, 0777) + if err != nil { + fmt.Printf("Error while making folder %c[0;34m%s%c[0m ...\n", 0x1B, conf.Tmpbin.Name, 0x1B) + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + } + } + + // Operations on files + if conf.Tmpbin.Enable { + for count, file := range dir { + if count > 10 { + break + } + modTime, strerr := GetFileModTime(path + file.Name()) + if strerr == "" { + // jump tmpbin + if file.Name() == conf.Tmpbin.Name { + continue + } + + if conf.Verbose { + fmt.Printf("%c[0;34m%s%c[0m %c[0;32m%s%c[0m\n", 0x1B, file.Name(), 0x1B, 0x1B, modTime, 0x1B) + } + + // If file reaches thresh + if time.Now().Unix()-modTime.Unix() >= int64(conf.Tmpbin.Thresh*86400) { + if conf.Verbose { + fmt.Printf("Moving %c[0;34m%s%c[0m\n", 0x1B, file.Name(), 0x1B) + } + src := path + file.Name() + des := path + conf.Tmpbin.Name + file.Name() + + // Check if file already existed in tmpbin + if _, err := os.Stat(des); !os.IsNotExist(err) { + fmt.Printf("Error while moving %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B) + fmt.Printf("\t%c[0;31m%s already existed in %s%c[0m\n", 0x1B, file.Name(), conf.Tmpbin.Name, 0x1B) + } else { + if !file.IsDir() { // file, not folder + _, err := CopyFile(src, des) + if err != nil { + fmt.Printf("Error while moving %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B) + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + } + err = os.Remove(src) + if err != nil { + fmt.Printf("Error while moving %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B) + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + } + } else { // folder + err := os.Rename(src, des) + if err != nil { + fmt.Printf("Error while moving %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B) + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + } + } + } + } + + } else { + fmt.Printf("Error while scanning %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B) + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + } + } + } + + // Save conf.yml + if isChanged { + fmt.Println("Saving conf.yml ...") + yamlChanged, err := yaml.Marshal(conf) + if err != nil { + fmt.Printf("Error while saving conf.yml :\n") + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + } + err = ioutil.WriteFile("conf.yml", yamlChanged, 0644) + } + +} + +// GetFileModTime :获取文件修改时间 返回时间 +func GetFileModTime(path string) (t time.Time, strerr string) { + f, err := os.Open(path) + if err != nil { + return time.Now(), "open file error" + } + defer f.Close() + + fi, err := f.Stat() + if err != nil { + return time.Now(), "stat fileinfo error" + } + + return fi.ModTime(), "" +} + +// CopyFile : via io.Copy +func CopyFile(src, des string) (written int64, err error) { + srcFile, err := os.Open(src) + if err != nil { + return 0, err + } + defer srcFile.Close() + + //获取源文件的权限 + fi, _ := srcFile.Stat() + perm := fi.Mode() + + //desFile, err := os.Create(des) //无法复制源文件的所有权限 + desFile, err := os.OpenFile(des, os.O_RDWR|os.O_CREATE|os.O_TRUNC, perm) //复制源文件的所有权限 + if err != nil { + return 0, err + } + defer desFile.Close() + + return io.Copy(desFile, srcFile) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..36140df --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module foldest-go + +go 1.13 + +require gopkg.in/yaml.v2 v2.2.8 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..0c4da7f --- /dev/null +++ b/go.sum @@ -0,0 +1,3 @@ +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/utils/yaml.go b/utils/yaml.go new file mode 100644 index 0000000..2c9146b --- /dev/null +++ b/utils/yaml.go @@ -0,0 +1,13 @@ +package utils + +// Yaml : a struct for conf.yml +type Yaml struct { + Verbose bool `yaml:"verbose"` + Targetdir string `yaml:"targetdir"` + Tmpbin struct { + Enable bool `yaml:"enable"` + Name string `yaml:"name"` + Thresh int `yaml:"treshday"` + Delete int `yaml:"deleteday"` + } +} From d4aec02a64b4a42a4afb542879db6a7aba56af00 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 5 May 2020 00:03:10 +0800 Subject: [PATCH 2/8] feat: wait before exit --- foldest.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/foldest.go b/foldest.go index 756c60a..57812f3 100644 --- a/foldest.go +++ b/foldest.go @@ -42,7 +42,6 @@ func main() { if !strings.HasSuffix(path, "/") { path = path + "/" } - fmt.Printf("Scanning %c[0;34m%s%c[0m ...\n", 0x1B, path, 0x1B) info, err := os.Stat(path) if err != nil || !info.IsDir() { fmt.Printf("Error while scanning %c[0;34m%s%c[0m :", 0x1B, path, 0x1B) @@ -164,6 +163,9 @@ func main() { err = ioutil.WriteFile("conf.yml", yamlChanged, 0644) } + fmt.Println("Press any key to exit...") + fmt.Scanln() + } // GetFileModTime :获取文件修改时间 返回时间 From 78216cc2b6cf90c8fc11844357b54731156b9e52 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 5 May 2020 13:00:06 +0800 Subject: [PATCH 3/8] feat: delete file in tmpbin --- .gitignore | 3 + README.md | 2 +- conf.yml | 14 ++-- foldest.go | 198 ++---------------------------------------------- utils/conf.go | 100 ++++++++++++++++++++++++ utils/file.go | 70 +++++++++++++++++ utils/tmpbin.go | 115 ++++++++++++++++++++++++++++ utils/yaml.go | 9 ++- 8 files changed, 308 insertions(+), 203 deletions(-) create mode 100644 utils/conf.go create mode 100644 utils/file.go create mode 100644 utils/tmpbin.go diff --git a/.gitignore b/.gitignore index 66fd13c..ddd33b5 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ *.dll *.so *.dylib +foldest-linux* # Test binary, built with `go test -c` *.test @@ -13,3 +14,5 @@ # Dependency directories (remove the comment below to include it) # vendor/ + +*.code-workspace diff --git a/README.md b/README.md index eab2720..d121007 100644 --- a/README.md +++ b/README.md @@ -20,5 +20,5 @@ tmpbin: ## Development progress - [ ] Automatic -- [ ] Temp trash bin +- [x] Temp trash bin - [ ] Customize rules diff --git a/conf.yml b/conf.yml index e17518b..d731241 100644 --- a/conf.yml +++ b/conf.yml @@ -1,7 +1,9 @@ -verbose: -targetdir: +verbose: true +targetdir: tmpbin: - enable: - name: - treshday: - deleteday: + enable: false + name: tmpbin/ + treshday: 30 + deleteday: 30 + ignore: + - .accelerate diff --git a/foldest.go b/foldest.go index 57812f3..11a5373 100644 --- a/foldest.go +++ b/foldest.go @@ -3,205 +3,19 @@ package main import ( "fmt" "foldest-go/utils" - "io" - "io/ioutil" - "os" - "strings" - "time" - - yaml "gopkg.in/yaml.v2" ) func main() { - // Read conf.yml - fmt.Println("Reading conf.yml ...") - conf := new(utils.Yaml) - if _, err := os.Stat("conf.yml"); os.IsNotExist(err) { - fmt.Println("conf.yml not found, starting with default value ...") - } else { - yamlFile, err := ioutil.ReadFile("conf.yml") - if err != nil { - fmt.Printf("Error while reading conf.yml :\n") - fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) - } - err = yaml.Unmarshal(yamlFile, conf) - if err != nil { - fmt.Printf("Error while reading conf.yml :\n") - fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) - } - } - - // Set path - var path string - path = conf.Targetdir - isChanged := false - - for path == "" { - fmt.Println("Please input path of the target folder:") - fmt.Scanln(&path) - if !strings.HasSuffix(path, "/") { - path = path + "/" - } - info, err := os.Stat(path) - if err != nil || !info.IsDir() { - fmt.Printf("Error while scanning %c[0;34m%s%c[0m :", 0x1B, path, 0x1B) - fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) - path = "" - continue - } - conf.Targetdir = path - isChanged = true - } - - // Set default values - if conf.Tmpbin.Name == "" { - conf.Tmpbin.Name = "tmpbin/" - } - if !strings.HasSuffix(conf.Tmpbin.Name, "/") { - conf.Tmpbin.Name = conf.Tmpbin.Name + "/" - } - if conf.Tmpbin.Thresh == 0 { - conf.Tmpbin.Thresh = 30 - } - if conf.Tmpbin.Delete == 0 { - conf.Tmpbin.Delete = 30 - } - - // Scan target dir - fmt.Printf("Scanning %c[0;34m%s%c[0m ...\n", 0x1B, path, 0x1B) - if !strings.HasSuffix(path, "/") { - path = path + "/" - } - dir, err := ioutil.ReadDir(path) - if err != nil { - fmt.Printf("Error while scanning %c[0;34m%s%c[0m :", 0x1B, path, 0x1B) - fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) - return - } - - fmt.Println("Press any key to start...") + conf := utils.ReadConf() + fmt.Println("Press enter to start...") fmt.Scanln() - // Make dir 'tmpbin/' - - _, err = os.Stat(path + conf.Tmpbin.Name) - if err != nil { - fmt.Printf("Making folder %c[0;34m%s%c[0m ...\n", 0x1B, conf.Tmpbin.Name, 0x1B) - err := os.Mkdir(path+conf.Tmpbin.Name, 0777) - if err != nil { - fmt.Printf("Error while making folder %c[0;34m%s%c[0m ...\n", 0x1B, conf.Tmpbin.Name, 0x1B) - fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) - } - } - - // Operations on files if conf.Tmpbin.Enable { - for count, file := range dir { - if count > 10 { - break - } - modTime, strerr := GetFileModTime(path + file.Name()) - if strerr == "" { - // jump tmpbin - if file.Name() == conf.Tmpbin.Name { - continue - } - - if conf.Verbose { - fmt.Printf("%c[0;34m%s%c[0m %c[0;32m%s%c[0m\n", 0x1B, file.Name(), 0x1B, 0x1B, modTime, 0x1B) - } - - // If file reaches thresh - if time.Now().Unix()-modTime.Unix() >= int64(conf.Tmpbin.Thresh*86400) { - if conf.Verbose { - fmt.Printf("Moving %c[0;34m%s%c[0m\n", 0x1B, file.Name(), 0x1B) - } - src := path + file.Name() - des := path + conf.Tmpbin.Name + file.Name() - - // Check if file already existed in tmpbin - if _, err := os.Stat(des); !os.IsNotExist(err) { - fmt.Printf("Error while moving %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B) - fmt.Printf("\t%c[0;31m%s already existed in %s%c[0m\n", 0x1B, file.Name(), conf.Tmpbin.Name, 0x1B) - } else { - if !file.IsDir() { // file, not folder - _, err := CopyFile(src, des) - if err != nil { - fmt.Printf("Error while moving %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B) - fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) - } - err = os.Remove(src) - if err != nil { - fmt.Printf("Error while moving %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B) - fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) - } - } else { // folder - err := os.Rename(src, des) - if err != nil { - fmt.Printf("Error while moving %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B) - fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) - } - } - } - } - - } else { - fmt.Printf("Error while scanning %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B) - fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) - } - } - } - - // Save conf.yml - if isChanged { - fmt.Println("Saving conf.yml ...") - yamlChanged, err := yaml.Marshal(conf) - if err != nil { - fmt.Printf("Error while saving conf.yml :\n") - fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) - } - err = ioutil.WriteFile("conf.yml", yamlChanged, 0644) + utils.Manage(conf) + } else { + fmt.Println("tmpbin is disabled, skipping...") } - fmt.Println("Press any key to exit...") + fmt.Println("Press enter to exit...") fmt.Scanln() - -} - -// GetFileModTime :获取文件修改时间 返回时间 -func GetFileModTime(path string) (t time.Time, strerr string) { - f, err := os.Open(path) - if err != nil { - return time.Now(), "open file error" - } - defer f.Close() - - fi, err := f.Stat() - if err != nil { - return time.Now(), "stat fileinfo error" - } - - return fi.ModTime(), "" -} - -// CopyFile : via io.Copy -func CopyFile(src, des string) (written int64, err error) { - srcFile, err := os.Open(src) - if err != nil { - return 0, err - } - defer srcFile.Close() - - //获取源文件的权限 - fi, _ := srcFile.Stat() - perm := fi.Mode() - - //desFile, err := os.Create(des) //无法复制源文件的所有权限 - desFile, err := os.OpenFile(des, os.O_RDWR|os.O_CREATE|os.O_TRUNC, perm) //复制源文件的所有权限 - if err != nil { - return 0, err - } - defer desFile.Close() - - return io.Copy(desFile, srcFile) } diff --git a/utils/conf.go b/utils/conf.go new file mode 100644 index 0000000..3b9c7ae --- /dev/null +++ b/utils/conf.go @@ -0,0 +1,100 @@ +package utils + +import ( + "fmt" + "io/ioutil" + "os" + "strings" + + "gopkg.in/yaml.v2" +) + +// ReadConf : Read conf.yml +func ReadConf() (conf *Yaml) { + fmt.Println("Reading conf.yml ...") + conf = new(Yaml) + if _, err := os.Stat("conf.yml"); os.IsNotExist(err) { + fmt.Println("conf.yml not found, starting with default value ...") + } else { + yamlFile, err := ioutil.ReadFile("conf.yml") + if err != nil { + fmt.Printf("Error while reading conf.yml :\n") + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + } + err = yaml.Unmarshal(yamlFile, conf) + if err != nil { + fmt.Printf("Error while reading conf.yml :\n") + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + } + } + + // Set path + var isChanged bool + isChanged = SetPath(&conf.Targetdir) + + // Set default values + SetDefault(conf) + + if isChanged { + SaveConf(conf) + } + + return conf +} + +// SetPath : Set target dir +func SetPath(path *string) (isChanged bool) { + isChanged = false + + for { + if *path == "" { + fmt.Println("Please input path of the target folder:") + fmt.Scanln(path) + isChanged = true + } + + if !strings.HasSuffix(*path, "/") { + *path = *path + "/" + isChanged = true + } + + if CheckDir(*path) { + break + } + } + + return isChanged +} + +// SetDefault : Set default value of the conf +func SetDefault(conf *Yaml) { + if conf.Tmpbin.Name == "" { + conf.Tmpbin.Name = "tmpbin/" + } + if !strings.HasSuffix(conf.Tmpbin.Name, "/") { + conf.Tmpbin.Name = conf.Tmpbin.Name + "/" + } + + if conf.Tmpbin.Thresh == 0 { + conf.Tmpbin.Thresh = 30 + } + + if conf.Tmpbin.Delete == 0 { + conf.Tmpbin.Delete = 30 + } + + if len(conf.Tmpbin.Ignore) == 0 { + conf.Tmpbin.Ignore = append(conf.Tmpbin.Ignore, ".accelerate") + } +} + +// SaveConf : Save the conf.yml +func SaveConf(conf *Yaml) { + fmt.Println("Saving conf.yml ...") + yamlChanged, err := yaml.Marshal(conf) + if err != nil { + fmt.Printf("Error while saving conf.yml :\n") + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + } + err = ioutil.WriteFile("conf.yml", yamlChanged, 0644) +} diff --git a/utils/file.go b/utils/file.go new file mode 100644 index 0000000..e27ca6e --- /dev/null +++ b/utils/file.go @@ -0,0 +1,70 @@ +package utils + +import ( + "fmt" + "io" + "io/ioutil" + "os" + "time" +) + +// GetFileModTime :获取文件修改时间 返回时间 +func GetFileModTime(path string) (t time.Time, strerr string) { + f, err := os.Open(path) + if err != nil { + return time.Now(), "open file error" + } + defer f.Close() + + fi, err := f.Stat() + if err != nil { + return time.Now(), "stat fileinfo error" + } + + return fi.ModTime(), "" +} + +// CopyFile : via io.Copy +func CopyFile(src, des string) (written int64, err error) { + srcFile, err := os.Open(src) + if err != nil { + return 0, err + } + defer srcFile.Close() + + //获取源文件的权限 + fi, _ := srcFile.Stat() + perm := fi.Mode() + + //desFile, err := os.Create(des) //无法复制源文件的所有权限 + desFile, err := os.OpenFile(des, os.O_RDWR|os.O_CREATE|os.O_TRUNC, perm) //复制源文件的所有权限 + if err != nil { + return 0, err + } + defer desFile.Close() + + return io.Copy(desFile, srcFile) +} + +// CheckDir : Check if path is a folder +func CheckDir(path string) (isDir bool) { + info, err := os.Stat(path) + if err != nil || !info.IsDir() { + fmt.Printf("Error while scanning %c[0;34m%s%c[0m :", 0x1B, path, 0x1B) + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + return false + } + return true +} + +//OpenDir : Open a folder +func OpenDir(path string) (dir []os.FileInfo) { + fmt.Printf("Scanning %c[0;34m%s%c[0m ...\n", 0x1B, path, 0x1B) + dir, err := ioutil.ReadDir(path) + if err != nil { + fmt.Printf("Error while scanning %c[0;34m%s%c[0m :", 0x1B, path, 0x1B) + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + return nil + } + return dir +} diff --git a/utils/tmpbin.go b/utils/tmpbin.go new file mode 100644 index 0000000..56041e9 --- /dev/null +++ b/utils/tmpbin.go @@ -0,0 +1,115 @@ +package utils + +import ( + "fmt" + "os" + "time" +) + +// Manage folder +func Manage(conf *Yaml) { + // Make dir 'tmpbin/' + _, err := os.Stat(conf.Targetdir + conf.Tmpbin.Name) + if err != nil { + fmt.Printf("Making folder %c[0;34m%s%c[0m ...\n", 0x1B, conf.Tmpbin.Name, 0x1B) + err := os.Mkdir(conf.Targetdir+conf.Tmpbin.Name, 0777) + if err != nil { + fmt.Printf("Error while making folder %c[0;34m%s%c[0m ...\n", 0x1B, conf.Tmpbin.Name, 0x1B) + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + } + } + + dir := OpenDir(conf.Targetdir) + if dir == nil { + return + } + + // Moving files to tmpbin + for count, file := range dir { + if count > 10 { + break + } + modTime, strerr := GetFileModTime(conf.Targetdir + file.Name()) + if strerr == "" { + // jump tmpbin + if file.Name() == conf.Tmpbin.Name { + continue + } + + if conf.Verbose { + fmt.Printf("%c[0;34m%s%c[0m %c[0;32m%s%c[0m\n", 0x1B, file.Name(), 0x1B, 0x1B, modTime, 0x1B) + } + + // If file reaches thresh + if time.Now().Unix()-modTime.Unix() >= int64(conf.Tmpbin.Thresh*86400) { + //if conf.Verbose { + fmt.Printf("Moving %c[0;34m%s%c[0m\n", 0x1B, file.Name(), 0x1B) + //} + src := conf.Targetdir + file.Name() + des := conf.Targetdir + conf.Tmpbin.Name + file.Name() + + // Check if file already existed in tmpbin + if _, err := os.Stat(des); !os.IsNotExist(err) { + fmt.Printf("Error while moving %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B) + fmt.Printf("\t%c[0;31m%s already existed in %s%c[0m\n", 0x1B, file.Name(), conf.Tmpbin.Name, 0x1B) + } else { + if !file.IsDir() { // file, not folder + _, err := CopyFile(src, des) + if err != nil { + fmt.Printf("Error while moving %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B) + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + } + err = os.Remove(src) + if err != nil { + fmt.Printf("Error while moving %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B) + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + } + } else { // folder + err := os.Rename(src, des) + if err != nil { + fmt.Printf("Error while moving %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B) + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + } + } + } + } + + } else { + fmt.Printf("Error while scanning %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B) + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + } + } + + // Delete files in tmpbin/ + dir = OpenDir(conf.Targetdir + conf.Tmpbin.Name) + if dir == nil { + return + } + for count, file := range dir { + if count > 10 { + break + } + modTime, strerr := GetFileModTime(conf.Targetdir + conf.Tmpbin.Name + file.Name()) + if strerr == "" { + if conf.Verbose { + fmt.Printf("%c[0;34m%s%c[0m %c[0;32m%s%c[0m\n", 0x1B, file.Name(), 0x1B, 0x1B, modTime, 0x1B) + } + + // If file reaches deleteday + if time.Now().Unix()-modTime.Unix() >= int64(conf.Tmpbin.Delete*86400) { + //if conf.Verbose { + fmt.Printf("Deleting %c[0;34m%s%c[0m\n", 0x1B, file.Name(), 0x1B) + //} + src := conf.Targetdir + conf.Tmpbin.Name + file.Name() + err = os.Remove(src) + if err != nil { + fmt.Printf("Error while deleting %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B) + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + } + } + } else { + fmt.Printf("Error while scanning %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B) + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + } + } +} diff --git a/utils/yaml.go b/utils/yaml.go index 2c9146b..06cd612 100644 --- a/utils/yaml.go +++ b/utils/yaml.go @@ -5,9 +5,10 @@ type Yaml struct { Verbose bool `yaml:"verbose"` Targetdir string `yaml:"targetdir"` Tmpbin struct { - Enable bool `yaml:"enable"` - Name string `yaml:"name"` - Thresh int `yaml:"treshday"` - Delete int `yaml:"deleteday"` + Enable bool `yaml:"enable"` + Name string `yaml:"name"` + Thresh int `yaml:"treshday"` + Delete int `yaml:"deleteday"` + Ignore []string `yaml:"ignore"` } } From d96d564dc851371e251844e05e47455b0329853d Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 5 May 2020 17:01:43 +0800 Subject: [PATCH 4/8] feat: customize regex --- foldest.go | 7 ++++ rules.yml | 7 ++++ utils/classify.go | 103 ++++++++++++++++++++++++++++++++++++++++++++++ utils/conf.go | 8 ++-- utils/file.go | 29 +++++++++++++ utils/tmpbin.go | 28 +------------ utils/yaml.go | 25 ++++++++++- 7 files changed, 175 insertions(+), 32 deletions(-) create mode 100644 rules.yml create mode 100644 utils/classify.go diff --git a/foldest.go b/foldest.go index 11a5373..3134276 100644 --- a/foldest.go +++ b/foldest.go @@ -10,6 +10,13 @@ func main() { fmt.Println("Press enter to start...") fmt.Scanln() + rules := utils.ReadRules() + if rules == nil { + fmt.Println("Skipping classify...") + } else { + utils.DoClassify(rules, conf.Targetdir, conf.Verbose) + } + if conf.Tmpbin.Enable { utils.Manage(conf) } else { diff --git a/rules.yml b/rules.yml new file mode 100644 index 0000000..b82afc8 --- /dev/null +++ b/rules.yml @@ -0,0 +1,7 @@ +rule1: + enable: true + name: document + regex: + - ".*?.doc" + - ".*?.docx" + - ".*?.pdf" \ No newline at end of file diff --git a/utils/classify.go b/utils/classify.go new file mode 100644 index 0000000..c0a75d0 --- /dev/null +++ b/utils/classify.go @@ -0,0 +1,103 @@ +package utils + +import ( + "fmt" + "io/ioutil" + "os" + "reflect" + "regexp" + "strings" + + "gopkg.in/yaml.v2" +) + +// ReadRules : Read rules.yml +func ReadRules() (rules *Rules) { + fmt.Println("Reading conf.yml ...") + rules = new(Rules) + + if _, err := os.Stat("rules.yml"); os.IsNotExist(err) { + fmt.Println("rules.yml not found, skipping ...") + return nil + } + + yamlFile, err := ioutil.ReadFile("rules.yml") + if err != nil { + fmt.Printf("Error while reading rules.yml :\n") + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + } + err = yaml.Unmarshal(yamlFile, rules) + if err != nil { + fmt.Printf("Error while reading rules.yml :\n") + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + } + + return rules +} + +// DoClassify : +func DoClassify(rules *Rules, path string, isVerbose bool) { + rType := reflect.TypeOf(rules) + rVal := reflect.ValueOf(rules) + if rType.Kind() == reflect.Ptr { + // 传入的rules是指针,需要.Elem()取得指针指向的value + rType = rType.Elem() + rVal = rVal.Elem() + } else { + panic("rules must be ptr to struct") + } + for i := 0; i < rType.NumField(); i++ { + rule := rVal.Field(i).Interface().(Rule) + if rule.Enable { + doRule(&rule, path, isVerbose) + } + } +} + +// doRule : +func doRule(rule *Rule, path string, isVerbose bool) { + fmt.Printf("Performing rule %c[0;33m%s%c[0m ...\n", 0x1B, rule.Name, 0x1B) + if !strings.HasSuffix(rule.Name, "/") { + rule.Name = rule.Name + "/" + } + _, err := os.Stat(path + rule.Name) + if err != nil { + fmt.Printf("Making folder %c[0;33m%s%c[0m ...\n", 0x1B, rule.Name, 0x1B) + err := os.Mkdir(path+rule.Name, 0777) + if err != nil { + fmt.Printf("Error while making folder %c[0;33m%s%c[0m ...\n", 0x1B, rule.Name, 0x1B) + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + } + } + + dir := OpenDir(path) + if dir == nil { + return + } + + // Moving files to rule dir + for count, file := range dir { + if count > 10 { + break + } + + // jump rule dir + if file.Name() == rule.Name || file.IsDir() { + continue + } + + for _, pattern := range rule.Regex { + re := regexp.MustCompile(pattern) + match := re.MatchString(file.Name()) + if match { + //if isVerbose { + fmt.Printf("%c[0;34m%s%c[0m matches %c[0;33m%s%c[0m\n", 0x1B, file.Name(), 0x1B, 0x1B, rule.Name, 0x1B) + //} + + src := path + file.Name() + des := path + rule.Name + file.Name() + MoveAll(file, src, des) + } + } + } +} diff --git a/utils/conf.go b/utils/conf.go index 3b9c7ae..01a5709 100644 --- a/utils/conf.go +++ b/utils/conf.go @@ -10,9 +10,9 @@ import ( ) // ReadConf : Read conf.yml -func ReadConf() (conf *Yaml) { +func ReadConf() (conf *Conf) { fmt.Println("Reading conf.yml ...") - conf = new(Yaml) + conf = new(Conf) if _, err := os.Stat("conf.yml"); os.IsNotExist(err) { fmt.Println("conf.yml not found, starting with default value ...") } else { @@ -67,7 +67,7 @@ func SetPath(path *string) (isChanged bool) { } // SetDefault : Set default value of the conf -func SetDefault(conf *Yaml) { +func SetDefault(conf *Conf) { if conf.Tmpbin.Name == "" { conf.Tmpbin.Name = "tmpbin/" } @@ -89,7 +89,7 @@ func SetDefault(conf *Yaml) { } // SaveConf : Save the conf.yml -func SaveConf(conf *Yaml) { +func SaveConf(conf *Conf) { fmt.Println("Saving conf.yml ...") yamlChanged, err := yaml.Marshal(conf) if err != nil { diff --git a/utils/file.go b/utils/file.go index e27ca6e..1095e15 100644 --- a/utils/file.go +++ b/utils/file.go @@ -5,6 +5,7 @@ import ( "io" "io/ioutil" "os" + "strings" "time" ) @@ -46,6 +47,34 @@ func CopyFile(src, des string) (written int64, err error) { return io.Copy(desFile, srcFile) } +// MoveAll : Move a file or folder +func MoveAll(file os.FileInfo, src, des string) { + // Check if file already existed in rule dir + if _, err := os.Stat(des); !os.IsNotExist(err) { + fmt.Printf("Error while moving %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B) + fmt.Printf("\t%c[0;31m%s already existed in %s%c[0m\n", 0x1B, file.Name(), strings.Replace(des, file.Name(), "", 1), 0x1B) + } else { + if !file.IsDir() { // file, not folder + _, err := CopyFile(src, des) + if err != nil { + fmt.Printf("Error while moving %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B) + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + } + err = os.Remove(src) + if err != nil { + fmt.Printf("Error while moving %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B) + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + } + } else { // folder + err := os.Rename(src, des) + if err != nil { + fmt.Printf("Error while moving %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B) + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + } + } + } +} + // CheckDir : Check if path is a folder func CheckDir(path string) (isDir bool) { info, err := os.Stat(path) diff --git a/utils/tmpbin.go b/utils/tmpbin.go index 56041e9..41feb9d 100644 --- a/utils/tmpbin.go +++ b/utils/tmpbin.go @@ -7,7 +7,7 @@ import ( ) // Manage folder -func Manage(conf *Yaml) { +func Manage(conf *Conf) { // Make dir 'tmpbin/' _, err := os.Stat(conf.Targetdir + conf.Tmpbin.Name) if err != nil { @@ -47,31 +47,7 @@ func Manage(conf *Yaml) { //} src := conf.Targetdir + file.Name() des := conf.Targetdir + conf.Tmpbin.Name + file.Name() - - // Check if file already existed in tmpbin - if _, err := os.Stat(des); !os.IsNotExist(err) { - fmt.Printf("Error while moving %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B) - fmt.Printf("\t%c[0;31m%s already existed in %s%c[0m\n", 0x1B, file.Name(), conf.Tmpbin.Name, 0x1B) - } else { - if !file.IsDir() { // file, not folder - _, err := CopyFile(src, des) - if err != nil { - fmt.Printf("Error while moving %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B) - fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) - } - err = os.Remove(src) - if err != nil { - fmt.Printf("Error while moving %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B) - fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) - } - } else { // folder - err := os.Rename(src, des) - if err != nil { - fmt.Printf("Error while moving %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B) - fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) - } - } - } + MoveAll(file, src, des) } } else { diff --git a/utils/yaml.go b/utils/yaml.go index 06cd612..fec58dd 100644 --- a/utils/yaml.go +++ b/utils/yaml.go @@ -1,7 +1,7 @@ package utils -// Yaml : a struct for conf.yml -type Yaml struct { +// Conf : a struct for conf.yml +type Conf struct { Verbose bool `yaml:"verbose"` Targetdir string `yaml:"targetdir"` Tmpbin struct { @@ -12,3 +12,24 @@ type Yaml struct { Ignore []string `yaml:"ignore"` } } + +// Rule : a template struct for a rule +type Rule struct { + Enable bool `yaml:"enable"` + Name string `yaml:"name"` + Regex []string `yaml:"regex"` +} + +// Rules : +type Rules struct { + Rule1 Rule `yaml:"rule1"` + Rule2 Rule `yaml:"rule2"` + Rule3 Rule `yaml:"rule3"` + Rule4 Rule `yaml:"rule4"` + Rule5 Rule `yaml:"rule5"` + Rule6 Rule `yaml:"rule6"` + Rule7 Rule `yaml:"rule7"` + Rule8 Rule `yaml:"rule8"` + Rule9 Rule `yaml:"rule9"` + Rule10 Rule `yaml:"rule10"` +} From 9f8c8c9c0259c83d37baf3b9e0d07a20a648978c Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 5 May 2020 17:07:20 +0800 Subject: [PATCH 5/8] feat: better output --- foldest.go | 1 + 1 file changed, 1 insertion(+) diff --git a/foldest.go b/foldest.go index 3134276..2a107a5 100644 --- a/foldest.go +++ b/foldest.go @@ -18,6 +18,7 @@ func main() { } if conf.Tmpbin.Enable { + fmt.Println("Performing tmpbin...") utils.Manage(conf) } else { fmt.Println("tmpbin is disabled, skipping...") From 0551d3e67e7091076963a994e6395ee0db331d3b Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 5 May 2020 17:10:44 +0800 Subject: [PATCH 6/8] Update README --- README.md | 14 +++++++++++++- conf.yml | 4 ++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d121007..2c80280 100644 --- a/README.md +++ b/README.md @@ -16,9 +16,21 @@ tmpbin: treshday: 30 # files not modified for more than this long will be moved into tmpbin, 30 days as default deleteday: 30 # files in tmpbin for more than this long will be deleted, 30 days as default ``` +4. Set your rules in `rules.yml`: (Currently support 10 rules atmost) +```yml +rule1: + enable: true + name: document + regex: + - ".*?.doc" + - ".*?.docx" + - ".*?.pdf" +rule2: +... +``` ## Development progress - [ ] Automatic - [x] Temp trash bin -- [ ] Customize rules +- [x] Customize rules diff --git a/conf.yml b/conf.yml index d731241..be5477a 100644 --- a/conf.yml +++ b/conf.yml @@ -1,7 +1,7 @@ verbose: true -targetdir: +targetdir: D:/Download/ tmpbin: - enable: false + enable: true name: tmpbin/ treshday: 30 deleteday: 30 From cce8f06d70f8851e0cdb17d425e673ac825dc62b Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 5 May 2020 17:32:18 +0800 Subject: [PATCH 7/8] feat: threshday for rules --- rules.yml | 3 ++- utils/classify.go | 26 +++++++++++++++++++------- utils/yaml.go | 1 + 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/rules.yml b/rules.yml index b82afc8..0c0ab61 100644 --- a/rules.yml +++ b/rules.yml @@ -4,4 +4,5 @@ rule1: regex: - ".*?.doc" - ".*?.docx" - - ".*?.pdf" \ No newline at end of file + - ".*?.pdf" + threshday: 30 \ No newline at end of file diff --git a/utils/classify.go b/utils/classify.go index c0a75d0..f2f1a70 100644 --- a/utils/classify.go +++ b/utils/classify.go @@ -7,6 +7,7 @@ import ( "reflect" "regexp" "strings" + "time" "gopkg.in/yaml.v2" ) @@ -90,13 +91,24 @@ func doRule(rule *Rule, path string, isVerbose bool) { re := regexp.MustCompile(pattern) match := re.MatchString(file.Name()) if match { - //if isVerbose { - fmt.Printf("%c[0;34m%s%c[0m matches %c[0;33m%s%c[0m\n", 0x1B, file.Name(), 0x1B, 0x1B, rule.Name, 0x1B) - //} - - src := path + file.Name() - des := path + rule.Name + file.Name() - MoveAll(file, src, des) + modTime, strerr := GetFileModTime(path + rule.Name + file.Name()) + if strerr == "" { + if isVerbose { + fmt.Printf("%c[0;34m%s%c[0m %c[0;32m%s%c[0m\n", 0x1B, file.Name(), 0x1B, 0x1B, modTime, 0x1B) + } + // If file reaches deleteday + if time.Now().Unix()-modTime.Unix() >= int64(rule.Thresh*86400) { + if isVerbose { + fmt.Printf("%c[0;34m%s%c[0m matches %c[0;33m%s%c[0m\n", 0x1B, file.Name(), 0x1B, 0x1B, rule.Name, 0x1B) + } + src := path + file.Name() + des := path + rule.Name + file.Name() + MoveAll(file, src, des) + } + } else { + fmt.Printf("Error while scanning %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B) + fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) + } } } } diff --git a/utils/yaml.go b/utils/yaml.go index fec58dd..8e68e41 100644 --- a/utils/yaml.go +++ b/utils/yaml.go @@ -18,6 +18,7 @@ type Rule struct { Enable bool `yaml:"enable"` Name string `yaml:"name"` Regex []string `yaml:"regex"` + Thresh int `yaml:"threshday"` } // Rules : From 7f7fbef781950d04236f8e99e7c3b9c1d1473862 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 5 May 2020 18:11:27 +0800 Subject: [PATCH 8/8] feat: filesize --- README.md | 5 ++++- rules.yml | 14 +++++++++++++- utils/classify.go | 22 ++++++++++------------ utils/tmpbin.go | 12 +++++++----- utils/yaml.go | 10 ++++++---- 5 files changed, 40 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 2c80280..c3ff061 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ tmpbin: treshday: 30 # files not modified for more than this long will be moved into tmpbin, 30 days as default deleteday: 30 # files in tmpbin for more than this long will be deleted, 30 days as default ``` -4. Set your rules in `rules.yml`: (Currently support 10 rules atmost) +4. Set your rules in `rules.yml`: (Currently support 10 rules utmost) ```yml rule1: enable: true @@ -25,6 +25,9 @@ rule1: - ".*?.doc" - ".*?.docx" - ".*?.pdf" + threshday: 7 + maxsize: # MB + minsize: # MB rule2: ... ``` diff --git a/rules.yml b/rules.yml index 0c0ab61..4d59005 100644 --- a/rules.yml +++ b/rules.yml @@ -5,4 +5,16 @@ rule1: - ".*?.doc" - ".*?.docx" - ".*?.pdf" - threshday: 30 \ No newline at end of file + threshday: 7 + maxsize: + minsize: +rule2: + enable: true + name: films + regex: + - ".*?.mp4" + - ".*?.avi" + - ".*?.flv" + threshday: 7 + maxsize: + minsize: 300 \ No newline at end of file diff --git a/utils/classify.go b/utils/classify.go index f2f1a70..6611b74 100644 --- a/utils/classify.go +++ b/utils/classify.go @@ -77,11 +77,7 @@ func doRule(rule *Rule, path string, isVerbose bool) { } // Moving files to rule dir - for count, file := range dir { - if count > 10 { - break - } - + for _, file := range dir { // jump rule dir if file.Name() == rule.Name || file.IsDir() { continue @@ -91,19 +87,21 @@ func doRule(rule *Rule, path string, isVerbose bool) { re := regexp.MustCompile(pattern) match := re.MatchString(file.Name()) if match { - modTime, strerr := GetFileModTime(path + rule.Name + file.Name()) + modTime, strerr := GetFileModTime(path + file.Name()) if strerr == "" { if isVerbose { - fmt.Printf("%c[0;34m%s%c[0m %c[0;32m%s%c[0m\n", 0x1B, file.Name(), 0x1B, 0x1B, modTime, 0x1B) + fmt.Printf("%c[0;34m%s%c[0m %c[0;32m%s%c[0m %d\n", 0x1B, file.Name(), 0x1B, 0x1B, modTime, 0x1B, file.Size()) } // If file reaches deleteday if time.Now().Unix()-modTime.Unix() >= int64(rule.Thresh*86400) { - if isVerbose { - fmt.Printf("%c[0;34m%s%c[0m matches %c[0;33m%s%c[0m\n", 0x1B, file.Name(), 0x1B, 0x1B, rule.Name, 0x1B) + if (rule.Maxsize <= 0 || file.Size() < (int64)(rule.Maxsize)*1024*1024) && file.Size() > (int64)(rule.Minsize)*1024*1024 { + if isVerbose { + fmt.Printf("%c[0;34m%s%c[0m matches %c[0;33m%s%c[0m\n", 0x1B, file.Name(), 0x1B, 0x1B, rule.Name, 0x1B) + } + src := path + file.Name() + des := path + rule.Name + file.Name() + MoveAll(file, src, des) } - src := path + file.Name() - des := path + rule.Name + file.Name() - MoveAll(file, src, des) } } else { fmt.Printf("Error while scanning %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B) diff --git a/utils/tmpbin.go b/utils/tmpbin.go index 41feb9d..4a25a01 100644 --- a/utils/tmpbin.go +++ b/utils/tmpbin.go @@ -61,10 +61,7 @@ func Manage(conf *Conf) { if dir == nil { return } - for count, file := range dir { - if count > 10 { - break - } + for _, file := range dir { modTime, strerr := GetFileModTime(conf.Targetdir + conf.Tmpbin.Name + file.Name()) if strerr == "" { if conf.Verbose { @@ -77,7 +74,12 @@ func Manage(conf *Conf) { fmt.Printf("Deleting %c[0;34m%s%c[0m\n", 0x1B, file.Name(), 0x1B) //} src := conf.Targetdir + conf.Tmpbin.Name + file.Name() - err = os.Remove(src) + if file.IsDir() { + err = os.RemoveAll(src) + } else { + err = os.Remove(src) + } + if err != nil { fmt.Printf("Error while deleting %c[0;34m%s%c[0m :", 0x1B, file.Name(), 0x1B) fmt.Printf("\t%c[0;31m%s%c[0m\n", 0x1B, err, 0x1B) diff --git a/utils/yaml.go b/utils/yaml.go index 8e68e41..7274dc6 100644 --- a/utils/yaml.go +++ b/utils/yaml.go @@ -15,10 +15,12 @@ type Conf struct { // Rule : a template struct for a rule type Rule struct { - Enable bool `yaml:"enable"` - Name string `yaml:"name"` - Regex []string `yaml:"regex"` - Thresh int `yaml:"threshday"` + Enable bool `yaml:"enable"` + Name string `yaml:"name"` + Regex []string `yaml:"regex"` + Thresh int `yaml:"threshday"` + Maxsize int `yaml:"maxsize"` + Minsize int `yaml:"minsize"` } // Rules :