Skip to content

Commit 2e819dc

Browse files
committed
compose delete
1 parent 6ac4130 commit 2e819dc

File tree

2 files changed

+101
-3
lines changed

2 files changed

+101
-3
lines changed

pkg/koyeb/compose.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func NewComposeCmd() *cobra.Command {
5959
cmd.Flags().BoolP("verbose", "v", false, "Tails service logs to have more information about your deployment.")
6060

6161
cmd.AddCommand(NewComposeLogsCmd())
62+
cmd.AddCommand(NewComposeDeleteCmd())
6263

6364
return cmd
6465
}
@@ -128,12 +129,25 @@ func (h *KoyebComposeHandler) Compose(ctx *CLIContext, koyebCompose *KoyebCompos
128129
}
129130
}
130131

131-
log.Infof("\nYour app %v has been succesfully deployed 🚀", appName)
132+
log.Infof("Your app %v has been succesfully deployed 🚀", appName)
132133

133134
return nil
134135
}
135136

136-
func (h *KoyebComposeHandler) isMonitoringEndState(status koyeb.DeploymentStatus) bool {
137+
func isAppMonitoringEndState(status koyeb.AppStatus) bool {
138+
endStates := []koyeb.AppStatus{
139+
koyeb.APPSTATUS_DELETED,
140+
}
141+
142+
for _, endState := range endStates {
143+
if status == endState {
144+
return true
145+
}
146+
}
147+
return false
148+
}
149+
150+
func (h *KoyebComposeHandler) isDeploymentMonitoringEndState(status koyeb.DeploymentStatus) bool {
137151
endStates := []koyeb.DeploymentStatus{
138152
koyeb.DEPLOYMENTSTATUS_HEALTHY,
139153
koyeb.DEPLOYMENTSTATUS_DEGRADED,
@@ -238,13 +252,14 @@ func (h *KoyebComposeHandler) MonitorService(ctx *CLIContext, deploymentId, serv
238252
}
239253
}
240254

241-
if h.isMonitoringEndState(currentStatus) {
255+
if h.isDeploymentMonitoringEndState(currentStatus) {
242256
break
243257
}
244258

245259
time.Sleep(5 * time.Second)
246260
}
247261

262+
fmt.Printf("\n")
248263
if previousStatus == koyeb.DEPLOYMENTSTATUS_HEALTHY {
249264
log.Infof("Succcessfully deployed %v ✅", serviceName)
250265
} else {

pkg/koyeb/compose_delete.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package koyeb
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"github.com/briandowns/spinner"
8+
"github.com/koyeb/koyeb-api-client-go/api/v1/koyeb"
9+
"github.com/koyeb/koyeb-cli/pkg/koyeb/errors"
10+
log "github.com/sirupsen/logrus"
11+
"github.com/spf13/cobra"
12+
)
13+
14+
func NewComposeDeleteCmd() *cobra.Command {
15+
cmd := &cobra.Command{
16+
Use: "delete",
17+
Short: "d",
18+
Args: cobra.ExactArgs(1),
19+
RunE: WithCLIContext(func(ctx *CLIContext, cmd *cobra.Command, args []string) error {
20+
composeFile, err := parseComposeFile(args[0])
21+
if err != nil {
22+
return err
23+
}
24+
25+
if composeFile == nil {
26+
return nil
27+
}
28+
29+
appList, _, err := ctx.Client.AppsApi.ListApps(ctx.Context).Name(*composeFile.Name).Execute()
30+
if err != nil {
31+
return err
32+
}
33+
if !appList.HasApps() || len(appList.Apps) == 0 {
34+
return nil
35+
}
36+
37+
if _, _, err := ctx.Client.AppsApi.DeleteApp(ctx.Context, appList.Apps[0].GetId()).Execute(); err != nil {
38+
return err
39+
}
40+
41+
return monitorAppDelete(ctx, appList.Apps[0].GetId())
42+
}),
43+
}
44+
45+
return cmd
46+
}
47+
48+
func monitorAppDelete(ctx *CLIContext, appId string) error {
49+
s := spinner.New(spinner.CharSets[21], 100*time.Millisecond, spinner.WithColor("red"))
50+
s.Start()
51+
defer s.Stop()
52+
53+
previousStatus := koyeb.AppStatus("")
54+
for {
55+
resApp, resp, err := ctx.Client.AppsApi.GetApp(ctx.Context, appId).Execute()
56+
if err != nil {
57+
return errors.NewCLIErrorFromAPIError(
58+
"Error while fetching deployment status",
59+
err,
60+
resp,
61+
)
62+
}
63+
64+
currentStatus := resApp.App.GetStatus()
65+
if previousStatus != currentStatus {
66+
previousStatus = currentStatus
67+
s.Suffix = fmt.Sprintf(" Deleting app %s: %s", *resApp.App.Name, currentStatus)
68+
}
69+
70+
if isAppMonitoringEndState(currentStatus) {
71+
break
72+
}
73+
74+
time.Sleep(5 * time.Second)
75+
}
76+
77+
fmt.Printf("\n")
78+
if previousStatus == koyeb.APPSTATUS_DELETED {
79+
log.Infof("Succcessfully deleted ✅♻️")
80+
}
81+
82+
return nil
83+
}

0 commit comments

Comments
 (0)