@@ -2,18 +2,14 @@ package apps
22
33import (
44 "context"
5- "fmt"
6- "os"
7-
8- "github.com/olekukonko/tablewriter"
95
106 "github.com/Scalingo/cli/config"
11- "github.com/Scalingo/cli/io "
12- "github.com/Scalingo/cli/utils "
7+ "github.com/Scalingo/cli/internal/boundaries/out/renderer "
8+ "github.com/Scalingo/go-scalingo/v11 "
139 "github.com/Scalingo/go-utils/errors/v3"
1410)
1511
16- func List (ctx context.Context , projectSlug string ) error {
12+ func List (ctx context.Context , renderer renderer. Renderer [[] * scalingo. App ], projectSlug string ) error {
1713 c , err := config .ScalingoClient (ctx )
1814 if err != nil {
1915 return errors .Wrap (ctx , err , "get Scalingo client" )
@@ -24,30 +20,28 @@ func List(ctx context.Context, projectSlug string) error {
2420 return errors .Wrap (ctx , err , "list apps" )
2521 }
2622
27- if len (apps ) == 0 {
28- fmt .Println (io .Indent ("\n You haven't created any app yet, create your first application using:\n → scalingo create <app_name>\n " , 2 ))
29- return nil
23+ filteredApps := filterAppsByProject (apps , projectSlug )
24+ renderer .SetData (ctx , filteredApps )
25+
26+ err = renderer .Render (ctx )
27+ if err != nil {
28+ return errors .Wrap (ctx , err , "render apps list" )
3029 }
3130
32- t := tablewriter . NewWriter ( os . Stdout )
33- t . Header ([] string { "Name" , "Role" , "Status" , "Project" })
31+ return nil
32+ }
3433
35- currentUser , err := config . C . CurrentUser ( ctx )
36- if err != nil {
37- return errors . Wrap ( ctx , err , "fail to get current user" )
34+ func filterAppsByProject ( apps [] * scalingo. App , projectSlug string ) [] * scalingo. App {
35+ if projectSlug == "" {
36+ return apps
3837 }
3938
39+ filteredApps := make ([]* scalingo.App , 0 , len (apps ))
4040 for _ , app := range apps {
41- // If a filter was set but the app is not in the project, skip to the next one.
42- if projectSlug != "" && projectSlug != app .ProjectSlug () {
43- continue
41+ if app .ProjectSlug () == projectSlug {
42+ filteredApps = append (filteredApps , app )
4443 }
45-
46- role := utils .AppRole (currentUser , app )
47-
48- _ = t .Append ([]string {app .Name , string (role ), string (app .Status ), app .ProjectSlug ()})
4944 }
50- _ = t .Render ()
5145
52- return nil
46+ return filteredApps
5347}
0 commit comments