generated from templates/go-generic
Compare commits
1 commit
ab880bbbbb
...
c098a4c8ce
Author | SHA1 | Date | |
---|---|---|---|
c098a4c8ce |
6 changed files with 35 additions and 57 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1 +1 @@
|
||||||
/pokecli
|
pokecli
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
repl()
|
|
||||||
}
|
|
|
@ -1,13 +0,0 @@
|
||||||
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,10 +3,7 @@ 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 {
|
||||||
|
@ -19,21 +16,13 @@ func HelpFunc(summaries map[string]string) CommandFunc {
|
||||||
|
|
||||||
slices.Sort(keys)
|
slices.Sort(keys)
|
||||||
|
|
||||||
var builder strings.Builder
|
fmt.Printf("\nCommands:\n")
|
||||||
|
|
||||||
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.Fprintf(tableWriter, "\n%s\t%s", key, summaries[key])
|
fmt.Printf("\n%s: %s", key, summaries[key])
|
||||||
}
|
}
|
||||||
|
|
||||||
tableWriter.Flush()
|
fmt.Printf("\n\n")
|
||||||
|
|
||||||
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 {
|
||||||
path := "./cmd/" + app
|
main := "main.go"
|
||||||
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, path)
|
args = append(args, main)
|
||||||
|
|
||||||
return build(args...)
|
return build(args...)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"maps"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -17,7 +18,11 @@ type command struct {
|
||||||
callback commands.CommandFunc
|
callback commands.CommandFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
func repl() {
|
func main() {
|
||||||
|
run()
|
||||||
|
}
|
||||||
|
|
||||||
|
func run() {
|
||||||
var (
|
var (
|
||||||
cacheCleanupInterval = 30 * time.Minute
|
cacheCleanupInterval = 30 * time.Minute
|
||||||
httpTimeout = 10 * time.Second
|
httpTimeout = 10 * time.Second
|
||||||
|
@ -75,46 +80,52 @@ func repl() {
|
||||||
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)
|
||||||
|
|
||||||
loopFunc := func() {
|
for scanner.Scan() {
|
||||||
defer printPrompt()
|
|
||||||
|
|
||||||
input := scanner.Text()
|
input := scanner.Text()
|
||||||
|
|
||||||
command, args := parseInput(input)
|
command, args := parseArgs(input)
|
||||||
|
|
||||||
cmd, ok := commandMap[command]
|
cmd, ok := commandMap[command]
|
||||||
if !ok {
|
if !ok {
|
||||||
fmt.Println("ERROR: Unrecognised command.")
|
fmt.Println("ERROR: Unrecognised command.")
|
||||||
|
|
||||||
return
|
fmt.Print("\npokedex > ")
|
||||||
|
|
||||||
|
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.")
|
||||||
|
|
||||||
return
|
fmt.Print("\npokedex > ")
|
||||||
|
|
||||||
|
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")
|
func summaryMap(commandMap map[string]command) map[string]string {
|
||||||
printPrompt()
|
summaries := make(map[string]string)
|
||||||
|
|
||||||
for scanner.Scan() {
|
for key, value := range maps.All(commandMap) {
|
||||||
loopFunc()
|
summaries[key] = value.description
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseInput(input string) (string, []string) {
|
return summaries
|
||||||
input = strings.TrimSpace(input)
|
}
|
||||||
input = strings.ToLower(input)
|
|
||||||
|
func parseArgs(input string) (string, []string) {
|
||||||
split := strings.Split(input, " ")
|
split := strings.Split(input, " ")
|
||||||
|
|
||||||
if len(split) == 0 {
|
if len(split) == 0 {
|
||||||
|
@ -127,7 +138,3 @@ func parseInput(input string) (string, []string) {
|
||||||
|
|
||||||
return split[0], split[1:]
|
return split[0], split[1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
func printPrompt() {
|
|
||||||
fmt.Print("pokecli > ")
|
|
||||||
}
|
|
Loading…
Reference in a new issue