MCPcopy Create free account
hub / github.com/diillson/chatcli / handleExportCommand

Method handleExportCommand

cli/export_command.go:28–66  ·  view source on GitHub ↗
(input string)

Source from the content-addressed store, hash-verified

26)
27
28func (cli *ChatCLI) handleExportCommand(input string) {
29 if len(cli.history) == 0 {
30 fmt.Println(colorize(" "+i18n.T("export.empty"), ColorGray))
31 return
32 }
33
34 path := strings.TrimSpace(strings.TrimPrefix(input, "/export"))
35 if path == "" {
36 dir, err := defaultExportDir()
37 if err != nil {
38 fmt.Printf(" %s %v\n", colorize("ERR", ColorRed), err)
39 return
40 }
41 path = filepath.Join(dir, fmt.Sprintf("trajectory-%s.jsonl", time.Now().Format("20060102-150405")))
42 }
43 if expanded, err := utils.ExpandPath(path); err == nil {
44 path = expanded
45 }
46
47 if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
48 fmt.Printf(" %s %v\n", colorize("ERR", ColorRed), err)
49 return
50 }
51
52 f, err := os.Create(path) //#nosec G304 -- user-provided export path
53 if err != nil {
54 fmt.Printf(" %s %v\n", colorize("ERR", ColorRed), err)
55 return
56 }
57 defer func() { _ = f.Close() }()
58
59 n, err := trajectory.WriteJSONL(f, cli.history)
60 if err != nil {
61 fmt.Printf(" %s %v\n", colorize("ERR", ColorRed), err)
62 return
63 }
64
65 fmt.Printf(" %s %s\n", colorize("OK", ColorGreen), i18n.T("export.done", n, path))
66}
67
68func defaultExportDir() (string, error) {
69 home, err := utils.GetHomeDir()

Callers 1

buildRoutesMethod · 0.80

Calls 8

TFunction · 0.92
ExpandPathFunction · 0.92
WriteJSONLFunction · 0.92
colorizeFunction · 0.85
defaultExportDirFunction · 0.85
CreateMethod · 0.65
CloseMethod · 0.65
DirMethod · 0.45

Tested by

no test coverage detected