add /open endpoint: open files with EDITOR or xdg-open

This commit is contained in:
Levi Neely 2026-04-26 15:54:12 +02:00
parent ed0cce92f1
commit 292448c05d
1 changed files with 22 additions and 0 deletions

22
main.go
View File

@ -191,6 +191,11 @@ func writeJSON(w http.ResponseWriter, status int, v any) {
}
func handler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/open" {
handleOpen(w, r)
return
}
fsys, err := dial()
if err != nil {
writeJSON(w, http.StatusBadGateway, map[string]string{"error": "9p connect: " + err.Error()})
@ -424,6 +429,23 @@ func handleStat(w http.ResponseWriter, fsys *client.Fsys, p string) {
w.Header().Set("Content-Type", "application/json")
}
func handleOpen(w http.ResponseWriter, r *http.Request) {
p := r.URL.Query().Get("path")
if p == "" {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "missing path"})
return
}
editor := os.Getenv("EDITOR")
if editor == "" {
editor = "xdg-open"
}
if err := exec.Command(editor, p).Start(); err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
}
writeJSON(w, http.StatusOK, map[string]string{"opened": p})
}
func handlePut(w http.ResponseWriter, r *http.Request, fsys *client.Fsys, p string) {
body, err := io.ReadAll(r.Body)
if err != nil {