From fa6d5bc78a18ef3915568434fc5b753f87560d98 Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Tue, 27 Feb 2024 11:48:02 -0500 Subject: [PATCH] Implement ReadConfig for reading ~/.config/libmutton/libmutton.ini --- go.mod | 6 +- go.sum | 10 +++ main.go | 4 ++ src/cli/{ALLglobals.go => 1globals.go} | 1 - src/cli/entryList.go | 16 ++--- src/cli/printInfo.go | 61 ++++++++++--------- src/offline/{ALLglobals.go => 1globals.go} | 6 ++ .../{UNIXglobals.go => 2UNIXglobals.go} | 0 src/offline/{WINglobals.go => 3WINglobals.go} | 0 src/offline/configParser.go | 57 +++++++++++++++++ 10 files changed, 122 insertions(+), 39 deletions(-) rename src/cli/{ALLglobals.go => 1globals.go} (92%) rename src/offline/{ALLglobals.go => 1globals.go} (51%) rename src/offline/{UNIXglobals.go => 2UNIXglobals.go} (100%) rename src/offline/{WINglobals.go => 3WINglobals.go} (100%) create mode 100644 src/offline/configParser.go diff --git a/go.mod b/go.mod index 7d598a0..d75b99a 100644 --- a/go.mod +++ b/go.mod @@ -2,9 +2,13 @@ module github.com/rwinkhart/MUTN go 1.22.0 -require golang.org/x/crypto v0.20.0 +require ( + golang.org/x/crypto v0.20.0 + gopkg.in/ini.v1 v1.67.0 +) require ( + github.com/stretchr/testify v1.8.4 // indirect golang.org/x/sys v0.17.0 // indirect golang.org/x/term v0.17.0 // indirect ) diff --git a/go.sum b/go.sum index 6675135..11eb3c9 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,16 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= 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= +gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= +gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index b485051..d6d6221 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "github.com/rwinkhart/MUTN/src/cli" + "github.com/rwinkhart/MUTN/src/offline" "os" ) @@ -34,5 +35,8 @@ func main() { cli.HelpMain() } + } else if argsCount > 1 { + // load config (libmutton.ini) + offline.ReadConfig() } } diff --git a/src/cli/ALLglobals.go b/src/cli/1globals.go similarity index 92% rename from src/cli/ALLglobals.go rename to src/cli/1globals.go index 9c453c9..55b127a 100644 --- a/src/cli/ALLglobals.go +++ b/src/cli/1globals.go @@ -14,7 +14,6 @@ var ( // global constants used across multiple files const ( - ansiReset = "\033[0m" ansiBold = "\033[1m" ansiBlackOnWhite = "\033[38;5;0;48;5;15m" ) diff --git a/src/cli/entryList.go b/src/cli/entryList.go index 6d74e3b..0310d01 100644 --- a/src/cli/entryList.go +++ b/src/cli/entryList.go @@ -68,14 +68,14 @@ func printFileEntry(entry string, lastSlash int, charCounter int, colorAlternato } // print fileEntryName to screen - fmt.Printf("%s%s%s ", colorCode, fileEntryName, ansiReset) + fmt.Printf("%s%s%s ", colorCode, fileEntryName, offline.AnsiReset) return charCounter, colorAlternator } // EntryListGen generates and displays full libmutton entry list func EntryListGen() { - fmt.Print("\n" + ansiBlackOnWhite + "libmutton entries:" + ansiReset) + fmt.Print("\n" + ansiBlackOnWhite + "libmutton entries:" + offline.AnsiReset) // define file/directory containing slices so that they may be accessed by the anonymous WalkDir function var fileList []string @@ -93,7 +93,7 @@ func EntryListGen() { dirList = append(dirList, "") } else { // otherwise, print the source of the error - fmt.Print("\n\n\033[38;5;9mAn unexpected error occurred while generating the entry list: " + err.Error() + ansiReset) + fmt.Print("\n\n" + offline.AnsiError + "An unexpected error occurred while generating the entry list: " + err.Error() + offline.AnsiReset) } // quit walking EntryRoot and return nil to allow the program to continue return nil @@ -161,9 +161,9 @@ func EntryListGen() { 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 !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) + fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/"+offline.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, offline.PathSeparator, "/")) + fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/"+offline.AnsiReset+"\n", strings.ReplaceAll(vanityDirectory, offline.PathSeparator, "/")) } } @@ -177,11 +177,11 @@ func EntryListGen() { 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 !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) + fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/"+offline.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, offline.PathSeparator, "/")) + fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/"+offline.AnsiReset+"\n", strings.ReplaceAll(vanityDirectory, offline.PathSeparator, "/")) } - fmt.Print(strings.Repeat(" ", indent*2) + "\033[38;5;11m-empty directory-" + ansiReset) + fmt.Print(strings.Repeat(" ", indent*2) + "\033[38;5;11m-empty directory-" + offline.AnsiReset) } else { // warn if the only thing that exists is the root-level directory fmt.Print("\n\nNothing's here! For help creating your first entry, run \"mutn help\".") } diff --git a/src/cli/printInfo.go b/src/cli/printInfo.go index dc4dc4b..d6b8922 100644 --- a/src/cli/printInfo.go +++ b/src/cli/printInfo.go @@ -1,6 +1,9 @@ package cli -import "fmt" +import ( + "fmt" + "github.com/rwinkhart/MUTN/src/offline" +) // global constants used only in this file const ( @@ -9,13 +12,13 @@ const ( ) func HelpMain() { - fmt.Print(ansiBold + "\nMUTN | Copyright (c) 2024 Randall Winkhart\n" + ansiReset + ` + fmt.Print(ansiBold + "\nMUTN | Copyright (c) 2024 Randall Winkhart\n" + offline.AnsiReset + ` This software exists under the MIT license; you may redistribute it under certain conditions. This program comes with absolutely no warranty; type "mutn version" for details. -` + ansiBold + "Usage:" + ansiReset + ` mutn [/ [argument] [option]] | [argument] +` + ansiBold + "Usage:" + offline.AnsiReset + ` mutn [/ [argument] [option]] | [argument] -` + ansiBold + "Arguments:" + ansiReset + ` +` + ansiBold + "Arguments:" + offline.AnsiReset + ` help/--help/-h Bring up this menu version/-v Display version and license information init Set up MUTN @@ -27,7 +30,7 @@ This program comes with absolutely no warranty; type "mutn version" for details. shear Delete an existing entry sync Manually sync the entry directory -` + ansiBold + "Options:" + ansiReset + ` +` + ansiBold + "Options:" + offline.AnsiReset + ` add: password/-p Add a password entry note/-n Add a note entry @@ -46,15 +49,15 @@ This program comes with absolutely no warranty; type "mutn version" for details. gen: update/-u Generate a password for an existing entry -` + ansiBold + "Tip 1:" + ansiReset + ` You can quickly read an entry with "mutn /" -` + ansiBold + "Tip 2:" + ansiReset + ` Type "mutn" (no arguments/options) to view a list of saved entries -` + ansiBold + "Tip 3:" + ansiReset + " Provide \"add\", \"edit\", \"copy\", or \"gen\" as the only argument to receive more specific help\n\n") +` + ansiBold + "Tip 1:" + offline.AnsiReset + ` You can quickly read an entry with "mutn /" +` + ansiBold + "Tip 2:" + offline.AnsiReset + ` Type "mutn" (no arguments/options) to view a list of saved entries +` + ansiBold + "Tip 3:" + offline.AnsiReset + " Provide \"add\", \"edit\", \"copy\", or \"gen\" as the only argument to receive more specific help\n\n") } func HelpAdd() { - fmt.Print(ansiBold + "\nUsage:" + ansiReset + ` mutn / add