Skip to content

Commit b6e3651

Browse files
committed
include api for listing directories
1 parent 7b349d7 commit b6e3651

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

cli/web.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ package cli
33
import (
44
"context"
55
"embed"
6+
"encoding/json"
67
"io/fs"
78
"net"
89
"net/http"
910
"os"
11+
"path/filepath"
12+
"slices"
1013

1114
"github.com/go-chi/chi"
1215

@@ -45,6 +48,32 @@ func (r *RootCmd) WebsocketServer() *serpent.Command {
4548
logger := slog.Make(sloghuman.Sink(i.Stderr)).Leveled(slog.LevelDebug)
4649

4750
mux := chi.NewMux()
51+
mux.HandleFunc("/directories", func(rw http.ResponseWriter, r *http.Request) {
52+
entries, err := os.ReadDir(".")
53+
if err != nil {
54+
http.Error(rw, "Could not read directory", http.StatusInternalServerError)
55+
return
56+
}
57+
58+
var dirs []string
59+
for _, entry := range entries {
60+
if entry.IsDir() {
61+
subentries, err := os.ReadDir(entry.Name())
62+
if err != nil {
63+
continue
64+
}
65+
if !slices.ContainsFunc(subentries, func(entry fs.DirEntry) bool {
66+
return filepath.Ext(entry.Name()) == ".tf"
67+
}) {
68+
continue
69+
}
70+
dirs = append(dirs, entry.Name())
71+
}
72+
}
73+
rw.Header().Set("Content-Type", "application/json")
74+
rw.WriteHeader(http.StatusOK)
75+
_ = json.NewEncoder(rw).Encode(dirs)
76+
})
4877
mux.HandleFunc("/ws/{dir}", websocketHandler(logger))
4978

5079
staticDir, err := fs.Sub(static, "static")

0 commit comments

Comments
 (0)