Skip to content

Commit 80900a2

Browse files
committed
Refactor 'pgo show' functions and update help output
Issue: PGO-13
1 parent dd4ef30 commit 80900a2

File tree

4 files changed

+72
-37
lines changed

4 files changed

+72
-37
lines changed

docs/content/reference/pgo_show_backup.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ stanza: db
5353
### Options
5454

5555
```
56-
-h, --help help for backup
57-
-o, --output pgbackrestFormat output format. types supported: text,json (default text)
58-
--repoName string Set the repository name for the command. example: repo1
56+
-h, --help help for backup
57+
-o, --output string output format. types supported: text,json (default "text")
58+
--repoName string Set the repository name for the command. example: repo1
5959
```
6060

6161
### Options inherited from parent commands

docs/content/reference/pgo_show_ha.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ pgo show ha hippo --output json
4444
### Options
4545

4646
```
47-
-h, --help help for ha
48-
-o, --output patroniFormat output format. types supported: pretty,tsv,json,yaml (default pretty)
47+
-h, --help help for ha
48+
-o, --output string output format. types supported: pretty,tsv,json,yaml (default "pretty")
4949
```
5050

5151
### Options inherited from parent commands

internal/cmd/show.go

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,26 @@ HA
8787

8888
// Print the pgbackrest info output received.
8989
cmd.Printf("BACKUP\n\n")
90-
if err := newShowBackupCommand(config).RunE(cmd, args); err != nil {
90+
if stdout, stderr, err := showBackup(config, args, "text", ""); err != nil {
9191
return err
92+
} else {
93+
cmd.Printf(stdout)
94+
if stderr != "" {
95+
cmd.Printf("\nError returned: %s\n", stderr)
96+
}
9297
}
9398

9499
// Print the patronictl list output received.
95100
cmd.Printf("\nHA\n\n")
96-
return newShowHACommand(config).RunE(cmd, args)
101+
if stdout, stderr, err := showHA(config, args, "pretty"); err != nil {
102+
return err
103+
} else {
104+
cmd.Printf(stdout)
105+
if stderr != "" {
106+
cmd.Printf("\nError returned: %s\n", stderr)
107+
}
108+
}
109+
return nil
97110
}
98111

99112
return cmdShow
@@ -164,28 +177,37 @@ stanza: db
164177
// handle validation.
165178
repoNum := strings.TrimPrefix(repoName, "repo")
166179

167-
exec, err := getPrimaryExec(config, args)
168-
if err != nil {
169-
return err
170-
}
180+
stdout, stderr, err := showBackup(config, args, outputEnum.String(), repoNum)
171181

172-
stdout, stderr, err := Executor(exec).pgBackRestInfo(outputEnum.String(), repoNum)
173-
if err != nil {
174-
return err
182+
if err == nil {
183+
cmd.Printf(stdout)
184+
if stderr != "" {
185+
cmd.Printf("\nError returned: %s\n", stderr)
186+
}
175187
}
176188

177-
// Print the output received.
178-
cmd.Printf(stdout)
179-
if stderr != "" {
180-
cmd.Printf("\nError returned: %s\n", stderr)
181-
}
182-
183-
return nil
189+
return err
184190
}
185191

186192
return cmdShowBackup
187193
}
188194

195+
// showBackup execs into the primary Pod, runs the 'pgbackrest info' command and
196+
// returns the command output and/or error
197+
func showBackup(
198+
config *internal.Config,
199+
args []string,
200+
output string,
201+
repoNum string) (string, string, error) {
202+
203+
exec, err := getPrimaryExec(config, args)
204+
if err != nil {
205+
return "", "", err
206+
}
207+
208+
return Executor(exec).pgBackRestInfo(output, repoNum)
209+
}
210+
189211
// newShowHACommand returns the output of the 'patronictl list' command.
190212
// - https://patroni.readthedocs.io/en/latest/patronictl.html#patronictl-list
191213
func newShowHACommand(config *internal.Config) *cobra.Command {
@@ -227,28 +249,35 @@ pgo show ha hippo --output json
227249
// Define the 'show backup' command
228250
cmdShowHA.RunE = func(cmd *cobra.Command, args []string) error {
229251

230-
exec, err := getPrimaryExec(config, args)
231-
if err != nil {
232-
return err
233-
}
234-
235-
stdout, stderr, err := Executor(exec).patronictl("list", outputEnum.String())
236-
if err != nil {
237-
return err
238-
}
252+
stdout, stderr, err := showHA(config, args, outputEnum.String())
239253

240-
// Print the output received.
241-
cmd.Printf(stdout)
242-
if stderr != "" {
243-
cmd.Printf("\nError returned: %s\n", stderr)
254+
if err == nil {
255+
cmd.Printf(stdout)
256+
if stderr != "" {
257+
cmd.Printf("\nError returned: %s\n", stderr)
258+
}
244259
}
245260

246-
return nil
261+
return err
247262
}
248263

249264
return cmdShowHA
250265
}
251266

267+
// showHA execs into the primary Pod, runs the 'patronictl list' command and
268+
// returns the command output and/or error
269+
func showHA(
270+
config *internal.Config,
271+
args []string,
272+
output string) (string, string, error) {
273+
exec, err := getPrimaryExec(config, args)
274+
if err != nil {
275+
return "", "", err
276+
}
277+
278+
return Executor(exec).patronictl("list", output)
279+
}
280+
252281
// getPrimaryExec returns a executor function for the primary Pod to allow for
253282
// commands to be run against it.
254283
func getPrimaryExec(config *internal.Config, args []string) (

internal/util/enum.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ package util
1616

1717
import "errors"
1818

19+
// Define custom value types to use as flags for certain commands.
20+
// Cobra uses the pflag package and allows custom value types by implementing
21+
// the pflag.Value interface.
22+
// - https://github.com/spf13/pflag
23+
// - https://pkg.go.dev/github.com/spf13/pflag#Value
24+
1925
// 'patroni list' output format options
2026
// - https://patroni.readthedocs.io/en/latest/patronictl.html#patronictl-list
2127
type patroniFormat string
@@ -45,7 +51,7 @@ func (e *patroniFormat) Set(v string) error {
4551

4652
// Type is only used in help text
4753
func (e *patroniFormat) Type() string {
48-
return "patroniFormat"
54+
return "string"
4955
}
5056

5157
// 'pgbackrest info' output format options
@@ -75,5 +81,5 @@ func (e *pgbackrestFormat) Set(v string) error {
7581

7682
// Type is only used in help text
7783
func (e *pgbackrestFormat) Type() string {
78-
return "pgbackrestFormat"
84+
return "string"
7985
}

0 commit comments

Comments
 (0)