mirror of
https://github.com/anotherhadi/jwt-tui.git
synced 2026-06-26 09:12:33 +02:00
f3dce1f4ab
Signed-off-by: Hadi <112569860+anotherhadi@users.noreply.github.com>
48 lines
962 B
Go
48 lines
962 B
Go
package util
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
|
|
tea "charm.land/bubbletea/v2"
|
|
)
|
|
|
|
type EditorFinishedMsg struct {
|
|
Content string
|
|
Err error
|
|
}
|
|
|
|
func OpenExternalEditor(content string) tea.Cmd {
|
|
editor := os.Getenv("EDITOR")
|
|
if editor == "" {
|
|
editor = os.Getenv("VISUAL")
|
|
}
|
|
if editor == "" {
|
|
editor = "vi"
|
|
}
|
|
|
|
f, err := os.CreateTemp("", "jwt-tui-*.json")
|
|
if err != nil {
|
|
return func() tea.Msg { return EditorFinishedMsg{Err: err} }
|
|
}
|
|
tmpPath := f.Name()
|
|
if _, werr := f.WriteString(content); werr != nil {
|
|
f.Close()
|
|
os.Remove(tmpPath)
|
|
return func() tea.Msg { return EditorFinishedMsg{Err: werr} }
|
|
}
|
|
f.Close()
|
|
|
|
return tea.ExecProcess(exec.Command(editor, tmpPath), func(err error) tea.Msg {
|
|
defer os.Remove(tmpPath)
|
|
if err != nil {
|
|
return EditorFinishedMsg{Err: err}
|
|
}
|
|
data, readErr := os.ReadFile(tmpPath)
|
|
if readErr != nil {
|
|
return EditorFinishedMsg{Err: readErr}
|
|
}
|
|
return EditorFinishedMsg{Content: string(data)}
|
|
})
|
|
}
|