-
Notifications
You must be signed in to change notification settings - Fork 23
feat: add JSON output for the apps command
#1204
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package renderer | ||
|
|
||
| type Format string | ||
|
|
||
| const ( | ||
| FormatJSON Format = "json" | ||
| FormatTable Format = "table" | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package json | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "os" | ||
|
|
||
| "github.com/Scalingo/cli/internal/boundaries/out/renderer" | ||
| "github.com/Scalingo/go-scalingo/v11" | ||
| "github.com/Scalingo/go-utils/errors/v3" | ||
| ) | ||
|
|
||
| type appsListRenderer struct { | ||
| apps []*scalingo.App | ||
| } | ||
|
|
||
| type appsListResponse struct { | ||
| Apps []*scalingo.App `json:"apps"` | ||
| } | ||
|
|
||
| func NewAppsList() renderer.Renderer[[]*scalingo.App] { | ||
| return &appsListRenderer{} | ||
| } | ||
|
|
||
| func (r *appsListRenderer) Render(ctx context.Context) error { | ||
| err := json.NewEncoder(os.Stdout).Encode(appsListResponse{Apps: r.apps}) | ||
| if err != nil { | ||
| return errors.Wrap(ctx, err, "encode apps list to JSON") | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (r *appsListRenderer) SetData(ctx context.Context, apps []*scalingo.App) { | ||
| r.apps = apps | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package renderer | ||
|
|
||
| import "context" | ||
|
|
||
| type Renderer[D any] interface { | ||
| Render(ctx context.Context) error | ||
| SetData(ctx context.Context, data D) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package table | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/olekukonko/tablewriter" | ||
|
|
||
| "github.com/Scalingo/cli/internal/boundaries/out/renderer" | ||
| "github.com/Scalingo/cli/io" | ||
| "github.com/Scalingo/cli/utils" | ||
| "github.com/Scalingo/go-scalingo/v11" | ||
| "github.com/Scalingo/go-utils/errors/v3" | ||
| ) | ||
|
|
||
| type appsListRenderer struct { | ||
| currentUser *scalingo.User | ||
| apps []*scalingo.App | ||
| } | ||
|
|
||
| func NewAppsList(currentUser *scalingo.User) renderer.Renderer[[]*scalingo.App] { | ||
| return &appsListRenderer{ | ||
| currentUser: currentUser, | ||
| } | ||
| } | ||
|
|
||
| func (r *appsListRenderer) Render(ctx context.Context) error { | ||
| if len(r.apps) == 0 { | ||
| fmt.Println(io.Indent("\nYou haven't created any app yet, create your first application using:\n→ scalingo create <app_name>\n", 2)) | ||
| return nil | ||
| } | ||
|
|
||
| t := tablewriter.NewWriter(os.Stdout) | ||
| t.Header([]string{"Name", "Role", "Status", "Project"}) | ||
|
|
||
| for _, app := range r.apps { | ||
| role := utils.AppRole(r.currentUser, app) | ||
| err := t.Append([]string{app.Name, string(role), string(app.Status), app.ProjectSlug()}) | ||
| if err != nil { | ||
| return errors.Wrap(ctx, err, "append app to table") | ||
| } | ||
| } | ||
|
|
||
| return t.Render() | ||
| } | ||
|
|
||
| func (r *appsListRenderer) SetData(ctx context.Context, apps []*scalingo.App) { | ||
| r.apps = apps | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ import ( | |
| "github.com/Scalingo/cli/cmd" | ||
| "github.com/Scalingo/cli/cmd/autocomplete" | ||
| "github.com/Scalingo/cli/config" | ||
| "github.com/Scalingo/cli/internal/boundaries/out/renderer" | ||
| "github.com/Scalingo/cli/signals" | ||
| "github.com/Scalingo/cli/update" | ||
| "github.com/Scalingo/go-scalingo/v11/debug" | ||
|
|
@@ -88,6 +89,7 @@ func main() { | |
| app.Flags = []cli.Flag{ | ||
| &cli.StringFlag{Name: "addon", Value: "<addon_id>", Usage: "ID of the current addon", Sources: cli.EnvVars("SCALINGO_ADDON")}, | ||
| &cli.StringFlag{Name: "app", Aliases: []string{"a"}, Value: "<name>", Usage: "Name of the app", Sources: cli.EnvVars("SCALINGO_APP")}, | ||
| &cli.StringFlag{Name: "format", Value: string(renderer.FormatTable), Usage: "[" + string(renderer.FormatJSON) + "|" + string(renderer.FormatTable) + "]"}, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: will this root
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question! I double checked and I confirm that the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks nice. |
||
| &cli.StringFlag{Name: "remote", Aliases: []string{"r"}, Value: "scalingo", Usage: "Name of the remote"}, | ||
| &cli.StringFlag{Name: "region", Value: "", Usage: "Name of the region to use"}, | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: When filtering apps by project, is this message still valid?
Is it possible the user may have created apps that are not in the project?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mmmh that's a good point 🤔
Would the following phrasing be better for you?
Or you thought about something else?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That works.