From e61beb3f947da3171aca0eba33c311028949b098 Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Mon, 26 Feb 2024 16:58:39 -0500 Subject: [PATCH] Split into separate packages --- commit.sh | 5 +++-- go.mod | 10 ++++++++++ go.sum | 6 ++++++ src/main.go => main.go | 17 +++++++++-------- src/UNIXglobals.go | 12 ------------ src/WINglobals.go | 12 ------------ src/{ => cli}/ALLglobals.go | 6 +++--- src/{ => cli}/entryList.go | 23 ++++++++++++----------- src/{ => cli}/printInfo.go | 2 +- src/go.mod | 10 ---------- src/offline/ALLglobals.go | 10 ++++++++++ src/offline/UNIXglobals.go | 12 ++++++++++++ src/offline/WINglobals.go | 12 ++++++++++++ 13 files changed, 78 insertions(+), 59 deletions(-) create mode 100644 go.mod create mode 100644 go.sum rename src/main.go => main.go (62%) delete mode 100644 src/UNIXglobals.go delete mode 100644 src/WINglobals.go rename src/{ => cli}/ALLglobals.go (78%) rename src/{ => cli}/entryList.go (91%) rename src/{ => cli}/printInfo.go (99%) delete mode 100644 src/go.mod create mode 100644 src/offline/ALLglobals.go create mode 100644 src/offline/UNIXglobals.go create mode 100644 src/offline/WINglobals.go diff --git a/commit.sh b/commit.sh index 751a82e..729c383 100755 --- a/commit.sh +++ b/commit.sh @@ -1,5 +1,6 @@ #!/bin/sh -gofmt -l -w -s ./src/*.go -git add -f extra src/*.go src/*.mod .gitignore commit.sh LICENSE README.md +gofmt -l -w -s ./src/cli/*.go +gofmt -l -w -s ./src/offline/*.go +git add -f extra src .gitignore commit.sh go.mod go.sum LICENSE main.go README.md git commit -m "$1" git push diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7d598a0 --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module github.com/rwinkhart/MUTN + +go 1.22.0 + +require golang.org/x/crypto v0.20.0 + +require ( + golang.org/x/sys v0.17.0 // indirect + golang.org/x/term v0.17.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..6675135 --- /dev/null +++ b/go.sum @@ -0,0 +1,6 @@ +golang.org/x/crypto v0.20.0 h1:jmAMJJZXr5KiCw05dfYK9QnqaqKLYXijU23lsEdcQqg= +golang.org/x/crypto v0.20.0/go.mod h1:Xwo95rrVNIoSMx9wa1JroENMToLWn3RNVrTBpLHgZPQ= +golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= +golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U= +golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= diff --git a/src/main.go b/main.go similarity index 62% rename from src/main.go rename to main.go index 44c8c81..b485051 100644 --- a/src/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "github.com/rwinkhart/MUTN/src/cli" "os" ) @@ -11,26 +12,26 @@ func main() { if argsCount == 0 { - EntryListGen() + cli.EntryListGen() } else if argsCount == 1 { switch args[0] { case "help", "--help", "-h": - HelpMain() + cli.HelpMain() case "add": - HelpAdd() + cli.HelpAdd() case "edit": - HelpEdit() + cli.HelpEdit() case "copy": - HelpCopy() + cli.HelpCopy() case "gen": - HelpGen() + cli.HelpGen() case "version", "-v": - Version() + cli.Version() default: - HelpMain() + cli.HelpMain() } } diff --git a/src/UNIXglobals.go b/src/UNIXglobals.go deleted file mode 100644 index 60cc48a..0000000 --- a/src/UNIXglobals.go +++ /dev/null @@ -1,12 +0,0 @@ -//go:build !windows - -package main - -// EntryRoot path to libmutton entry directory TODO to be moved to libmutton -var EntryRoot = home + "/.local/share/libmutton" - -// exported constants TODO to be moved to libmutton -const ( - PathSeparator = "/" - Windows = false -) diff --git a/src/WINglobals.go b/src/WINglobals.go deleted file mode 100644 index a241076..0000000 --- a/src/WINglobals.go +++ /dev/null @@ -1,12 +0,0 @@ -//go:build windows - -package main - -// EntryRoot path to libmutton entry directory TODO to be moved to libmutton -var EntryRoot = home + "\\AppData\\Local\\libmutton\\entries" - -// exported constants TODO to be moved to libmutton -const ( - PathSeparator = "\\" - Windows = true -) diff --git a/src/ALLglobals.go b/src/cli/ALLglobals.go similarity index 78% rename from src/ALLglobals.go rename to src/cli/ALLglobals.go index 42a2a42..9c453c9 100644 --- a/src/ALLglobals.go +++ b/src/cli/ALLglobals.go @@ -1,14 +1,14 @@ -package main +package cli import ( + "github.com/rwinkhart/MUTN/src/offline" termy "golang.org/x/crypto/ssh/terminal" "os" ) // global variables used across multiple files var ( - home, _ = os.UserHomeDir() - rootLength = len(EntryRoot) + rootLength = len(offline.EntryRoot) width, _, _ = termy.GetSize(int(os.Stdout.Fd())) ) diff --git a/src/entryList.go b/src/cli/entryList.go similarity index 91% rename from src/entryList.go rename to src/cli/entryList.go index ae5eaeb..6d74e3b 100644 --- a/src/entryList.go +++ b/src/cli/entryList.go @@ -1,7 +1,8 @@ -package main +package cli import ( "fmt" + "github.com/rwinkhart/MUTN/src/offline" "io/fs" "os" "path/filepath" @@ -20,7 +21,7 @@ func indentSubtractor(skippedDirList []bool, dirList []string, currentDirIndex i var trimmedDirectory = dirList[currentDirIndex] for i, skipped := range skippedDirList[:currentDirIndex] { // checks each skipped directory to determine if it is a parent to the current directory - if strings.HasPrefix(trimmedDirectory, dirList[i]+PathSeparator) { // if the current directory is the child of this iteration's directory... + if strings.HasPrefix(trimmedDirectory, dirList[i]+offline.PathSeparator) { // if the current directory is the child of this iteration's directory... if skipped { // ...and this iteration's directory was skipped... subtractor++ // increment the subtractor to indicate that the visual indentation should be reduced } else { @@ -81,14 +82,14 @@ func EntryListGen() { var dirList []string // walk entry directory - _ = filepath.WalkDir(EntryRoot, + _ = filepath.WalkDir(offline.EntryRoot, func(fullPath string, entry fs.DirEntry, err error) error { // check for errors encountered while walking directory if err != nil { // create EntryRoot if the error is the result of it not existing on the system if os.IsNotExist(err) { - _ = os.Mkdir(EntryRoot, 0700) + _ = os.Mkdir(offline.EntryRoot, 0700) dirList = append(dirList, "") } else { // otherwise, print the source of the error @@ -134,11 +135,11 @@ func EntryListGen() { } // determine directory's indentation multiplier based on PathSeparator occurrences - indent = strings.Count(directory, PathSeparator) - 1 // subtract 1 to avoid indenting root-level directories + indent = strings.Count(directory, offline.PathSeparator) - 1 // subtract 1 to avoid indenting root-level directories // check if next directory is within the current one if dirListLength > i+1 { - if nextDir := dirList[i+1]; directory == nextDir[:strings.LastIndex(nextDir, PathSeparator)] { + if nextDir := dirList[i+1]; directory == nextDir[:strings.LastIndex(nextDir, offline.PathSeparator)] { containsSubdirectory = true } else { containsSubdirectory = false @@ -152,17 +153,17 @@ func EntryListGen() { for _, file := range fileList { // print the current file if it belongs in the current directory - otherwise, break the loop and move on to the next directory - if lastSlash := strings.LastIndex(file, PathSeparator) + 1; file[:lastSlash-1] == directory { + if lastSlash := strings.LastIndex(file, offline.PathSeparator) + 1; file[:lastSlash-1] == directory { // print directory header if this is the first run of the loop if !containsFiles { containsFiles = true skippedDirList[i] = false // the directory header is being printed, indicate that it is not being skipped indent, vanityDirectory = indentSubtractor(skippedDirList, dirList, i, indent) // calculate the final indentation multiplier - if !Windows { // for consistency, format directories with UNIX-style path separators on all platforms + if !offline.Windows { // for consistency, format directories with UNIX-style path separators on all platforms fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/"+ansiReset+"\n", vanityDirectory) } else { - fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/"+ansiReset+"\n", strings.ReplaceAll(vanityDirectory, PathSeparator, "/")) + fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/"+ansiReset+"\n", strings.ReplaceAll(vanityDirectory, offline.PathSeparator, "/")) } } @@ -175,10 +176,10 @@ func EntryListGen() { if dirListLength > 1 { // and directories besides the root-level exist... display directory header and empty directory warning skippedDirList[i] = false // the directory header is being printed, indicate that it is not being skipped indent, vanityDirectory = indentSubtractor(skippedDirList, dirList, i, indent) // calculate the final indentation multiplier - if !Windows { // for consistency, format directories with UNIX-style path separators on all platforms + if !offline.Windows { // for consistency, format directories with UNIX-style path separators on all platforms fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/"+ansiReset+"\n", vanityDirectory) } else { - fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/"+ansiReset+"\n", strings.ReplaceAll(vanityDirectory, PathSeparator, "/")) + fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/"+ansiReset+"\n", strings.ReplaceAll(vanityDirectory, offline.PathSeparator, "/")) } fmt.Print(strings.Repeat(" ", indent*2) + "\033[38;5;11m-empty directory-" + ansiReset) } else { // warn if the only thing that exists is the root-level directory diff --git a/src/printInfo.go b/src/cli/printInfo.go similarity index 99% rename from src/printInfo.go rename to src/cli/printInfo.go index 67f3912..dc4dc4b 100644 --- a/src/printInfo.go +++ b/src/cli/printInfo.go @@ -1,4 +1,4 @@ -package main +package cli import "fmt" diff --git a/src/go.mod b/src/go.mod deleted file mode 100644 index a7448c6..0000000 --- a/src/go.mod +++ /dev/null @@ -1,10 +0,0 @@ -module main - -go 1.21.6 - -require golang.org/x/crypto v0.18.0 - -require ( - golang.org/x/sys v0.16.0 // indirect - golang.org/x/term v0.16.0 // indirect -) diff --git a/src/offline/ALLglobals.go b/src/offline/ALLglobals.go new file mode 100644 index 0000000..6422791 --- /dev/null +++ b/src/offline/ALLglobals.go @@ -0,0 +1,10 @@ +package offline + +import ( + "os" +) + +// global variables used across multiple files +var ( + home, _ = os.UserHomeDir() +) diff --git a/src/offline/UNIXglobals.go b/src/offline/UNIXglobals.go new file mode 100644 index 0000000..db6a448 --- /dev/null +++ b/src/offline/UNIXglobals.go @@ -0,0 +1,12 @@ +//go:build !windows + +package offline + +// EntryRoot path to libmutton entry directory +var EntryRoot = home + "/.local/share/libmutton" + +// exported constants +const ( + PathSeparator = "/" + Windows = false +) diff --git a/src/offline/WINglobals.go b/src/offline/WINglobals.go new file mode 100644 index 0000000..cfd9357 --- /dev/null +++ b/src/offline/WINglobals.go @@ -0,0 +1,12 @@ +//go:build windows + +package offline + +// EntryRoot path to libmutton entry directory +var EntryRoot = home + "\\AppData\\Local\\libmutton\\entries" + +// exported constants +const ( + PathSeparator = "\\" + Windows = true +)