1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
package tui
import (
"context"
"fmt"
"log"
"os"
"os/exec"
"time"
config "codeberg.org/snonux/gos/internal/config/client"
tea "github.com/charmbracelet/bubbletea"
)
type composePostAction int
const (
noPostAction composePostAction = iota
queueAfterCompose
submitAfterCompose
)
func composeActionCmd(ctx context.Context, conf config.ClientConfig, postAction composePostAction) tea.Cmd {
err := ensureDirectoryExists(conf.DataDir)
composeFile := fmt.Sprintf("%s/%s", conf.DataDir, conf.ComposeFile)
log.Println("Composing", composeFile)
return openEditorCmd(conf.Editor, composeFile, func() error {
if err != nil {
return err
}
switch postAction {
case submitAfterCompose:
return submitEntry(ctx, conf, composeFile)
case queueAfterCompose:
timestamp := time.Now().Format("20060102-150405")
queuedFile := fmt.Sprintf("%s/queued-%s.txt", conf.DataDir, timestamp)
return os.Rename(composeFile, queuedFile)
}
return nil
})
}
func openEditorCmd(editor, filePath string, cb func() error) tea.Cmd {
return tea.ExecProcess(exec.Command(editor, filePath), func(err error) tea.Msg {
return finishedMsg{
cb: cb,
err: 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)
}
|