Skip to content

Commit

Permalink
feat: add maintenance listing
Browse files Browse the repository at this point in the history
  • Loading branch information
SCedricThomas committed Jul 4, 2023
1 parent fe9c5c7 commit 9e5d8d3
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ var (
&databaseEnableFeature,
&databaseDisableFeature,

// Maintenance
&databaseMaintenanceList,

// Backups
&backupsListCommand,
&backupsCreateCommand,
Expand Down
41 changes: 41 additions & 0 deletions cmd/maintenance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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",
Usage: "List database maintenance",
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{},
}.Render(),

Action: func(c *cli.Context) error {
currentApp := detect.CurrentApp(c)
utils.CheckForConsent(c.Context, currentApp, utils.ConsentTypeDBs)
addonName := addonNameFromFlags(c, 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"
"gopkg.in/errgo.v1"

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

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

response, err := c.DatabaseListMaintenance(ctx, app, addonName, paginationOpts)

Check failure on line 23 in db/maintenance/list.go

View workflow job for this annotation

GitHub Actions / Unit Tests

c.DatabaseListMaintenance undefined (type *scalingo.Client has no field or method DatabaseListMaintenance)
if err != nil {
return errgo.Notef(err, "fail to 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.Format(utils.TimeFormat)
}

endedAt := ""
if maintenance.EndedAt != nil {
endedAt = maintenance.EndedAt.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 9e5d8d3

Please sign in to comment.