Skip to content

Commit 9b16489

Browse files
committed
Initial commit
1 parent d5109ec commit 9b16489

File tree

4 files changed

+114
-1
lines changed

4 files changed

+114
-1
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2018 Clint Tyler
3+
Copyright (c) 2018 Clint Tyler <ctyler@cdtwebsolutions.com>
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Kickrocks
2+
=========
3+
4+
Kickrocks is a program written in Golang that rotates unused files into last_week/ and last_month/ directories.
5+

kickrocks

2.02 MB
Binary file not shown.

kickrocks.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"io/ioutil"
7+
"log"
8+
"time"
9+
"os"
10+
"path/filepath"
11+
)
12+
13+
func main() {
14+
// Get the working dir
15+
pwd, err := filepath.Abs(filepath.Dir(os.Args[0]))
16+
if err != nil {
17+
log.Fatal(err)
18+
}
19+
20+
// Allow working directory override; default to parent working directory
21+
pwdInput := flag.String("dir", pwd, "a path to rotate")
22+
23+
// Flags to skip dotfiles and sub-dirs
24+
skipDotenvFlag := flag.Bool("skipdotfiles", false, "a flag to prevent dotfiles (.env) from being rotated")
25+
skipDirsFlag := flag.Bool("skipsubdirs", false, "a flag to prevent sub-directories from being rotated")
26+
27+
// Flag to show output
28+
verboseFlag := flag.Bool("verbose", false, "a flag to show output")
29+
30+
flag.Parse()
31+
32+
pwd = *pwdInput
33+
var skipDotenvs bool = *skipDotenvFlag
34+
var skipDirs bool = *skipDirsFlag
35+
var verbose bool = *verboseFlag
36+
37+
// Define rotate paths
38+
rotatePaths := make(map[string]string)
39+
rotatePaths["last_month"] = filepath.Join(pwd, "last_month")
40+
rotatePaths["last_week"] = filepath.Join(pwd, "last_week")
41+
42+
rotate := func(src string, dest string, after int64) {
43+
// Get files in src directory
44+
files, err := ioutil.ReadDir(src)
45+
if err != nil {
46+
log.Fatal(err)
47+
}
48+
for _, f := range files {
49+
var name string = f.Name()
50+
if name[0] == '.' && skipDotenvs {
51+
continue
52+
} else if f.IsDir() && skipDirs {
53+
continue
54+
}
55+
56+
// Skip protected directories
57+
var fp bool = false
58+
for name, path := range rotatePaths {
59+
// Only skip if in parent working directory
60+
if (name == f.Name() && pwd == src) {
61+
if (verbose) {
62+
fmt.Println(path, "is a protected directory; Skipping")
63+
}
64+
fp = true
65+
break
66+
}
67+
}
68+
if fp {
69+
continue
70+
}
71+
72+
// Rotate top-level resources
73+
var hours = int64(time.Since(f.ModTime()).Hours())
74+
var days = int64(hours / 24)
75+
76+
if days >= after {
77+
if (verbose) {
78+
fmt.Println(filepath.Join(src, f.Name()), "moved to", filepath.Join(dest, f.Name()))
79+
}
80+
os.Rename(filepath.Join(src, f.Name()), filepath.Join(dest, f.Name()))
81+
}
82+
switch {
83+
case days >= 30:
84+
if (verbose) {
85+
fmt.Println(f.Name(), "to be moved to last_month dir");
86+
}
87+
break
88+
case days >= 7:
89+
if (verbose) {
90+
fmt.Println(f.Name(), "to be moved to last_week dir");
91+
}
92+
break;
93+
}
94+
}
95+
return
96+
}
97+
98+
// Make sure that rotate paths actual exist
99+
for _, rotatePath := range rotatePaths {
100+
if _, err := os.Stat(rotatePath); os.IsNotExist(err) {
101+
os.Mkdir(rotatePath, 0775)
102+
}
103+
}
104+
105+
// Rotate files in last_week that are > 14 days old to the last_month directory
106+
rotate(rotatePaths["last_week"], rotatePaths["last_month"], 14);
107+
rotate(pwd, rotatePaths["last_week"], 7);
108+
}

0 commit comments

Comments
 (0)