Skip to content

Commit 821c0b2

Browse files
Implement delete operation
1 parent d1390c1 commit 821c0b2

File tree

3 files changed

+67
-10
lines changed

3 files changed

+67
-10
lines changed

internal/api/api.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,11 @@ func NewHTTPRouter(dockerClient *dockerClient.Client) http.Handler {
6969
}
7070
})
7171

72+
deletehandler := handlers.HandleAppDelete()
73+
mux.HandleFunc("DELETE /v1/apps/{path...}", func(w http.ResponseWriter, r *http.Request) {
74+
path := r.PathValue("path")
75+
deletehandler(w, r, orchestrator.ID(path))
76+
})
77+
7278
return mux
7379
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package handlers
2+
3+
import (
4+
"log/slog"
5+
"net/http"
6+
7+
"github.com/arduino/arduino-app-cli/internal/orchestrator"
8+
"github.com/arduino/arduino-app-cli/pkg/parser"
9+
"github.com/arduino/arduino-app-cli/pkg/render"
10+
)
11+
12+
func HandleAppDelete() HandlerAppFunc {
13+
return func(w http.ResponseWriter, r *http.Request, id orchestrator.ID) {
14+
if id == "" {
15+
render.EncodeResponse(w, http.StatusPreconditionFailed, "id must be set")
16+
return
17+
}
18+
appPath, err := id.ToPath()
19+
if err != nil {
20+
render.EncodeResponse(w, http.StatusPreconditionFailed, "invalid id")
21+
return
22+
}
23+
24+
app, err := parser.Load(appPath.String())
25+
if err != nil {
26+
slog.Error("Unable to parse the app.yaml", slog.String("error", err.Error()), slog.String("path", string(id)))
27+
render.EncodeResponse(w, http.StatusInternalServerError, "unable to find the app")
28+
return
29+
}
30+
31+
err = orchestrator.DeleteApp(r.Context(), app)
32+
if err != nil {
33+
slog.Error("Unable to delete the app", slog.String("error", err.Error()))
34+
render.EncodeResponse(w, http.StatusInternalServerError, "unable to delete the app")
35+
return
36+
}
37+
render.EncodeResponse(w, http.StatusOK, nil)
38+
}
39+
}

internal/orchestrator/orchestrator.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -185,16 +185,19 @@ func StopApp(ctx context.Context, app parser.App) iter.Seq[StreamMessage] {
185185
return
186186
}
187187
mainCompose := provisioningStateDir.Join("app-compose.yaml")
188-
process, err := paths.NewProcess(nil, "docker", "compose", "-f", mainCompose.String(), "stop")
189-
if err != nil {
190-
yield(StreamMessage{error: err})
191-
return
192-
}
193-
process.RedirectStderrTo(callbackWriter)
194-
process.RedirectStdoutTo(callbackWriter)
195-
if err := process.RunWithinContext(ctx); err != nil {
196-
yield(StreamMessage{error: err})
197-
return
188+
// In case the app was never started
189+
if mainCompose.Exist() {
190+
process, err := paths.NewProcess(nil, "docker", "compose", "-f", mainCompose.String(), "stop")
191+
if err != nil {
192+
yield(StreamMessage{error: err})
193+
return
194+
}
195+
process.RedirectStderrTo(callbackWriter)
196+
process.RedirectStdoutTo(callbackWriter)
197+
if err := process.RunWithinContext(ctx); err != nil {
198+
yield(StreamMessage{error: err})
199+
return
200+
}
198201
}
199202
}
200203
_ = yield(StreamMessage{progress: &Progress{Name: "", Progress: 100.0}})
@@ -479,6 +482,15 @@ func CloneApp(ctx context.Context, req CloneAppRequest) (response CloneAppRespon
479482
return CloneAppResponse{ID: id}, nil
480483
}
481484

485+
func DeleteApp(ctx context.Context, app parser.App) error {
486+
for msg := range StopApp(ctx, app) {
487+
if msg.error != nil {
488+
return fmt.Errorf("failed to stop app: %w", msg.error)
489+
}
490+
}
491+
return app.FullPath.RemoveAll()
492+
}
493+
482494
func getCurrentUser() string {
483495
// MacOS and Windows uses a VM so we don't need to map the user.
484496
if runtime.GOOS == "darwin" || runtime.GOOS == "windows" {

0 commit comments

Comments
 (0)