Skip to content

Add yaml output format to mongodbflex backup commands #319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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