]> git.giacomolli.uk - onechat/blob - main.go
Import WhatsApp exports before live messages
[onechat] / main.go
1 package main
2
3 import (
4         "context"
5         "fmt"
6         "os"
7         "os/signal"
8         "strings"
9         "syscall"
10 )
11
12 // usage is the help text of the command.
13 const usage = `Usage: onechat [COMMAND]
14
15 Commands:
16   chat                     talk to the agent in this terminal (default)
17   daemon                   run the agent, Telegram, and WhatsApp
18   import [-n] ZIP...        import WhatsApp history (stop the daemon first)
19     -n                     print the plan without model calls
20   login                    log in to ChatGPT with a device code
21   probe LOG COUNT [BUILDS] measure the compactor on a log, with real calls
22   help                     show this help
23
24 Chat needs a running daemon. ONECHAT_DIR sets the data directory.
25 `
26
27 func main() {
28         err := run()
29         if err != nil {
30                 fmt.Fprintln(os.Stderr, "onechat:", err)
31                 os.Exit(1)
32         }
33 }
34
35 func run() error {
36         ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
37         defer cancel()
38
39         args := os.Args[1:]
40         switch {
41         case len(args) == 1 && (args[0] == "help" || args[0] == "--help" || args[0] == "-h"):
42                 _, err := fmt.Print(usage)
43                 if err != nil {
44                         return err
45                 }
46         case len(args) == 1 && args[0] == "login":
47                 err := chatgptLogin(ctx, os.Stdout)
48                 if err != nil {
49                         return err
50                 }
51         // A signal is the normal stop of the daemon and the client, so their
52         // result does not include ctx.Err().
53         case len(args) == 1 && args[0] == "daemon":
54                 return daemon(ctx, os.Stdout)
55         case len(args) == 0 || len(args) == 1 && args[0] == "chat":
56                 return terminal(ctx, os.Stdin, os.Stdout)
57         case len(args) > 0 && args[0] == "import":
58                 return whatsappImport(ctx, args[1:], os.Stdout)
59         case len(args) > 0 && args[0] == "probe":
60                 err := probe(ctx, args[1:], os.Stdout)
61                 if err != nil {
62                         return err
63                 }
64         default:
65                 return fmt.Errorf("unknown command %q\n\n%s", strings.Join(args, " "), strings.TrimSuffix(usage, "\n"))
66         }
67
68         return ctx.Err()
69 }