|
| 1 | +package describe |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/inhies/go-bytesize" |
| 11 | + "github.com/spf13/cobra" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 13 | + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
| 14 | + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 15 | + "github.com/stackitcloud/stackit-cli/internal/pkg/flags" |
| 16 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 17 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 18 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/client" |
| 19 | + "github.com/stackitcloud/stackit-cli/internal/pkg/tables" |
| 20 | + "github.com/stackitcloud/stackit-sdk-go/services/postgresflex" |
| 21 | +) |
| 22 | + |
| 23 | +const ( |
| 24 | + backupIdArg = "BACKUP_ID" |
| 25 | + |
| 26 | + instanceIdFlag = "instance-id" |
| 27 | + |
| 28 | + backupExpireYearOffset = 0 |
| 29 | + backupExpireMonthOffset = 0 |
| 30 | + backupExpireDayOffset = 30 |
| 31 | +) |
| 32 | + |
| 33 | +type inputModel struct { |
| 34 | + *globalflags.GlobalFlagModel |
| 35 | + |
| 36 | + InstanceId string |
| 37 | + BackupId string |
| 38 | +} |
| 39 | + |
| 40 | +func NewCmd(p *print.Printer) *cobra.Command { |
| 41 | + cmd := &cobra.Command{ |
| 42 | + Use: fmt.Sprintf("describe %s", backupIdArg), |
| 43 | + Short: "Shows details of a backup for a PostgreSQL Flex instance", |
| 44 | + Long: "Shows details of a backup for a PostgreSQL Flex instance.", |
| 45 | + Example: examples.Build( |
| 46 | + examples.NewExample( |
| 47 | + `Get details of a backup with ID "xxx" for a PostgreSQL Flex instance with ID "yyy"`, |
| 48 | + "$ stackit postgresflex backup describe xxx --instance-id yyy"), |
| 49 | + examples.NewExample( |
| 50 | + `Get details of a backup with ID "xxx" for a PostgreSQL Flex instance with ID "yyy" in a table format`, |
| 51 | + "$ stackit postgresflex backup describe xxx --instance-id yyy --output-format pretty"), |
| 52 | + ), |
| 53 | + Args: args.SingleArg(backupIdArg, nil), |
| 54 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 55 | + ctx := context.Background() |
| 56 | + model, err := parseInput(p, cmd, args) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + // Configure API client |
| 62 | + apiClient, err := client.ConfigureClient(p) |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + |
| 67 | + // Call API |
| 68 | + req := buildRequest(ctx, model, apiClient) |
| 69 | + resp, err := req.Execute() |
| 70 | + |
| 71 | + if err != nil { |
| 72 | + return fmt.Errorf("describe backup for PostgreSQL Flex instance: %w", err) |
| 73 | + } |
| 74 | + |
| 75 | + return outputResult(p, cmd, model.OutputFormat, *resp.Item) |
| 76 | + }, |
| 77 | + } |
| 78 | + configureFlags(cmd) |
| 79 | + return cmd |
| 80 | +} |
| 81 | + |
| 82 | +func configureFlags(cmd *cobra.Command) { |
| 83 | + cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "Instance ID") |
| 84 | + |
| 85 | + err := flags.MarkFlagsRequired(cmd, instanceIdFlag) |
| 86 | + cobra.CheckErr(err) |
| 87 | +} |
| 88 | + |
| 89 | +func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) { |
| 90 | + backupId := inputArgs[0] |
| 91 | + |
| 92 | + globalFlags := globalflags.Parse(p, cmd) |
| 93 | + if globalFlags.ProjectId == "" { |
| 94 | + return nil, &errors.ProjectIdError{} |
| 95 | + } |
| 96 | + |
| 97 | + return &inputModel{ |
| 98 | + GlobalFlagModel: globalFlags, |
| 99 | + InstanceId: flags.FlagToStringValue(p, cmd, instanceIdFlag), |
| 100 | + BackupId: backupId, |
| 101 | + }, nil |
| 102 | +} |
| 103 | + |
| 104 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *postgresflex.APIClient) postgresflex.ApiGetBackupRequest { |
| 105 | + req := apiClient.GetBackup(ctx, model.ProjectId, model.InstanceId, model.BackupId) |
| 106 | + return req |
| 107 | +} |
| 108 | + |
| 109 | +func outputResult(p *print.Printer, cmd *cobra.Command, outputFormat string, backup postgresflex.Backup) error { |
| 110 | + backupStartTime, err := time.Parse(time.RFC3339, *backup.StartTime) |
| 111 | + if err != nil { |
| 112 | + return fmt.Errorf("parse backup start time: %w", err) |
| 113 | + } |
| 114 | + backupExpireDate := backupStartTime.AddDate(backupExpireYearOffset, backupExpireMonthOffset, backupExpireDayOffset).Format(time.DateOnly) |
| 115 | + |
| 116 | + switch outputFormat { |
| 117 | + case print.PrettyOutputFormat: |
| 118 | + table := tables.NewTable() |
| 119 | + table.AddRow("ID", *backup.Id) |
| 120 | + table.AddSeparator() |
| 121 | + table.AddRow("START TIME", *backup.StartTime) |
| 122 | + table.AddSeparator() |
| 123 | + table.AddRow("EXPIRES AT", backupExpireDate) |
| 124 | + table.AddSeparator() |
| 125 | + table.AddRow("BACKUP SIZE", bytesize.New(float64(*backup.Size))) |
| 126 | + |
| 127 | + err := table.Display(p) |
| 128 | + if err != nil { |
| 129 | + return fmt.Errorf("render table: %w", err) |
| 130 | + } |
| 131 | + |
| 132 | + return nil |
| 133 | + default: |
| 134 | + details, err := json.MarshalIndent(backup, "", " ") |
| 135 | + if err != nil { |
| 136 | + return fmt.Errorf("marshal backup for PostgreSQL Flex instance: %w", err) |
| 137 | + } |
| 138 | + cmd.Println(string(details)) |
| 139 | + |
| 140 | + return nil |
| 141 | + } |
| 142 | +} |
0 commit comments