Update main.go

This commit is contained in:
2026-03-28 12:47:38 +01:00
parent c5695af7fd
commit befa9795c1

38
main.go
View File

@@ -62,7 +62,8 @@ type openResponse struct {
} }
type settingsConfig struct { type settingsConfig struct {
PathMap map[string]string `json:"path_map"` PathMap map[string]string `json:"path_map"`
OpenLocationCommand string `json:"open_location_command"`
} }
type configStore struct { type configStore struct {
@@ -84,7 +85,10 @@ func newConfigStore() (*configStore, error) {
func (s *configStore) Snapshot() config.Config { func (s *configStore) Snapshot() config.Config {
s.mu.RLock() s.mu.RLock()
defer s.mu.RUnlock() defer s.mu.RUnlock()
return config.Config{PathMap: clonePathMap(s.cfg.PathMap)} return config.Config{
PathMap: clonePathMap(s.cfg.PathMap),
OpenLocationCommand: s.cfg.OpenLocationCommand,
}
} }
func (s *configStore) Update(cfg config.Config) error { func (s *configStore) Update(cfg config.Config) error {
@@ -292,7 +296,11 @@ func main() {
case http.MethodGet: case http.MethodGet:
cfg := configStore.Snapshot() cfg := configStore.Snapshot()
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{"ok": true, "path_map": cfg.PathMap}) _ = json.NewEncoder(w).Encode(map[string]any{
"ok": true,
"path_map": cfg.PathMap,
"open_location_command": cfg.OpenLocationCommand,
})
return return
case http.MethodPost, http.MethodPut: case http.MethodPost, http.MethodPut:
dec := json.NewDecoder(http.MaxBytesReader(w, r.Body, 128*1024)) dec := json.NewDecoder(http.MaxBytesReader(w, r.Body, 128*1024))
@@ -309,12 +317,18 @@ func main() {
return return
} }
if err := configStore.Update(config.Config{PathMap: pathMap}); err != nil { openCmd := strings.TrimSpace(req.OpenLocationCommand)
if err := configStore.Update(config.Config{PathMap: pathMap, OpenLocationCommand: openCmd}); err != nil {
writeJSON(w, http.StatusInternalServerError, openResponse{OK: false, Message: err.Error()}) writeJSON(w, http.StatusInternalServerError, openResponse{OK: false, Message: err.Error()})
return return
} }
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{"ok": true, "path_map": pathMap}) _ = json.NewEncoder(w).Encode(map[string]any{
"ok": true,
"path_map": pathMap,
"open_location_command": openCmd,
})
return return
default: default:
w.WriteHeader(http.StatusMethodNotAllowed) w.WriteHeader(http.StatusMethodNotAllowed)
@@ -372,10 +386,16 @@ func main() {
return return
} }
if err := openfolder.OpenLocation(target); err != nil { var openErr error
errLog.Printf("/open open-failed method=%s path=%q normalized=%q err=%v dur=%s", r.Method, rawPath, target, err, time.Since(start)) if customCmd := configStore.Snapshot().OpenLocationCommand; customCmd != "" {
notify.Show("luxtools-client", fmt.Sprintf("Failed to open: %s (%v)", target, err)) openErr = openfolder.OpenLocationCustom(customCmd, target)
writeJSON(w, http.StatusInternalServerError, openResponse{OK: false, Message: err.Error()}) } else {
openErr = openfolder.OpenLocation(target)
}
if openErr != nil {
errLog.Printf("/open open-failed method=%s path=%q normalized=%q err=%v dur=%s", r.Method, rawPath, target, openErr, time.Since(start))
notify.Show("luxtools-client", fmt.Sprintf("Failed to open: %s (%v)", target, openErr))
writeJSON(w, http.StatusInternalServerError, openResponse{OK: false, Message: openErr.Error()})
return return
} }
infoLog.Printf("/open opened method=%s path=%q normalized=%q dur=%s", r.Method, rawPath, target, time.Since(start)) infoLog.Printf("/open opened method=%s path=%q normalized=%q dur=%s", r.Method, rawPath, target, time.Since(start))