-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcli.go
More file actions
178 lines (155 loc) · 5.48 KB
/
cli.go
File metadata and controls
178 lines (155 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"flag"
"fmt"
"os"
"runtime/debug"
"strings"
"github.com/digitalghost-dev/poke-cli/cmd/ability"
"github.com/digitalghost-dev/poke-cli/cmd/berry"
"github.com/digitalghost-dev/poke-cli/cmd/card"
"github.com/digitalghost-dev/poke-cli/cmd/item"
"github.com/digitalghost-dev/poke-cli/cmd/move"
"github.com/digitalghost-dev/poke-cli/cmd/natures"
"github.com/digitalghost-dev/poke-cli/cmd/pokemon"
"github.com/digitalghost-dev/poke-cli/cmd/search"
"github.com/digitalghost-dev/poke-cli/cmd/speed"
"github.com/digitalghost-dev/poke-cli/cmd/types"
"github.com/digitalghost-dev/poke-cli/cmd/utils"
"github.com/digitalghost-dev/poke-cli/flags"
"github.com/digitalghost-dev/poke-cli/styling"
)
var version = "(devel)"
var commandDescriptions = []struct {
name string
desc string
}{
{"ability", "Get details about an ability"},
{"berry", "Get details about a berry"},
{"card", "Get details about a TCG card"},
{"item", "Get details about an item"},
{"move", "Get details about a move"},
{"natures", "Get details about all natures"},
{"pokemon", "Get details about a Pokémon"},
{"search", "Search for a resource"},
{"speed", "Calculate the speed of a Pokémon in battle"},
{"types", "Get details about a typing"},
}
func renderCommandList() string {
var sb strings.Builder
for _, cmd := range commandDescriptions {
sb.WriteString(fmt.Sprintf("\n\t%-15s %s", cmd.name, cmd.desc))
}
return sb.String()
}
func currentVersion() {
if version != "(devel)" {
// Use version injected by -ldflags
fmt.Printf("Version: %s\n", version)
return
}
// Fallback to build info when the version is not set
buildInfo, ok := debug.ReadBuildInfo()
if !ok {
fmt.Println("Version: unknown (unable to read build info)")
return
}
if buildInfo.Main.Version != "" {
fmt.Printf("Version: %s\n", buildInfo.Main.Version)
} else {
fmt.Println("Version: (devel)")
}
}
func runCLI(args []string) int {
var output strings.Builder
mainFlagSet := flag.NewFlagSet("poke-cli", flag.ContinueOnError)
// -l, --latest flag retrieves the latest Docker image and GitHub release versions available
latestFlag := mainFlagSet.Bool("latest", false, "Prints the program's latest Docker image and release versions.")
shortLatestFlag := mainFlagSet.Bool("l", false, "Prints the program's latest Docker image and release versions.")
// -v, --version flag retrieves the currently installed version
currentVersionFlag := mainFlagSet.Bool("version", false, "Prints the current version")
shortCurrentVersionFlag := mainFlagSet.Bool("v", false, "Prints the current version")
mainFlagSet.Usage = func() {
helpMessage := styling.HelpBorder.Render(
"Welcome! This tool displays data related to Pokémon!",
"\n\n", styling.StyleBold.Render("USAGE:"),
fmt.Sprintf("\n\t%-15s %s", "poke-cli [flag]", ""),
fmt.Sprintf("\n\t%-15s %s", "poke-cli <command> [flag]", ""),
fmt.Sprintf("\n\t%-15s %s", "poke-cli <command> <subcommand> [flag]", ""),
"\n\n", styling.StyleBold.Render("FLAGS:"),
fmt.Sprintf("\n\t%-15s %s", "-h, --help", "Shows the help menu"),
fmt.Sprintf("\n\t%-15s %s", "-l, --latest", "Prints the latest version available"),
fmt.Sprintf("\n\t%-15s %s", "-v, --version", "Prints the current version"),
"\n\n", styling.StyleBold.Render("COMMANDS:"),
renderCommandList(),
"\n\n", styling.StyleItalic.Render(styling.HyphenHint),
"\n", styling.StyleItalic.Render("example: poke-cli ability strong-jaw"),
"\n", styling.StyleItalic.Render("example: poke-cli pokemon flutter-mane"),
"\n\n", fmt.Sprintf("%s %s", "↓ ctrl/cmd + click for docs/guides\n", styling.DocsLink),
)
fmt.Println(helpMessage)
}
switch {
case len(args) == 0:
mainFlagSet.Usage()
return 0
case len(args) > 0:
if args[0] == "-h" || args[0] == "--help" {
mainFlagSet.Usage()
return 0
}
}
err := mainFlagSet.Parse(args)
if err != nil {
return 2
}
remainingArgs := mainFlagSet.Args()
commands := map[string]func() int{
"ability": utils.HandleCommandOutput(ability.AbilityCommand),
"berry": utils.HandleCommandOutput(berry.BerryCommand),
"card": utils.HandleCommandOutput(card.CardCommand),
"item": utils.HandleCommandOutput(item.ItemCommand),
"move": utils.HandleCommandOutput(move.MoveCommand),
"natures": utils.HandleCommandOutput(natures.NaturesCommand),
"pokemon": utils.HandleCommandOutput(pokemon.PokemonCommand),
"speed": utils.HandleCommandOutput(speed.SpeedCommand),
"types": utils.HandleCommandOutput(types.TypesCommand),
"search": utils.HandleCommandOutput(search.SearchCommand),
}
cmdArg := ""
if len(remainingArgs) >= 1 {
cmdArg = remainingArgs[0]
}
cmdFunc, exists := commands[cmdArg]
switch {
case len(remainingArgs) == 0 && !*latestFlag && !*shortLatestFlag && !*currentVersionFlag && !*shortCurrentVersionFlag:
mainFlagSet.Usage()
return 1
case *latestFlag || *shortLatestFlag:
_, err := flags.LatestFlag()
if err != nil {
return 1
}
return 0
case *currentVersionFlag || *shortCurrentVersionFlag:
currentVersion()
return 0
case exists:
return cmdFunc()
default:
errMessage := styling.ErrorBorder.Render(
styling.ErrorColor.Render("✖ Error!"),
fmt.Sprintf("\n\t%-15s", fmt.Sprintf("'%s' is not a valid command.\n", cmdArg)),
styling.StyleBold.Render("\nCommands:"),
renderCommandList(),
fmt.Sprintf("\n\nAlso run %s for more info!", styling.StyleBold.Render("poke-cli -h")),
)
output.WriteString(errMessage)
fmt.Println(output.String())
return 1
}
}
var exit = os.Exit
func main() {
exit(runCLI(os.Args[1:]))
}