]> git.giacomolli.uk - onechat/blobdiff - main.go
Open the chat when no command is given
[onechat] / main.go
diff --git a/main.go b/main.go
index 271a91a56d749d67880213d921a949ecfa6b8b15..04b06770977eac566ca0599a0766f4864b095bbb 100644 (file)
--- a/main.go
+++ b/main.go
@@ -5,9 +5,23 @@ import (
        "fmt"
        "os"
        "os/signal"
        "fmt"
        "os"
        "os/signal"
+       "strings"
        "syscall"
 )
 
        "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
+  login                    log in to ChatGPT with a device code
+  probe LOG COUNT [BUILDS] measure the compactor on a log, with real calls
+  help                     show this help
+
+Chat needs a running daemon. ONECHAT_DIR sets the data directory.
+`
+
 func main() {
        err := run()
        if err != nil {
 func main() {
        err := run()
        if err != nil {
@@ -20,9 +34,31 @@ func run() error {
        ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
        defer cancel()
 
        ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
        defer cancel()
 
-       _, err := fmt.Println("onechat")
-       if err != nil {
-               return err
+       args := os.Args[1:]
+       switch {
+       case len(args) == 1 && (args[0] == "help" || args[0] == "--help" || args[0] == "-h"):
+               _, err := fmt.Print(usage)
+               if err != nil {
+                       return err
+               }
+       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] == "probe":
+               err := probe(ctx, args[1:], os.Stdout)
+               if err != nil {
+                       return err
+               }
+       default:
+               return fmt.Errorf("unknown command %q\n\n%s", strings.Join(args, " "), strings.TrimSuffix(usage, "\n"))
        }
 
        return ctx.Err()
        }
 
        return ctx.Err()