|
9 | 9 | "github.com/digitalghost-dev/poke-cli/cmd/pokemon" |
10 | 10 | "github.com/digitalghost-dev/poke-cli/cmd/search" |
11 | 11 | "github.com/digitalghost-dev/poke-cli/cmd/types" |
| 12 | + "github.com/digitalghost-dev/poke-cli/cmd/utils" |
12 | 13 | "github.com/digitalghost-dev/poke-cli/flags" |
13 | 14 | "github.com/digitalghost-dev/poke-cli/styling" |
14 | 15 | "os" |
@@ -39,19 +40,6 @@ func currentVersion() { |
39 | 40 | } |
40 | 41 | } |
41 | 42 |
|
42 | | -// handleCommandOutput wraps a function that returns (string, error) into a no-arg function |
43 | | -// that prints the output to stdout or stderr depending on whether an error occurred. |
44 | | -func handleCommandOutput(fn func() (string, error)) func() { |
45 | | - return func() { |
46 | | - output, err := fn() |
47 | | - if err != nil { |
48 | | - fmt.Fprintln(os.Stderr, output) |
49 | | - return |
50 | | - } |
51 | | - fmt.Println(output) |
52 | | - } |
53 | | -} |
54 | | - |
55 | 43 | func runCLI(args []string) int { |
56 | 44 | var output strings.Builder |
57 | 45 |
|
@@ -107,11 +95,11 @@ func runCLI(args []string) int { |
107 | 95 | } |
108 | 96 |
|
109 | 97 | commands := map[string]func(){ |
110 | | - "ability": handleCommandOutput(ability.AbilityCommand), |
111 | | - "move": handleCommandOutput(move.MoveCommand), |
112 | | - "natures": handleCommandOutput(natures.NaturesCommand), |
113 | | - "pokemon": handleCommandOutput(pokemon.PokemonCommand), |
114 | | - "types": handleCommandOutput(types.TypesCommand), |
| 98 | + "ability": utils.HandleCommandOutput(ability.AbilityCommand), |
| 99 | + "move": utils.HandleCommandOutput(move.MoveCommand), |
| 100 | + "natures": utils.HandleCommandOutput(natures.NaturesCommand), |
| 101 | + "pokemon": utils.HandleCommandOutput(pokemon.PokemonCommand), |
| 102 | + "types": utils.HandleCommandOutput(types.TypesCommand), |
115 | 103 | "search": search.SearchCommand, |
116 | 104 | } |
117 | 105 |
|
|
0 commit comments