Files
Memoh/cmd/memoh/root.go
T
晨苒 d50eeea114 reactor(cli): move memoh cli to tui
1. Split the oversized `cmd/agent` entrypoint into a multi-file package and update dev/build scripts to use the package path instead of compiling `main.go` directly.
2. Add a new `memoh` terminal UI for local bot chat, with Bubble Tea
2026-04-14 06:22:00 +08:00

70 lines
1.5 KiB
Go

package main
import (
"fmt"
tea "github.com/charmbracelet/bubbletea"
"github.com/spf13/cobra"
"github.com/memohai/memoh/internal/tui"
)
type cliContext struct {
state tui.State
server string
}
func newRootCommand() *cobra.Command {
ctx := &cliContext{}
rootCmd := &cobra.Command{
Use: "memoh",
Short: "Memoh terminal operator CLI",
RunE: func(_ *cobra.Command, _ []string) error {
return runTUI(ctx)
},
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
state, err := tui.LoadState()
if err != nil {
return err
}
ctx.state = state
if ctx.server != "" {
ctx.state.ServerURL = tui.NormalizeServerURL(ctx.server)
}
return nil
},
}
rootCmd.PersistentFlags().StringVar(&ctx.server, "server", "", "Memoh server URL")
rootCmd.AddCommand(newMigrateCommand())
rootCmd.AddCommand(newLoginCommand(ctx))
rootCmd.AddCommand(newChatCommand(ctx))
rootCmd.AddCommand(&cobra.Command{
Use: "tui",
Short: "Open the terminal UI",
RunE: func(_ *cobra.Command, _ []string) error {
return runTUI(ctx)
},
})
rootCmd.AddCommand(&cobra.Command{
Use: "version",
Short: "Print version information",
RunE: func(_ *cobra.Command, _ []string) error {
return runVersion()
},
})
return rootCmd
}
func runTUI(ctx *cliContext) error {
model := tui.NewTUIModel(ctx.state)
program := tea.NewProgram(model, tea.WithAltScreen())
if _, err := program.Run(); err != nil {
return fmt.Errorf("run tui: %w", err)
}
return nil
}