-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdstort.go
102 lines (83 loc) · 2.53 KB
/
dstort.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// dstort - "organize" a directory randomly
// Copyright (C) 2024 Tomás Gutiérrez L. (0x00)
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"flag"
"fmt"
"math/rand"
"os"
"path/filepath"
)
const (
version = "0.1.0"
license_text = `Copyright (C) 2024 Tomás Gutiérrez L. (0x00)
License GPLv3: GNU GPL version 3 <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
This program comes with ABSOLUTELY NO WARRANTY.
`
)
var directories []string
var files []string
func list_files_and_directories(dir string) {
file_dir, err := os.Open(dir)
if err != nil {
fmt.Printf("Error opening directory %s\n", dir)
return
}
defer file_dir.Close()
files_dir, err := file_dir.Readdir(-1)
if err != nil {
fmt.Printf("Error reading directory %s\n", dir)
return
}
for _, file := range files_dir {
if file.IsDir() {
directories = append(directories, filepath.Join(dir, file.Name()))
list_files_and_directories(filepath.Join(dir, file.Name()))
} else {
files = append(files, filepath.Join(dir, file.Name()))
}
}
}
func main() {
version_flag := flag.Bool("v", false, "Print the version number.")
help_flag := flag.Bool("h", false, "Print this help message.")
flag.Parse()
if *version_flag {
fmt.Printf("dstort %s\n%s", version, license_text)
os.Exit(0)
}
if *help_flag {
flag.PrintDefaults()
os.Exit(0)
}
arguments := flag.Args()
if len(arguments) != 1 {
fmt.Println("Only 1 directory at a time")
os.Exit(1)
}
dir := arguments[0]
directories = append(directories, dir)
list_files_and_directories(dir)
for _, file := range files {
filename := filepath.Base(file)
os.Rename(file, directories[rand.Intn(len(directories))]+"/"+filename)
}
for i := 1; i < len(directories); i++ {
dst := directories[rand.Intn(len(directories))] + "/" + filepath.Base(directories[i])
err := os.Rename(directories[i], dst)
if err == nil {
directories[i] = dst
}
}
}