Skip to content

Commit

Permalink
Add yaml output format to mongodbflex backup commands (#319)
Browse files Browse the repository at this point in the history
* add yaml output to mongodbflex commands

* update docs

* Update internal/cmd/mongodbflex/backup/describe/describe.go

Co-authored-by: João Palet <joao.palet@outlook.com>

* change error log

---------

Co-authored-by: João Palet <joao.palet@outlook.com>
  • Loading branch information
GokceGK and joaopalet authored May 15, 2024
1 parent 107b90d commit 41915da
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 23 deletions.
2 changes: 1 addition & 1 deletion docs/stackit_mongodbflex_backup.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ stackit mongodbflex backup [flags]
```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty" "none"]
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
-p, --project-id string Project ID
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```
Expand Down
2 changes: 1 addition & 1 deletion docs/stackit_mongodbflex_backup_describe.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ stackit mongodbflex backup describe BACKUP_ID [flags]
```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty" "none"]
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
-p, --project-id string Project ID
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```
Expand Down
2 changes: 1 addition & 1 deletion docs/stackit_mongodbflex_backup_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ stackit mongodbflex backup list [flags]
```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty" "none"]
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
-p, --project-id string Project ID
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```
Expand Down
2 changes: 1 addition & 1 deletion docs/stackit_mongodbflex_backup_restore-jobs.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ stackit mongodbflex backup restore-jobs [flags]
```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty" "none"]
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
-p, --project-id string Project ID
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```
Expand Down
2 changes: 1 addition & 1 deletion docs/stackit_mongodbflex_backup_restore.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ stackit mongodbflex backup restore [flags]
```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty" "none"]
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
-p, --project-id string Project ID
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```
Expand Down
2 changes: 1 addition & 1 deletion docs/stackit_mongodbflex_backup_schedule.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ stackit mongodbflex backup schedule [flags]
```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty" "none"]
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
-p, --project-id string Project ID
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```
Expand Down
2 changes: 1 addition & 1 deletion docs/stackit_mongodbflex_backup_update-schedule.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ stackit mongodbflex backup update-schedule [flags]
```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty" "none"]
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
-p, --project-id string Project ID
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```
Expand Down
11 changes: 10 additions & 1 deletion internal/cmd/mongodbflex/backup/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"

"github.com/goccy/go-yaml"
"github.com/inhies/go-bytesize"
"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
Expand Down Expand Up @@ -129,10 +130,18 @@ func outputResult(p *print.Printer, cmd *cobra.Command, outputFormat, restoreSta
case print.JSONOutputFormat:
details, err := json.MarshalIndent(backup, "", " ")
if err != nil {
return fmt.Errorf("marshal backup for MongoDB Flex backup: %w", err)
return fmt.Errorf("marshal MongoDB Flex backup: %w", err)
}
cmd.Println(string(details))

return nil
case print.YAMLOutputFormat:
details, err := yaml.Marshal(backup)
if err != nil {
return fmt.Errorf("marshal MongoDB Flex backup: %w", err)
}
p.Outputln(string(details))

return nil
default:
table := tables.NewTable()
Expand Down
9 changes: 9 additions & 0 deletions internal/cmd/mongodbflex/backup/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"

"github.com/goccy/go-yaml"
"github.com/inhies/go-bytesize"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
Expand Down Expand Up @@ -152,6 +153,14 @@ func outputResult(p *print.Printer, outputFormat string, backups []mongodbflex.B
}
p.Outputln(string(details))

return nil
case print.YAMLOutputFormat:
details, err := yaml.Marshal(backups)
if err != nil {
return fmt.Errorf("marshal MongoDB Flex backups list: %w", err)
}
p.Outputln(string(details))

return nil
default:
table := tables.NewTable()
Expand Down
9 changes: 9 additions & 0 deletions internal/cmd/mongodbflex/backup/restore-jobs/restore_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"

"github.com/goccy/go-yaml"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
Expand Down Expand Up @@ -146,6 +147,14 @@ func outputResult(p *print.Printer, outputFormat string, restoreJobs []mongodbfl
}
p.Outputln(string(details))

return nil
case print.YAMLOutputFormat:
details, err := yaml.Marshal(restoreJobs)
if err != nil {
return fmt.Errorf("marshal MongoDB Flex restore jobs list: %w", err)
}
p.Outputln(string(details))

return nil
default:
table := tables.NewTable()
Expand Down
40 changes: 25 additions & 15 deletions internal/cmd/mongodbflex/backup/schedule/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"

"github.com/goccy/go-yaml"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
Expand Down Expand Up @@ -103,29 +104,38 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mongodbflex
}

func outputResult(p *print.Printer, outputFormat string, instance *mongodbflex.Instance) error {
output := struct {
BackupSchedule string `json:"backup_schedule"`
DailySnaphotRetentionDays string `json:"daily_snapshot_retention_days"`
MonthlySnapshotRetentionMonths string `json:"monthly_snapshot_retention_months"`
PointInTimeWindowHours string `json:"point_in_time_window_hours"`
SnapshotRetentionDays string `json:"snapshot_retention_days"`
WeeklySnapshotRetentionWeeks string `json:"weekly_snapshot_retention_weeks"`
}{
BackupSchedule: *instance.BackupSchedule,
DailySnaphotRetentionDays: (*instance.Options)["dailySnapshotRetentionDays"],
MonthlySnapshotRetentionMonths: (*instance.Options)["monthlySnapshotRetentionDays"],
PointInTimeWindowHours: (*instance.Options)["pointInTimeWindowHours"],
SnapshotRetentionDays: (*instance.Options)["snapshotRetentionDays"],
WeeklySnapshotRetentionWeeks: (*instance.Options)["weeklySnapshotRetentionWeeks"],
}

switch outputFormat {
case print.JSONOutputFormat:
output := struct {
BackupSchedule string `json:"backup_schedule"`
DailySnaphotRetentionDays string `json:"daily_snapshot_retention_days"`
MonthlySnapshotRetentionMonths string `json:"monthly_snapshot_retention_months"`
PointInTimeWindowHours string `json:"point_in_time_window_hours"`
SnapshotRetentionDays string `json:"snapshot_retention_days"`
WeeklySnapshotRetentionWeeks string `json:"weekly_snapshot_retention_weeks"`
}{
BackupSchedule: *instance.BackupSchedule,
DailySnaphotRetentionDays: (*instance.Options)["dailySnapshotRetentionDays"],
MonthlySnapshotRetentionMonths: (*instance.Options)["monthlySnapshotRetentionDays"],
PointInTimeWindowHours: (*instance.Options)["pointInTimeWindowHours"],
SnapshotRetentionDays: (*instance.Options)["snapshotRetentionDays"],
WeeklySnapshotRetentionWeeks: (*instance.Options)["weeklySnapshotRetentionWeeks"],
}
details, err := json.MarshalIndent(output, "", " ")
if err != nil {
return fmt.Errorf("marshal MongoDB Flex backup schedule: %w", err)
}
p.Outputln(string(details))

return nil
case print.YAMLOutputFormat:
details, err := yaml.Marshal(output)
if err != nil {
return fmt.Errorf("marshal MongoDB Flex backup schedule: %w", err)
}
p.Outputln(string(details))

return nil
default:
table := tables.NewTable()
Expand Down

0 comments on commit 41915da

Please sign in to comment.