-
Notifications
You must be signed in to change notification settings - Fork 23
feat: add JSON output for the apps command
#1199
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
Draft
EtienneM
wants to merge
1
commit into
master
Choose a base branch
from
feat/512/json_output
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,18 +2,17 @@ package apps | |
|
|
||
| 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/v10" | ||
| "github.com/Scalingo/go-utils/errors/v3" | ||
| ) | ||
|
|
||
| func List(ctx context.Context, projectSlug string) error { | ||
| type ListRenderer interface { | ||
| Render(ctx context.Context, apps []*scalingo.App) error | ||
| } | ||
|
|
||
| func List(ctx context.Context, renderer ListRenderer, projectSlug string) error { | ||
|
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. What I like with this pattern is that the |
||
| c, err := config.ScalingoClient(ctx) | ||
| if err != nil { | ||
| return errors.Wrap(ctx, err, "get Scalingo client") | ||
|
|
@@ -24,30 +23,27 @@ func List(ctx context.Context, projectSlug string) error { | |
| return errors.Wrap(ctx, err, "list apps") | ||
| } | ||
|
|
||
| if len(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 | ||
| filteredApps := filterAppsByProject(apps, projectSlug) | ||
|
|
||
| err = renderer.Render(ctx, filteredApps) | ||
| if err != nil { | ||
| return errors.Wrap(ctx, err, "render apps list") | ||
| } | ||
|
|
||
| t := tablewriter.NewWriter(os.Stdout) | ||
| t.Header([]string{"Name", "Role", "Status", "Project"}) | ||
| return nil | ||
| } | ||
|
|
||
| currentUser, err := config.C.CurrentUser(ctx) | ||
| if err != nil { | ||
| return errors.Wrap(ctx, err, "fail to get current user") | ||
| func filterAppsByProject(apps []*scalingo.App, projectSlug string) []*scalingo.App { | ||
| if projectSlug == "" { | ||
| return apps | ||
| } | ||
|
|
||
| filteredApps := make([]*scalingo.App, 0, len(apps)) | ||
| for _, app := range apps { | ||
| // If a filter was set but the app is not in the project, skip to the next one. | ||
| if projectSlug != "" && projectSlug != app.ProjectSlug() { | ||
| continue | ||
| if app.ProjectSlug() == projectSlug { | ||
| filteredApps = append(filteredApps, app) | ||
| } | ||
|
|
||
| role := utils.AppRole(currentUser, app) | ||
|
|
||
| _ = t.Append([]string{app.Name, string(role), string(app.Status), app.ProjectSlug()}) | ||
| } | ||
| _ = t.Render() | ||
|
|
||
| return nil | ||
| return filteredApps | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package json | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "os" | ||
|
|
||
| "github.com/Scalingo/cli/apps" | ||
| "github.com/Scalingo/go-scalingo/v10" | ||
| "github.com/Scalingo/go-utils/errors/v3" | ||
| ) | ||
|
|
||
| type appsListRenderer struct { | ||
| } | ||
|
|
||
| type appsListResponse struct { | ||
| Apps []*scalingo.App `json:"apps"` | ||
| } | ||
|
|
||
| func NewAppsList() apps.ListRenderer { | ||
| return appsListRenderer{} | ||
| } | ||
|
|
||
| func (r appsListRenderer) Render(ctx context.Context, apps []*scalingo.App) error { | ||
| err := json.NewEncoder(os.Stdout).Encode(appsListResponse{Apps: apps}) | ||
| if err != nil { | ||
| return errors.Wrap(ctx, err, "encode apps list to JSON") | ||
| } | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package table | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/olekukonko/tablewriter" | ||
|
|
||
| "github.com/Scalingo/cli/apps" | ||
| "github.com/Scalingo/cli/io" | ||
| "github.com/Scalingo/cli/utils" | ||
| "github.com/Scalingo/go-scalingo/v10" | ||
| "github.com/Scalingo/go-utils/errors/v3" | ||
| ) | ||
|
|
||
| type appsListRenderer struct { | ||
| currentUser *scalingo.User | ||
| } | ||
|
|
||
| func NewAppsList(currentUser *scalingo.User) apps.ListRenderer { | ||
| return appsListRenderer{ | ||
| currentUser: currentUser, | ||
| } | ||
| } | ||
|
|
||
| func (r appsListRenderer) Render(ctx context.Context, apps []*scalingo.App) error { | ||
| if len(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 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() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What I don't like is that it would require an interface per command. I don't really like that. I would love to have a single interface whatever the command. And the command only calls
Render(ctx). Then we would need a way to inject the data (list of apps or anything else depending on the command) in the renderer.