Added delete functionality

This commit is contained in:
2026-06-06 16:39:48 -05:00
parent 3a01e835a1
commit 0e4d96b1be
3 changed files with 90 additions and 2 deletions

View File

@@ -45,6 +45,7 @@ func StartDaemon(cfg *AppConfig, configPath string) error {
mux.HandleFunc("/instances/command", ds.handleCommand)
mux.HandleFunc("/instances/list", ds.handleList)
mux.HandleFunc("/instances/logs", ds.handleGetLogs)
mux.HandleFunc("/instances/delete", ds.handleDelete)
corsWrappedHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
@@ -389,3 +390,75 @@ func (ds *DaemonServer) handleGetLogs(w http.ResponseWriter, r *http.Request) {
http.Error(w, fmt.Sprintf("Failed encoding log matrix: %v", err), http.StatusInternalServerError)
}
}
func (ds *DaemonServer) handleDelete(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
name := r.URL.Query().Get("name")
if name == "" {
http.Error(w, "Missing name parameter", http.StatusBadRequest)
return
}
purge := false
purgeParam := r.URL.Query().Get("purge")
if purgeParam != "" {
result, err := strconv.ParseBool(purgeParam)
if err != nil {
http.Error(w, "Invalid value for 'purge' parameter", http.StatusBadRequest)
return
}
purge = result
}
if ds.reloadConfig() != nil {
http.Error(w, "Could not reload config", http.StatusInternalServerError)
return
}
ds.Lock()
defer ds.Unlock()
ds.procManager.RLock()
defer ds.procManager.RUnlock()
_, exists := ds.cfg.Instances[name]
if !exists {
http.Error(w, fmt.Sprintf("Cannot delete instance '%s'; Does not exist.", name), http.StatusBadRequest)
return
}
if _, running := ds.procManager.ActiveInstances[name]; running {
http.Error(w, fmt.Sprintf("Cannot delete instance '%s'; It is currently running.", name), http.StatusBadRequest)
return
}
delete(ds.cfg.Instances, name)
data, err := json.MarshalIndent(ds.cfg, "", " ")
if err != nil {
http.Error(w, fmt.Sprintf("Failed processing profile adjustments: %v", err), http.StatusInternalServerError)
return
}
if err := os.WriteFile(ds.configPath, data, 0644); err != nil {
http.Error(w, fmt.Sprintf("Failed saving configuration adjustments to disk: %v", err), http.StatusInternalServerError)
return
}
if purge {
path := filepath.Join(ds.cfg.Storage.InstancesDir, name)
err := os.RemoveAll(path)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to delete instance from disk: %v", err), http.StatusInternalServerError)
return
}
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Successfully deleted instance %s", name)
}