-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathapplication_list.go
More file actions
104 lines (82 loc) · 3.05 KB
/
Copy pathapplication_list.go
File metadata and controls
104 lines (82 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package cmd
import (
"context"
"encoding/json"
"github.com/qovery/qovery-client-go"
"os"
"github.com/qovery/qovery-cli/utils"
"github.com/spf13/cobra"
)
var applicationListCmd = &cobra.Command{
Use: "list",
Short: "List applications",
Run: func(cmd *cobra.Command, args []string) {
utils.Capture(cmd)
tokenType, token, err := utils.GetAccessToken()
if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}
client := utils.GetQoveryClient(tokenType, token)
_, _, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client)
if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}
applications, _, err := client.ApplicationsAPI.ListApplication(context.Background(), envId).Execute()
if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}
statuses, _, err := client.EnvironmentMainCallsAPI.GetEnvironmentStatuses(context.Background(), envId).Execute()
if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}
var data [][]string
if jsonFlag {
utils.Println(getAppJsonOutput(applications.GetResults(), statuses))
return
}
for _, application := range applications.GetResults() {
data = append(data, []string{application.Id, application.Name, "Application",
utils.FindStatusTextWithColor(statuses.GetApplications(), application.Id), application.UpdatedAt.String()})
}
err = utils.PrintTable([]string{"Id", "Name", "Type", "Status", "Last Update"}, data)
if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}
},
}
func getAppJsonOutput(applications []qovery.Application, statuses *qovery.EnvironmentStatuses) string {
var results []interface{}
for _, application := range applications {
results = append(results, map[string]interface{}{
"id": application.Id,
"name": application.Name,
"type": "Application",
"status": utils.FindStatus(statuses.GetApplications(), application.Id),
"last_update": application.UpdatedAt.String(),
})
}
j, err := json.Marshal(results)
if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}
return string(j)
}
func init() {
applicationCmd.AddCommand(applicationListCmd)
applicationListCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name")
applicationListCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name")
applicationListCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name")
applicationListCmd.Flags().BoolVarP(&jsonFlag, "json", "", false, "JSON output")
}