-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathapp.go
More file actions
156 lines (128 loc) · 3.53 KB
/
Copy pathapp.go
File metadata and controls
156 lines (128 loc) · 3.53 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package cmd
import (
"strings"
"github.com/onkernel/cli/pkg/util"
"github.com/onkernel/kernel-go-sdk"
"github.com/pterm/pterm"
"github.com/samber/lo"
"github.com/spf13/cobra"
)
var appCmd = &cobra.Command{
Use: "app",
Aliases: []string{"apps"},
Short: "Manage deployed applications",
Long: "Commands for managing deployed Kernel applications",
}
// --- app list subcommand
var appListCmd = &cobra.Command{
Use: "list",
Short: "List deployed application versions",
RunE: runAppList,
}
// --- app history subcommand (scaffold)
var appHistoryCmd = &cobra.Command{
Use: "history <app_name>",
Short: "Show deployment history for an application",
Args: cobra.ExactArgs(1),
RunE: runAppHistory,
}
func init() {
// register subcommands under app
appCmd.AddCommand(appListCmd)
appCmd.AddCommand(appHistoryCmd)
// Add optional filters for list
appListCmd.Flags().String("name", "", "Filter by application name")
appListCmd.Flags().String("version", "", "Filter by version label")
// Limit rows returned for app history (0 = all)
appHistoryCmd.Flags().Int("limit", 100, "Max deployments to return (default 100)")
}
func runAppList(cmd *cobra.Command, args []string) error {
client := getKernelClient(cmd)
appName, _ := cmd.Flags().GetString("name")
version, _ := cmd.Flags().GetString("version")
pterm.Debug.Println("Fetching deployed applications...")
params := kernel.AppListParams{}
if appName != "" {
params.AppName = kernel.Opt(appName)
}
if version != "" {
params.Version = kernel.Opt(version)
}
apps, err := client.Apps.List(cmd.Context(), params)
if err != nil {
pterm.Error.Printf("Failed to list applications: %v\n", err)
return nil
}
if apps == nil || len(*apps) == 0 {
pterm.Info.Println("No applications found")
return nil
}
// Prepare table data
tableData := pterm.TableData{
{"App Name", "Version", "App Version ID", "Region", "Actions", "Env Vars"},
}
for _, app := range *apps {
// Format env vars
envVarsStr := "-"
if len(app.EnvVars) > 0 {
envVarsStr = strings.Join(lo.Keys(app.EnvVars), ", ")
}
actionsStr := "-"
if len(app.Actions) > 0 {
actionsStr = strings.Join(lo.Map(app.Actions, func(a kernel.AppAction, _ int) string {
return a.Name
}), ", ")
}
tableData = append(tableData, []string{
app.AppName,
app.Version,
app.ID,
string(app.Region),
actionsStr,
envVarsStr,
})
}
PrintTableNoPad(tableData, true)
return nil
}
func runAppHistory(cmd *cobra.Command, args []string) error {
client := getKernelClient(cmd)
appName := args[0]
lim, _ := cmd.Flags().GetInt("limit")
pterm.Debug.Printf("Fetching deployment history for app '%s'...\n", appName)
params := kernel.DeploymentListParams{}
if appName != "" {
params.AppName = kernel.Opt(appName)
}
deployments, err := client.Deployments.List(cmd.Context(), params)
if err != nil {
pterm.Error.Printf("Failed to list deployments: %v\n", err)
return nil
}
if deployments == nil || len(deployments.Items) == 0 {
pterm.Info.Println("No deployments found for this application")
return nil
}
tableData := pterm.TableData{
{"Deployment ID", "Created At", "Region", "Status", "Entrypoint", "Reason"},
}
rows := 0
for _, dep := range deployments.Items {
created := util.FormatLocal(dep.CreatedAt)
status := string(dep.Status)
tableData = append(tableData, []string{
dep.ID,
created,
string(dep.Region),
status,
dep.EntrypointRelPath,
dep.StatusReason,
})
rows++
if lim > 0 && rows >= lim {
break
}
}
PrintTableNoPad(tableData, true)
return nil
}