generated from templates/go-generic
Compare commits
3 commits
c098a4c8ce
...
ab880bbbbb
Author | SHA1 | Date | |
---|---|---|---|
ab880bbbbb | |||
f7359402ef | |||
23d6ddbdc8 |
7 changed files with 93 additions and 36 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1 +1 @@
|
||||||
pokecli
|
/pokecli
|
||||||
|
|
37
README.md
37
README.md
|
@ -1,3 +1,38 @@
|
||||||
# pokecli
|
# pokecli
|
||||||
|
|
||||||
A simple CLI application for exploring the Pokémon world.
|
## Overview
|
||||||
|
|
||||||
|
**pokecli** is a simple CLI application that uses the [PokéAPI](https://pokeapi.co/) for exploring the Pokémon world and capturing Pokémon.
|
||||||
|
|
||||||
|
### Repository mirrors
|
||||||
|
|
||||||
|
- **Code Flow:** https://codeflow.dananglin.me.uk/apollo/pokecli
|
||||||
|
- **GitHub:** https://github.com/dananglin/pokecli
|
||||||
|
|
||||||
|
## Requirements
|
||||||
|
|
||||||
|
- **Go:** A minimum version of Go 1.23.1 is required for building the pokecli. Please go [here](https://go.dev/dl/) to download the latest version.
|
||||||
|
|
||||||
|
## Build the application
|
||||||
|
|
||||||
|
Clone this repository to your local machine.
|
||||||
|
|
||||||
|
```
|
||||||
|
git clone https://github.com/dananglin/pokecli.git
|
||||||
|
```
|
||||||
|
|
||||||
|
Build the application.
|
||||||
|
|
||||||
|
- Build with go.
|
||||||
|
```
|
||||||
|
go build -o pokecli .
|
||||||
|
```
|
||||||
|
|
||||||
|
- Or build with [mage](https://magefile.org/) if you have it installed.
|
||||||
|
```
|
||||||
|
mage clean build
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
TBC
|
||||||
|
|
5
cmd/pokecli/main.go
Normal file
5
cmd/pokecli/main.go
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
repl()
|
||||||
|
}
|
|
@ -3,7 +3,6 @@ package main
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"maps"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -18,11 +17,7 @@ type command struct {
|
||||||
callback commands.CommandFunc
|
callback commands.CommandFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func repl() {
|
||||||
run()
|
|
||||||
}
|
|
||||||
|
|
||||||
func run() {
|
|
||||||
var (
|
var (
|
||||||
cacheCleanupInterval = 30 * time.Minute
|
cacheCleanupInterval = 30 * time.Minute
|
||||||
httpTimeout = 10 * time.Second
|
httpTimeout = 10 * time.Second
|
||||||
|
@ -80,52 +75,46 @@ func run() {
|
||||||
callback: commands.HelpFunc(summaries),
|
callback: commands.HelpFunc(summaries),
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("\nWelcome to the Pokedex!\n")
|
|
||||||
fmt.Print("\npokedex > ")
|
|
||||||
|
|
||||||
scanner := bufio.NewScanner(os.Stdin)
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
|
|
||||||
for scanner.Scan() {
|
loopFunc := func() {
|
||||||
|
defer printPrompt()
|
||||||
|
|
||||||
input := scanner.Text()
|
input := scanner.Text()
|
||||||
|
|
||||||
command, args := parseArgs(input)
|
command, args := parseInput(input)
|
||||||
|
|
||||||
cmd, ok := commandMap[command]
|
cmd, ok := commandMap[command]
|
||||||
if !ok {
|
if !ok {
|
||||||
fmt.Println("ERROR: Unrecognised command.")
|
fmt.Println("ERROR: Unrecognised command.")
|
||||||
|
|
||||||
fmt.Print("\npokedex > ")
|
return
|
||||||
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmd.callback == nil {
|
if cmd.callback == nil {
|
||||||
fmt.Println("ERROR: This command is defined but does not have a callback function.")
|
fmt.Println("ERROR: This command is defined but does not have a callback function.")
|
||||||
|
|
||||||
fmt.Print("\npokedex > ")
|
return
|
||||||
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := commandMap[command].callback(args); err != nil {
|
if err := commandMap[command].callback(args); err != nil {
|
||||||
fmt.Printf("ERROR: %v.\n", err)
|
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 {
|
func parseInput(input string) (string, []string) {
|
||||||
summaries := make(map[string]string)
|
input = strings.TrimSpace(input)
|
||||||
|
input = strings.ToLower(input)
|
||||||
for key, value := range maps.All(commandMap) {
|
|
||||||
summaries[key] = value.description
|
|
||||||
}
|
|
||||||
|
|
||||||
return summaries
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseArgs(input string) (string, []string) {
|
|
||||||
split := strings.Split(input, " ")
|
split := strings.Split(input, " ")
|
||||||
|
|
||||||
if len(split) == 0 {
|
if len(split) == 0 {
|
||||||
|
@ -138,3 +127,7 @@ func parseArgs(input string) (string, []string) {
|
||||||
|
|
||||||
return split[0], split[1:]
|
return split[0], split[1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func printPrompt() {
|
||||||
|
fmt.Print("pokecli > ")
|
||||||
|
}
|
13
cmd/pokecli/summaries.go
Normal file
13
cmd/pokecli/summaries.go
Normal file
|
@ -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
|
||||||
|
}
|
|
@ -3,7 +3,10 @@ package commands
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"maps"
|
"maps"
|
||||||
|
"os"
|
||||||
"slices"
|
"slices"
|
||||||
|
"strings"
|
||||||
|
"text/tabwriter"
|
||||||
)
|
)
|
||||||
|
|
||||||
func HelpFunc(summaries map[string]string) CommandFunc {
|
func HelpFunc(summaries map[string]string) CommandFunc {
|
||||||
|
@ -16,13 +19,21 @@ func HelpFunc(summaries map[string]string) CommandFunc {
|
||||||
|
|
||||||
slices.Sort(keys)
|
slices.Sort(keys)
|
||||||
|
|
||||||
fmt.Printf("\nCommands:\n")
|
var builder strings.Builder
|
||||||
|
|
||||||
|
builder.WriteString("\nCommands:\n")
|
||||||
|
|
||||||
|
tableWriter := tabwriter.NewWriter(&builder, 0, 8, 0, '\t', 0)
|
||||||
|
|
||||||
for _, key := range slices.All(keys) {
|
for _, key := range slices.All(keys) {
|
||||||
fmt.Printf("\n%s: %s", key, summaries[key])
|
fmt.Fprintf(tableWriter, "\n%s\t%s", key, summaries[key])
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("\n\n")
|
tableWriter.Flush()
|
||||||
|
|
||||||
|
builder.WriteString("\n\n")
|
||||||
|
|
||||||
|
fmt.Fprint(os.Stdout, builder.String())
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,7 +56,7 @@ func Lint() error {
|
||||||
// To rebuild packages that are already up-to-date set POKECLI_BUILD_REBUILD_ALL=1
|
// To rebuild packages that are already up-to-date set POKECLI_BUILD_REBUILD_ALL=1
|
||||||
// To enable verbose mode set POKECLI_BUILD_VERBOSE=1
|
// To enable verbose mode set POKECLI_BUILD_VERBOSE=1
|
||||||
func Build() error {
|
func Build() error {
|
||||||
main := "main.go"
|
path := "./cmd/" + app
|
||||||
flags := ldflags()
|
flags := ldflags()
|
||||||
build := sh.RunCmd("go", "build")
|
build := sh.RunCmd("go", "build")
|
||||||
args := []string{"-ldflags=" + flags, "-o", binary}
|
args := []string{"-ldflags=" + flags, "-o", binary}
|
||||||
|
@ -69,7 +69,7 @@ func Build() error {
|
||||||
args = append(args, "-v")
|
args = append(args, "-v")
|
||||||
}
|
}
|
||||||
|
|
||||||
args = append(args, main)
|
args = append(args, path)
|
||||||
|
|
||||||
return build(args...)
|
return build(args...)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue