summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-06-23 10:52:22 +0300
committerPaul Buetow <paul@buetow.org>2024-06-23 10:52:22 +0300
commit639e8f297241e760f1ac003d53a57161bac792e9 (patch)
treee9d48dfb6bc8f8ebcafe9044db3490f2bde48d1b /internal
parentf455a5355a404e24c06665c8092586f892e8baa5 (diff)
can open compose.md in editor
Diffstat (limited to 'internal')
-rw-r--r--internal/client/tui/editor.go22
-rw-r--r--internal/client/tui/tui.go3
-rw-r--r--internal/config/client/client.go14
3 files changed, 32 insertions, 7 deletions
diff --git a/internal/client/tui/editor.go b/internal/client/tui/editor.go
index 1d17aa7..85bcff5 100644
--- a/internal/client/tui/editor.go
+++ b/internal/client/tui/editor.go
@@ -1,16 +1,34 @@
package tui
import (
+ "fmt"
+ "os"
"os/exec"
+ "path/filepath"
tea "github.com/charmbracelet/bubbletea"
)
type editorFinishedMsg struct{ err error }
-func openEditor(editor string) tea.Cmd {
- c := exec.Command(editor) //nolint:gosec
+func openEditor(editor, filePath string) tea.Cmd {
+ // Maybe handle the error explicitly? Should be obvious anyway
+ // when the editor can't open the file because directory isn't there
+ _ = ensureDirectoryExists(filepath.Dir(filePath))
+
+ c := exec.Command(editor, filePath) //nolint:gosec
return tea.ExecProcess(c, func(err error) tea.Msg {
return editorFinishedMsg{err}
})
}
+
+func ensureDirectoryExists(dir string) error {
+ info, err := os.Stat(dir)
+ if err != nil && os.IsNotExist(err) {
+ return os.MkdirAll(dir, os.ModePerm)
+ }
+ if info.IsDir() {
+ return nil
+ }
+ return fmt.Errorf("path %s is not a directory", dir)
+}
diff --git a/internal/client/tui/tui.go b/internal/client/tui/tui.go
index 54bf63c..4a27caa 100644
--- a/internal/client/tui/tui.go
+++ b/internal/client/tui/tui.go
@@ -60,7 +60,8 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case "enter":
switch m.cursor {
case composePostCursorPos:
- return m, openEditor(m.conf.Editor)
+ tmpFile := fmt.Sprintf("%s/compose.md", m.conf.DataDir)
+ return m, openEditor(m.conf.Editor, tmpFile)
}
case "a":
diff --git a/internal/config/client/client.go b/internal/config/client/client.go
index 65dd246..e3a5969 100644
--- a/internal/config/client/client.go
+++ b/internal/config/client/client.go
@@ -1,24 +1,30 @@
package client
import (
+ "fmt"
+ "os"
"strings"
"codeberg.org/snonux/gos/internal/config"
)
type ClientConfig struct {
- Server string `json:"Partner,omitempty"`
- APIKey string `json:"APIKey,omitempty"`
- Editor string `json:"Editor,omitempty"`
+ Server string `json:"Partner,omitempty"`
+ APIKey string `json:"APIKey,omitempty"`
+ Editor string `json:"Editor,omitempty"`
+ DataDir string `json:"StateDir,omitempty"`
}
func New(configFile string) (ClientConfig, error) {
conf, _ := config.FromFile[ClientConfig](configFile)
- // TODO: Refactor
+
conf.Server = config.FromENV("GOS_SERVERS", conf.Server)
conf.APIKey = config.FromENV("GOS_API_KEY", conf.APIKey)
conf.Editor = config.FromENV("GOS_EDITOR", "EDITOR", conf.Editor, "vi")
+ defaultDataDir := fmt.Sprintf("%s/.gos/data", os.Getenv("HOME"))
+ conf.DataDir = config.FromENV("GOS_DATA_DIR", conf.DataDir, defaultDataDir)
+
return conf, nil
}