GenGo

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 4e95bbe3a52dfd56f3be904fa9b85cf4051d409f
parent 0e1e64a7ed4235a77a7c5db628b1eaa1d00378c7
Author: Tyler Clark <tyler.clark@foxide.xyz>
Date:   Fri,  9 Jan 2026 14:53:34 -0500

Adding ability to recursively copy file/directory structure.

This patch adds the ability to copy the file and directory structure of the
source directory as well as moving the validation function of the existence of
the source directory to the 'copyFile' function. This is to make that function
work recursively.

Diffstat:
Mmain.go | 88+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------
1 file changed, 73 insertions(+), 15 deletions(-)

diff --git a/main.go b/main.go @@ -3,13 +3,10 @@ /*****************************************************************/ /* DONE: Basic functionality for converting markdown/org to html */ -/* TODO: Recursively mirroring dir structure of source files */ +/* DONE: Recursively mirroring dir structure of source files */ /* TODO: RSS generator built in */ /* TODO: Sitemap generator */ /*****************************************************************/ -/* LAYOUT OF CLI: */ -/* gengo ${SRC} ${DEST} ${DOMAIN} [optional args] */ -/*****************************************************************/ /* OPTIONAL ARGUMENTS: */ /* TODO -j: Set how many concurrent jobs can run */ /* PROG -h: Display help */ @@ -22,28 +19,89 @@ package main import ( "fmt" // For formatting some outputs + "io" // Used for copying individual files "os" // File system and arguments "os/exec" // For running OS operations "path" // For removing file extensions ) /* Version Number */ -const version = "0.01: Shoddy" +const version = "0.0.1: Shoddy" -func ValidateDir(directory string) { - _, err := os.Stat(directory) - if os.IsNotExist(err) { - fmt.Fprintln(os.Stderr, "No file or directory found!") - os.Exit(1) +func copyFile(source, dest string) error { + sourceFile, err := os.Open(source) + if err != nil { + return err + } + defer sourceFile.Close() + + destFile, err := os.Create(dest) + if err != nil { + return err + } + defer destFile.Close() + + _, err = io.Copy(destFile, sourceFile) + if err == nil { + sourceInfo, err := os.Stat(source) + if err != nil { + err = os.Chmod(dest, sourceInfo.Mode()) + } } + return err } + /* This is a test function to try to copy a directory */ -func DirSetup(source string, destination string) { - ValidateDir(source) - fsys := os.DirFS(source) - os.CopyFS(destination, fsys) - fmt.Printf("Directory: %v copied\n", source) +func DirSetup(source string, destination string) error { + /* Validate its existence */ + //ValidateDir(source) + + srcInfo, err := os.Stat(source) + if os.IsNotExist(err) { + fmt.Fprintf(os.Stderr, "No such directory \"%v\" found!\n", source) + // Should probably change the exit codes at some point + os.Exit(1) + } + if err != nil { + return err + } + + err = os.MkdirAll(destination, srcInfo.Mode()) + if err != nil { + return err + } + + entries, err := os.ReadDir(source) + if err != nil { + return err + } + + /* Based around solution presented here: */ + /* https://gistlib.com/go/copy-a-directory-in-go */ + /* Might change, as I am not sure how much I love that one */ + for _, entry := range entries { + srcPath := source + "/" + entry.Name() + dstPath := destination + "/" + entry.Name() + + if entry.IsDir() { + err = DirSetup(srcPath, dstPath) + if err != nil { + return err + } + } else { + err = copyFile(srcPath, dstPath) + if err != nil { + return err + } + } + } + + //fsys := os.DirFS(source) + //os.CopyFS(destination, fsys) + //fmt.Printf("Directory: %v copied\n", source) + + return nil } func RemoveExt(filename string) string {