]> git.giacomolli.uk - onechat/blobdiff - main.go
Format short daemon events with slog
[onechat] / main.go
diff --git a/main.go b/main.go
index 680c58d9a1c3884b5f8bab7bc8ef04d3874ee12c..a73407f7c8457e973f0fb7731312a139acb7c3d2 100644 (file)
--- a/main.go
+++ b/main.go
@@ -1,3 +1,4 @@
+// Command onechat runs the agent daemon and its terminal client.
 package main
 
 import (
@@ -9,6 +10,25 @@ import (
        "syscall"
 )
 
+// usage is the help text of the command.
+const usage = `Usage: onechat [COMMAND]
+
+Commands:
+  chat                     talk to the agent in this terminal (default)
+  daemon                   run the agent, Telegram, and WhatsApp
+  import [-n] ZIP...        import WhatsApp history (stop the daemon first)
+    -n                     print the plan without model calls
+  login                    log in to ChatGPT with a device code
+  probe LOG COUNT [BUILDS] measure the compactor on a log, with real calls
+  version                  show the build version
+  help                     show this help
+
+Chat needs a running daemon. ONECHAT_DIR sets the data directory.
+`
+
+// version is set by the package build and stays dev for direct Go builds.
+var version = "dev"
+
 func main() {
        err := run()
        if err != nil {
@@ -23,33 +43,37 @@ func run() error {
 
        args := os.Args[1:]
        switch {
-       case len(args) == 0:
-               _, err := fmt.Println("onechat")
-               if err != nil {
-                       return err
-               }
-       case len(args) == 1 && args[0] == "login":
-               err := login(ctx, os.Stdout)
+       case len(args) == 1 && (args[0] == "help" || args[0] == "--help" || args[0] == "-h"):
+               _, err := fmt.Print(usage)
                if err != nil {
-                       return err
+                       return fmt.Errorf("write command help: %w", err)
                }
-       case len(args) == 1 && args[0] == "daemon":
-               err := daemon(ctx, os.Stdout)
+       case len(args) == 1 && args[0] == "version":
+               _, err := fmt.Println(version)
                if err != nil {
-                       return err
+                       return fmt.Errorf("write build version: %w", err)
                }
-       case len(args) == 1 && args[0] == "chat":
-               err := chat(ctx, os.Stdin, os.Stdout)
+       case len(args) == 1 && args[0] == "login":
+               err := chatgptLogin(ctx, os.Stdout)
                if err != nil {
                        return err
                }
+
+       // A signal is the normal stop of the daemon and the client, so their
+       // result does not include ctx.Err().
+       case len(args) == 1 && args[0] == "daemon":
+               return daemon(ctx, os.Stdout)
+       case len(args) == 0 || len(args) == 1 && args[0] == "chat":
+               return terminal(ctx, os.Stdin, os.Stdout)
+       case len(args) > 0 && args[0] == "import":
+               return whatsappImport(ctx, args[1:], os.Stdout)
        case len(args) > 0 && args[0] == "probe":
                err := probe(ctx, args[1:], os.Stdout)
                if err != nil {
                        return err
                }
        default:
-               return fmt.Errorf("unknown command %q", strings.Join(args, " "))
+               return fmt.Errorf("unknown command %q\n\n%s", strings.Join(args, " "), strings.TrimSuffix(usage, "\n"))
        }
 
        return ctx.Err()