Update main.go
This commit is contained in:
38
main.go
38
main.go
@@ -62,7 +62,8 @@ type openResponse 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 {
|
||||
@@ -84,7 +85,10 @@ func newConfigStore() (*configStore, error) {
|
||||
func (s *configStore) Snapshot() config.Config {
|
||||
s.mu.RLock()
|
||||
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 {
|
||||
@@ -292,7 +296,11 @@ func main() {
|
||||
case http.MethodGet:
|
||||
cfg := configStore.Snapshot()
|
||||
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
|
||||
case http.MethodPost, http.MethodPut:
|
||||
dec := json.NewDecoder(http.MaxBytesReader(w, r.Body, 128*1024))
|
||||
@@ -309,12 +317,18 @@ func main() {
|
||||
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()})
|
||||
return
|
||||
}
|
||||
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
|
||||
default:
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
@@ -372,10 +386,16 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
if err := openfolder.OpenLocation(target); err != nil {
|
||||
errLog.Printf("/open open-failed method=%s path=%q normalized=%q err=%v dur=%s", r.Method, rawPath, target, err, time.Since(start))
|
||||
notify.Show("luxtools-client", fmt.Sprintf("Failed to open: %s (%v)", target, err))
|
||||
writeJSON(w, http.StatusInternalServerError, openResponse{OK: false, Message: err.Error()})
|
||||
var openErr error
|
||||
if customCmd := configStore.Snapshot().OpenLocationCommand; customCmd != "" {
|
||||
openErr = openfolder.OpenLocationCustom(customCmd, target)
|
||||
} 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
|
||||
}
|
||||
infoLog.Printf("/open opened method=%s path=%q normalized=%q dur=%s", r.Method, rawPath, target, time.Since(start))
|
||||
|
||||
Reference in New Issue
Block a user