Browse Source

v1.0) Should do everything we need it to do

Apollo 1 year ago
parent
commit
4516e022e0
3 changed files with 130 additions and 3 deletions
  1. 25 3
      README.md
  2. 3 0
      go.mod
  3. 102 0
      main.go

+ 25 - 3
README.md

@@ -1,5 +1,27 @@
 # undir
 
-Un-Directories any directories that contain 1 file
-+ ignores .info files
-+ ignores .srt files (unless directed with "srt" argument)
+Un-Directories any directories that contain 1 file
++ ignores .info files
++ ignores .srt files (unless directed with "srt" argument)
+
+## Install into PATH
+
+Place the `undir` binary into one of the 2 path locations
+
+> Under Windows the binary will be `undir.exe`
+
+Just close and re-open your Terminal/Command Prompt for this to take effect.
+
+### Linux
+
+- `/home/<user_name>/bin`
+
+> You may need to make that directory
+
+### Windows
+
+> [maketecheasier](https://www.maketecheasier.com/what-is-the-windows-path/) talks about the windows path, and how to modify environment variables.
+
+- `C:\Users\<user_name>\AppData\Local\Undir`
+
+> You may need to make that directory

+ 3 - 0
go.mod

@@ -0,0 +1,3 @@
+module git.red-green.com/david/undir
+
+go 1.21.5

+ 102 - 0
main.go

@@ -0,0 +1,102 @@
+package main
+
+import (
+	"fmt"
+	"io"
+	"os"
+	"path/filepath"
+	"strings"
+)
+
+var (
+	KEEP_SRT bool = false
+)
+
+func scan_dir(dir string) ([]string, error) {
+	var r []string = []string{}
+	ls, err := os.ReadDir(dir)
+	if err != nil {
+		return nil, err
+	}
+	for _, elem := range ls {
+		if elem.IsDir() {
+			rs, err := scan_dir(filepath.Join(dir, elem.Name()))
+			if err != nil {
+				return nil, err
+			}
+			for _, e := range rs {
+				r = append(r, filepath.Join(elem.Name(), e))
+			}
+		} else {
+			if strings.HasSuffix(elem.Name(), ".srt") {
+				if !KEEP_SRT {
+					continue
+				}
+				r = append(r, elem.Name())
+				continue
+			}
+			if strings.HasSuffix(elem.Name(), ".info") {
+				continue
+			}
+			r = append(r, elem.Name())
+		}
+	}
+	return r, nil
+}
+
+func main() {
+	// Keeps
+	if len(os.Args) != 1 {
+		for idx, arg := range os.Args {
+			if idx == 0 { // Skip program name
+				continue
+			}
+			switch strings.ToLower(arg) {
+			case "srt":
+				KEEP_SRT = true
+			}
+		}
+		if KEEP_SRT {
+			fmt.Println("Keeping .srt's")
+		}
+	}
+	actions, err := scan_dir(".")
+	if err != nil {
+		fmt.Println("Err:", err)
+		return
+	}
+	fmt.Println("Found:", len(actions))
+	for _, a := range actions {
+		fmt.Println(a)
+		out, err := os.Create(filepath.Base(a))
+		if err != nil {
+			fmt.Println("Err (Create out):", err)
+			continue
+		}
+		in, err := os.Open(a)
+		if err != nil {
+			out.Close()
+			err1 := os.Remove(filepath.Base(a))
+			if err1 != nil {
+				fmt.Println("Err (Cleanup):", err1)
+			}
+			fmt.Println("Err (Open in):", err)
+			continue
+		}
+		_, err = io.Copy(out, in)
+		if err != nil {
+			in.Close()
+			out.Close()
+			err1 := os.Remove(filepath.Base(a))
+			if err1 != nil {
+				fmt.Println("Err (Cleanup):", err1)
+			}
+			fmt.Println("Err (Copy in -> out):", err)
+			continue
+		}
+		err = os.RemoveAll(filepath.Dir(a))
+		if err != nil {
+			fmt.Println("Err (Remove):", err)
+		}
+	}
+}