Files
Reims-Go/internal/control/control.go
T

51 lines
1.3 KiB
Go

package control
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"os"
"path/filepath"
"runtime"
"strings"
)
const MaxFrameBytes = 8 << 20
type Request struct {
PluginID string `json:"plugin_id,omitempty"`
Channel string `json:"channel,omitempty"`
Message json.RawMessage `json:"message,omitempty"`
RequestID json.RawMessage `json:"request_id,omitempty"`
CloseInput bool `json:"close_input,omitempty"`
}
type Response struct {
RequestID json.RawMessage `json:"request_id,omitempty"`
Message json.RawMessage `json:"message,omitempty"`
Error string `json:"error,omitempty"`
}
type Handler func(ctx context.Context, pluginID, channel string, message json.RawMessage) (json.RawMessage, error)
// PipeName gives the desktop process and its CLI clients a deterministic
// endpoint scoped to this installation path and Windows user.
func PipeName() string {
exe, err := os.Executable()
if err != nil {
exe = "reims-go"
}
return pipeNameForExecutable(exe)
}
func pipeNameForExecutable(exe string) string {
identity := filepath.Clean(exe)
if runtime.GOOS == "windows" {
identity = strings.ToLower(identity)
}
sum := sha256.Sum256([]byte(identity))
userScope := currentUserScope()
return `\\.\pipe\reims-go-control-` + hex.EncodeToString(sum[:8]) + "-" + userScope
}