mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
Open in editor split $VISUAL and $EDITOR on whitespace, so a Windows path such as "C:\Program Files\Notepad++\notepad++.exe" broke at the space and no editor was found. A user asked to open files in Notepad++, which is not on PATH by default (#538). Double or single quotes now keep a field together. Backslashes stay literal, since Windows paths use them.
244 lines
7.5 KiB
Go
244 lines
7.5 KiB
Go
package server
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"time"
|
|
"unicode"
|
|
|
|
"skillshare/internal/childproc"
|
|
)
|
|
|
|
// openInEditorRequest controls which editor to launch.
|
|
// editor="auto" (or empty) walks a platform-aware fallback chain.
|
|
type openInEditorRequest struct {
|
|
Editor string `json:"editor,omitempty"`
|
|
}
|
|
|
|
type openInEditorResponse struct {
|
|
Editor string `json:"editor"`
|
|
Path string `json:"path"`
|
|
PID int `json:"pid"`
|
|
}
|
|
|
|
// editorCandidate represents an external editor we can try to launch.
|
|
type editorCandidate struct {
|
|
// name is the alias the API accepts (e.g. "code", "cursor").
|
|
name string
|
|
// bin is the executable to spawn.
|
|
bin string
|
|
// args are appended before the file path.
|
|
args []string
|
|
}
|
|
|
|
// knownEditors returns explicit editor aliases the API accepts.
|
|
func knownEditors() map[string]editorCandidate {
|
|
return map[string]editorCandidate{
|
|
"code": {name: "code", bin: "code", args: []string{"--goto"}},
|
|
"cursor": {name: "cursor", bin: "cursor", args: nil},
|
|
"windsurf": {name: "windsurf", bin: "windsurf", args: nil},
|
|
"subl": {name: "subl", bin: "subl", args: nil},
|
|
"sublime": {name: "subl", bin: "subl", args: nil},
|
|
"vim": {name: "vim", bin: "vim", args: nil},
|
|
"nvim": {name: "nvim", bin: "nvim", args: nil},
|
|
"nano": {name: "nano", bin: "nano", args: nil},
|
|
"emacs": {name: "emacs", bin: "emacs", args: nil},
|
|
"idea": {name: "idea", bin: "idea", args: nil},
|
|
"webstorm": {name: "webstorm", bin: "webstorm", args: nil},
|
|
"goland": {name: "goland", bin: "goland", args: nil},
|
|
"textmate": {name: "textmate", bin: "mate", args: nil},
|
|
"mate": {name: "mate", bin: "mate", args: nil},
|
|
"zed": {name: "zed", bin: "zed", args: nil},
|
|
}
|
|
}
|
|
|
|
// handleOpenSkillInEditor launches the configured (or auto-detected) local
|
|
// editor against the skill's canonical markdown file.
|
|
//
|
|
// POST /api/resources/{name}/open-in-editor
|
|
// Body: {"editor": "auto" | "code" | "cursor" | "vim" | ...}
|
|
func (s *Server) handleOpenSkillInEditor(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
|
|
s.mu.RLock()
|
|
source := s.skillsSource()
|
|
agentsSource := s.agentsSource()
|
|
s.mu.RUnlock()
|
|
|
|
name := r.PathValue("name")
|
|
kind := r.URL.Query().Get("kind")
|
|
if kind != "" && kind != "skill" && kind != "agent" {
|
|
writeError(w, http.StatusBadRequest, "invalid kind: "+kind)
|
|
return
|
|
}
|
|
|
|
var req openInEditorRequest
|
|
// Body is optional; silently ignore decode errors when empty.
|
|
if r.Body != nil {
|
|
if err := decodeJSON(w, r, &req, defaultJSONBodyLimit); errors.Is(err, errBodyTooLarge) {
|
|
return
|
|
}
|
|
}
|
|
|
|
targetPath, resolvedKind, err := s.resolveEditableSkillPath(source, agentsSource, name, kind)
|
|
if err != nil {
|
|
writeError(w, http.StatusNotFound, err.Error())
|
|
return
|
|
}
|
|
|
|
// Skills are directories (SKILL.md + supporting files); open the folder
|
|
// so editors like VS Code / Cursor load the whole workspace. Agents are
|
|
// single .md files and stay as-is.
|
|
if resolvedKind == "skill" {
|
|
targetPath = filepath.Dir(targetPath)
|
|
}
|
|
|
|
if os.Getenv("SKILLSHARE_HEADLESS") == "1" {
|
|
writeError(w, http.StatusConflict, "refusing to launch editor: SKILLSHARE_HEADLESS=1")
|
|
return
|
|
}
|
|
|
|
picked, cmd, status, err := launchEditor(req.Editor, targetPath)
|
|
if err != nil {
|
|
writeError(w, status, err.Error())
|
|
return
|
|
}
|
|
|
|
s.writeOpsLog("skill.openInEditor", "ok", start, map[string]any{
|
|
"name": name,
|
|
"kind": resolvedKind,
|
|
"editor": picked,
|
|
"path": targetPath,
|
|
}, "")
|
|
|
|
writeJSON(w, openInEditorResponse{
|
|
Editor: picked,
|
|
Path: targetPath,
|
|
PID: cmd.Process.Pid,
|
|
})
|
|
}
|
|
|
|
// launchEditor opens path in the requested editor, or the one pickEditor finds, without
|
|
// waiting for it. On failure it returns the HTTP status to report.
|
|
func launchEditor(requested, path string) (string, *exec.Cmd, int, error) {
|
|
editor, picked, err := pickEditor(strings.TrimSpace(requested))
|
|
if err != nil {
|
|
return "", nil, http.StatusConflict, fmt.Errorf("no editor available: %w", err)
|
|
}
|
|
|
|
cmd := exec.Command(editor.bin, append(editor.args, path)...) //nolint:gosec // editor choice is explicit
|
|
// Detach from the current process so the server doesn't wait on the editor.
|
|
cmd.Stdin = nil
|
|
cmd.Stdout = nil
|
|
cmd.Stderr = nil
|
|
|
|
if err := childproc.StartDetached(cmd); err != nil {
|
|
return "", nil, http.StatusInternalServerError, fmt.Errorf("failed to launch %s: %w", editor.bin, err)
|
|
}
|
|
// Reap in the background so we don't accumulate zombies on *nix.
|
|
go func() { _ = cmd.Wait() }()
|
|
return picked, cmd, 0, nil
|
|
}
|
|
|
|
// pickEditor returns the editor to launch, honouring an explicit request
|
|
// (e.g. "code") when it resolves, otherwise walking the fallback chain.
|
|
func pickEditor(requested string) (editorCandidate, string, error) {
|
|
if requested != "" && requested != "auto" {
|
|
if cand, ok := knownEditors()[strings.ToLower(requested)]; ok {
|
|
if _, err := exec.LookPath(cand.bin); err == nil {
|
|
return cand, cand.name, nil
|
|
}
|
|
return editorCandidate{}, "", fmt.Errorf("%s not found on PATH", cand.bin)
|
|
}
|
|
// Unknown alias — reject to prevent arbitrary binary execution.
|
|
return editorCandidate{}, "", fmt.Errorf("unsupported editor: %s", requested)
|
|
}
|
|
|
|
// Auto: respect $VISUAL / $EDITOR first.
|
|
for _, env := range []string{"VISUAL", "EDITOR"} {
|
|
if v := strings.TrimSpace(os.Getenv(env)); v != "" {
|
|
// $EDITOR may include flags, e.g. "code --wait".
|
|
head, rest := splitCommand(v)
|
|
if _, err := exec.LookPath(head); err == nil {
|
|
return editorCandidate{name: head, bin: head, args: rest}, head, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
// GUI IDE preferences.
|
|
for _, alias := range []string{"code", "cursor", "zed", "subl", "windsurf", "idea"} {
|
|
if cand, ok := knownEditors()[alias]; ok {
|
|
if _, err := exec.LookPath(cand.bin); err == nil {
|
|
return cand, cand.name, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
// OS native fallback — opens with the user's default handler.
|
|
switch runtime.GOOS {
|
|
case "darwin":
|
|
if _, err := exec.LookPath("open"); err == nil {
|
|
return editorCandidate{name: "open", bin: "open"}, "open", nil
|
|
}
|
|
case "linux":
|
|
if _, err := exec.LookPath("xdg-open"); err == nil {
|
|
return editorCandidate{name: "xdg-open", bin: "xdg-open"}, "xdg-open", nil
|
|
}
|
|
case "windows":
|
|
if _, err := exec.LookPath("rundll32"); err == nil {
|
|
return editorCandidate{name: "rundll32", bin: "rundll32", args: []string{"url.dll,FileProtocolHandler"}}, "rundll32", nil
|
|
}
|
|
}
|
|
|
|
// Terminal fallbacks — nano is friendlier than vim for newcomers.
|
|
for _, alias := range []string{"nano", "vim", "nvim", "emacs"} {
|
|
if cand, ok := knownEditors()[alias]; ok {
|
|
if _, err := exec.LookPath(cand.bin); err == nil {
|
|
return cand, cand.name, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
return editorCandidate{}, "", fmt.Errorf("no usable editor found; set $EDITOR or pass editor=<name>")
|
|
}
|
|
|
|
// splitCommand splits an $EDITOR value into (bin, args...). Double or single quotes keep a
|
|
// field with spaces together, such as "C:\Program Files\Notepad++\notepad++.exe".
|
|
// Backslashes stay literal because Windows paths use them.
|
|
func splitCommand(v string) (string, []string) {
|
|
var fields []string
|
|
var field strings.Builder
|
|
var quote rune
|
|
inField := false
|
|
for _, r := range v {
|
|
switch {
|
|
case quote != 0 && r == quote:
|
|
quote = 0
|
|
case quote == 0 && (r == '"' || r == '\''):
|
|
quote, inField = r, true
|
|
case quote == 0 && unicode.IsSpace(r):
|
|
if inField {
|
|
fields = append(fields, field.String())
|
|
field.Reset()
|
|
inField = false
|
|
}
|
|
default:
|
|
field.WriteRune(r)
|
|
inField = true
|
|
}
|
|
}
|
|
if inField {
|
|
fields = append(fields, field.String())
|
|
}
|
|
if len(fields) == 0 {
|
|
return "", nil
|
|
}
|
|
return fields[0], fields[1:]
|
|
}
|