From 23d6ddbdc84788bc419c6ff7a5ac827db439af76 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Mon, 23 Sep 2024 22:05:30 +0100 Subject: [PATCH] fix: update prompt string plus some refactoring - Updated the prompt string. - Move the main package to cmd/pokecli. - Added a loop function in the main repl function. --- .gitignore | 2 +- cmd/pokecli/main.go | 5 ++++ main.go => cmd/pokecli/repl.go | 51 +++++++++++++++------------------- cmd/pokecli/summaries.go | 13 +++++++++ magefiles/mage.go | 4 +-- 5 files changed, 43 insertions(+), 32 deletions(-) create mode 100644 cmd/pokecli/main.go rename main.go => cmd/pokecli/repl.go (83%) create mode 100644 cmd/pokecli/summaries.go diff --git a/.gitignore b/.gitignore index 5a4076e..219ab2c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -pokecli +/pokecli diff --git a/cmd/pokecli/main.go b/cmd/pokecli/main.go new file mode 100644 index 0000000..3a98be8 --- /dev/null +++ b/cmd/pokecli/main.go @@ -0,0 +1,5 @@ +package main + +func main() { + repl() +} diff --git a/main.go b/cmd/pokecli/repl.go similarity index 83% rename from main.go rename to cmd/pokecli/repl.go index 5a615e2..dfe88ec 100644 --- a/main.go +++ b/cmd/pokecli/repl.go @@ -3,7 +3,6 @@ package main import ( "bufio" "fmt" - "maps" "os" "strings" "time" @@ -18,11 +17,7 @@ type command struct { callback commands.CommandFunc } -func main() { - run() -} - -func run() { +func repl() { var ( cacheCleanupInterval = 30 * time.Minute httpTimeout = 10 * time.Second @@ -80,52 +75,46 @@ func run() { callback: commands.HelpFunc(summaries), } - fmt.Printf("\nWelcome to the Pokedex!\n") - fmt.Print("\npokedex > ") - scanner := bufio.NewScanner(os.Stdin) - for scanner.Scan() { + loopFunc := func() { + defer printPrompt() + input := scanner.Text() - command, args := parseArgs(input) + command, args := parseInput(input) cmd, ok := commandMap[command] if !ok { fmt.Println("ERROR: Unrecognised command.") - fmt.Print("\npokedex > ") - - continue + return } if cmd.callback == nil { fmt.Println("ERROR: This command is defined but does not have a callback function.") - fmt.Print("\npokedex > ") - - continue + return } if err := commandMap[command].callback(args); err != nil { fmt.Printf("ERROR: %v.\n", err) + + return } + } - fmt.Print("pokedex > ") + fmt.Printf("\nWelcome to the Pokemon world!\n") + printPrompt() + + for scanner.Scan() { + loopFunc() } } -func summaryMap(commandMap map[string]command) map[string]string { - summaries := make(map[string]string) - - for key, value := range maps.All(commandMap) { - summaries[key] = value.description - } - - return summaries -} - -func parseArgs(input string) (string, []string) { +func parseInput(input string) (string, []string) { + input = strings.TrimSpace(input) + input = strings.ToLower(input) split := strings.Split(input, " ") if len(split) == 0 { @@ -138,3 +127,7 @@ func parseArgs(input string) (string, []string) { return split[0], split[1:] } + +func printPrompt() { + fmt.Print("pokecli > ") +} diff --git a/cmd/pokecli/summaries.go b/cmd/pokecli/summaries.go new file mode 100644 index 0000000..16e1969 --- /dev/null +++ b/cmd/pokecli/summaries.go @@ -0,0 +1,13 @@ +package main + +import "maps" + +func summaryMap(commandMap map[string]command) map[string]string { + summaries := make(map[string]string) + + for key, value := range maps.All(commandMap) { + summaries[key] = value.description + } + + return summaries +} diff --git a/magefiles/mage.go b/magefiles/mage.go index 9413512..dd7e895 100644 --- a/magefiles/mage.go +++ b/magefiles/mage.go @@ -56,7 +56,7 @@ func Lint() error { // To rebuild packages that are already up-to-date set POKECLI_BUILD_REBUILD_ALL=1 // To enable verbose mode set POKECLI_BUILD_VERBOSE=1 func Build() error { - main := "main.go" + path := "./cmd/" + app flags := ldflags() build := sh.RunCmd("go", "build") args := []string{"-ldflags=" + flags, "-o", binary} @@ -69,7 +69,7 @@ func Build() error { args = append(args, "-v") } - args = append(args, main) + args = append(args, path) return build(args...) }