Skip to content

Commit

Permalink
Merge pull request #982 from Scalingo/feat/968/maintenance-listing
Browse files Browse the repository at this point in the history
Add maintenance listing
  • Loading branch information
SCedricThomas authored Jul 13, 2023
2 parents f20a49a + 45b4813 commit f1e0b36
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* chore(term): remove `github.com/andrew-d/go-termutil`, use standard library instead
* feat(cmd): addon can be retrieve from addon type, not only UUID
* feat: add database maintenance listing with the new `database-maintenance-list` command
* feat(addons): add maintenance windows manipulation with the new `addon-config` command

### 1.29.1
Expand Down
3 changes: 3 additions & 0 deletions cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ var (
&databaseEnableFeature,
&databaseDisableFeature,

// Maintenance
&databaseMaintenanceList,

// Backups
&backupsListCommand,
&backupsCreateCommand,
Expand Down
44 changes: 44 additions & 0 deletions cmd/maintenance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cmd

import (
"github.com/urfave/cli/v2"

"github.com/Scalingo/cli/db/maintenance"
"github.com/Scalingo/cli/detect"
"github.com/Scalingo/cli/utils"
scalingo "github.com/Scalingo/go-scalingo/v6"
)

var databaseMaintenanceList = cli.Command{
Name: "database-maintenance-list",
Category: "Addons Maintenance",
Usage: "List the past and future maintenance on the given database",
Flags: []cli.Flag{
&appFlag,
&addonFlag,
&cli.IntFlag{Name: "page", Usage: "Page to display", Value: 1},
&cli.IntFlag{Name: "per-page", Usage: "Number of deployments to display", Value: 20},
},
Description: CommandDescription{
Description: "List database maintenance",
Examples: []string{
"scalingo --app my-app --addon addon-uuid database-maintenance-list",
"scalingo --app my-app --addon addon-uuid database-maintenance-list --per-page 20 --page 5",
},
}.Render(),

Action: func(c *cli.Context) error {
currentApp := detect.CurrentApp(c)
utils.CheckForConsent(c.Context, currentApp, utils.ConsentTypeDBs)
addonName := addonUUIDFromFlags(c, currentApp, true)

err := maintenance.List(c.Context, currentApp, addonName, scalingo.PaginationOpts{
Page: c.Int("page"),
PerPage: c.Int("per-page"),
})
if err != nil {
errorQuit(err)
}
return nil
},
}
53 changes: 53 additions & 0 deletions db/maintenance/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package maintenance

import (
"context"
"fmt"
"os"

"github.com/olekukonko/tablewriter"

"github.com/Scalingo/cli/config"
"github.com/Scalingo/cli/io"
"github.com/Scalingo/cli/utils"
"github.com/Scalingo/go-scalingo/v6"
"github.com/Scalingo/go-utils/errors/v2"
)

func List(ctx context.Context, app string, addonName string, paginationOpts scalingo.PaginationOpts) error {
c, err := config.ScalingoClient(ctx)
if err != nil {
return errors.Notef(ctx, err, "get Scalingo client")
}

response, err := c.DatabaseListMaintenance(ctx, app, addonName, paginationOpts)
if err != nil {
return errors.Notef(ctx, err, "list the database maintenance")
}

t := tablewriter.NewWriter(os.Stdout)
t.SetHeader([]string{"ID", "Type", "Started At", "Ended At", "Status"})

for _, maintenance := range response.Maintenance {
startedAt := "Not started"
if maintenance.StartedAt != nil {
startedAt = maintenance.StartedAt.Local().Format(utils.TimeFormat)
}

endedAt := ""
if maintenance.EndedAt != nil {
endedAt = maintenance.EndedAt.Local().Format(utils.TimeFormat)
}

t.Append([]string{
maintenance.ID,
string(maintenance.Type),
startedAt,
endedAt,
string(maintenance.Status),
})
}
t.Render()
fmt.Fprintln(os.Stderr, io.Gray(fmt.Sprintf("Page: %d, Last Page: %d", response.Meta.CurrentPage, response.Meta.TotalPages)))
return nil
}

0 comments on commit f1e0b36

Please sign in to comment.