Add CLI support.

This commit is contained in:
Ada Werefox 2025-04-10 13:22:08 -07:00
parent bef667d9fd
commit f476f33bb2

View file

@ -1,26 +1,46 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"log" "log"
"os" "os"
"path/filepath"
) )
func main() { func main() {
// TODO: Replace with cli arg parsing. // Add directory specification flag with default value
// Directory to be sorted sort_dir := flag.String("d", "media/", "Specify the directory of files to be sorted.")
media_dir := "media/" flag.Parse()
// A destination directory of sorted files
// Make sure the input directory is valid
_, err := os.Stat(*sort_dir)
if err != nil {
log.Fatalf("A valid directory was not given: %v", err)
}
// Get the absolute path of the directory to be sorted
media_dir_path, err := filepath.Abs(*sort_dir)
if err != nil {
log.Fatalf("Couldn't grab absolute filepath: %v", err)
}
// Make name for the destination directory of sorted files
media_dir := filepath.Base(media_dir_path)
sorted_media_dir := fmt.Sprintf("sorted_%s", media_dir) sorted_media_dir := fmt.Sprintf("sorted_%s", media_dir)
sorted_media_dir_path := fmt.Sprintf("%s/%s", filepath.Dir(media_dir_path), sorted_media_dir)
// Debug output of the filepaths
log.Default().Println(fmt.Sprintf("%s | %s | %s | %s", media_dir_path, media_dir, sorted_media_dir_path, sorted_media_dir))
// Get a list of the files in the directory to be sorted // Get a list of the files in the directory to be sorted
files, err := os.ReadDir(media_dir) files, err := os.ReadDir(media_dir_path)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
// Make the destination directory // Make the destination directory
err = os.Mkdir(sorted_media_dir, 0755) err = os.Mkdir(sorted_media_dir_path, 0755)
if err != nil && !os.IsExist(err) { if err != nil && !os.IsExist(err) {
log.Fatalf("Couldn't create sorted files directroy: %v", err) log.Fatalf("Couldn't create sorted files directroy: %v", err)
} }
@ -28,14 +48,14 @@ func main() {
// Iterate through the list of files // Iterate through the list of files
for _, file := range files { for _, file := range files {
// Don't try to sort directories // Don't try to sort directories
if file.IsDir() { if file.IsDir() || file.Name() == ".gitkeep" {
continue continue
} }
// The name string of the file we're looking at // The name string of the file we're looking at
current_file_name := file.Name() current_file_name := file.Name()
// Grab file info // Grab file info
current_file, err := os.Stat(fmt.Sprintf("%s%s", media_dir, current_file_name)) current_file, err := os.Stat(fmt.Sprintf("%s/%s", media_dir_path, current_file_name))
if err != nil { if err != nil {
log.Fatalf("Couldn't stat current file: %v", err) log.Fatalf("Couldn't stat current file: %v", err)
} }
@ -45,13 +65,13 @@ func main() {
current_file_month := current_file.ModTime().Month() current_file_month := current_file.ModTime().Month()
// Make directories for the sorted files to go into // Make directories for the sorted files to go into
err = os.Mkdir(fmt.Sprintf("%s%d", sorted_media_dir, current_file_year), 0755) err = os.Mkdir(fmt.Sprintf("%s/%d", sorted_media_dir_path, current_file_year), 0755)
if err != nil && !os.IsExist(err) { if err != nil && !os.IsExist(err) {
log.Fatalf("Couldn't create new directory: %v", err) log.Fatalf("Couldn't create new directory: %v", err)
} }
new_directory_path := fmt.Sprintf("%d/%02d/", current_file_year, current_file_month) new_directory_path := fmt.Sprintf("%d/%02d", current_file_year, current_file_month)
err = os.Mkdir(fmt.Sprintf("%s%s", sorted_media_dir, new_directory_path), 0755) err = os.Mkdir(fmt.Sprintf("%s/%s", sorted_media_dir_path, new_directory_path), 0755)
if err != nil && !os.IsExist(err) { if err != nil && !os.IsExist(err) {
log.Fatalf("Couldn't create new directory: %v", err) log.Fatalf("Couldn't create new directory: %v", err)
} }
@ -60,8 +80,8 @@ func main() {
log.Default().Println(fmt.Sprintf("%d/%02d | %s", current_file_year, current_file_month, current_file_name)) log.Default().Println(fmt.Sprintf("%d/%02d | %s", current_file_year, current_file_month, current_file_name))
// Set the current file path and the destination file path // Set the current file path and the destination file path
full_current_file_path := fmt.Sprintf("%s%s", media_dir, current_file_name) full_current_file_path := fmt.Sprintf("%s/%s", media_dir_path, current_file_name)
full_new_file_path := fmt.Sprintf("%s%s%s", sorted_media_dir, new_directory_path, current_file_name) full_new_file_path := fmt.Sprintf("%s/%s/%s", sorted_media_dir_path, new_directory_path, current_file_name)
// Move the file // Move the file
err = os.Rename(full_current_file_path, full_new_file_path) err = os.Rename(full_current_file_path, full_new_file_path)