xonecas.io
6119
My

mysis / commit f04407f

feat: add CLI improvements - help, shorthands, and delete-session

- Add -h/--help flag with formatted usage, examples, and in-session commands
- Add shorthands for all CLI flags:
  - -v for --version
  - -c for --config
  - -d for --debug
  - -p for --provider
  - -s for --session
  - -l for --list-sessions
  - -D for --delete-session
- Add --delete-session/-D flag to delete sessions by name
- Add DeleteSessionByName method to store with proper error handling
- Refactor flag parsing to use flag.BoolVar/StringVar for consistency
- Show session info before deletion (ID, provider, message count)
- Clarify that running without -s creates anonymous session

xonecas2026-02-10 f04407fe6c6d77532fa403c1ced4723e5f6c58c9 parent 3699359

2 files changed

cmd/mysis/main.go+136−23
@@ -25,19 +25,89 @@ import (
2525 // Version is set at build time via ldflags.
2626 var Version = "dev"
2727
28+// printHelp displays usage information.
29+func printHelp() {
30+ fmt.Println(styles.Brand.Render("╔══════════════════════════════════════╗"))
31+ fmt.Println(styles.Brand.Render("║") + " " + styles.BrandBold.Render("Mysis") + " - SpaceMolt Agent CLI " + styles.Brand.Render("║"))
32+ fmt.Println(styles.Brand.Render("╚══════════════════════════════════════╝"))
33+ fmt.Println()
34+ fmt.Println(styles.BrandBold.Render("USAGE:"))
35+ fmt.Println(" mysis [flags]")
36+ fmt.Println()
37+ fmt.Println(styles.BrandBold.Render("FLAGS:"))
38+ fmt.Println(" " + styles.Secondary.Render("-h, --help") + " Show this help message")
39+ fmt.Println(" " + styles.Secondary.Render("-v, --version") + " Show version information")
40+ fmt.Println(" " + styles.Secondary.Render("-c, --config") + " PATH Path to config file (default: config.toml)")
41+ fmt.Println(" " + styles.Secondary.Render("-d, --debug") + " Enable debug logging")
42+ fmt.Println(" " + styles.Secondary.Render("-p, --provider") + " NAME Provider name (overrides config default)")
43+ fmt.Println(" " + styles.Secondary.Render("-s, --session") + " NAME Session name (resume or create)")
44+ fmt.Println(" " + styles.Secondary.Render("-l, --list-sessions") + " List recent sessions and exit")
45+ fmt.Println(" " + styles.Secondary.Render("-D, --delete-session") + " N Delete session by name and exit")
46+ fmt.Println()
47+ fmt.Println(styles.BrandBold.Render("EXAMPLES:"))
48+ fmt.Println(" # Start anonymous session")
49+ fmt.Println(" mysis")
50+ fmt.Println()
51+ fmt.Println(" # Resume or create named session")
52+ fmt.Println(" mysis -s mybot")
53+ fmt.Println()
54+ fmt.Println(" # List all sessions")
55+ fmt.Println(" mysis -l")
56+ fmt.Println()
57+ fmt.Println(" # Delete a session")
58+ fmt.Println(" mysis -D mybot")
59+ fmt.Println()
60+ fmt.Println(styles.BrandBold.Render("IN-SESSION COMMANDS:"))
61+ fmt.Println(" " + styles.Secondary.Render("/autoplay <message>") + " Start autonomous gameplay with given goal")
62+ fmt.Println(" " + styles.Secondary.Render("/autoplay stop") + " Stop autonomous gameplay")
63+ fmt.Println(" " + styles.Secondary.Render("exit, quit") + " Exit the session")
64+ fmt.Println()
65+ fmt.Println(styles.Muted.Render("Note: Running without -s/--session creates an anonymous session (not saved by name)."))
66+ fmt.Println()
67+}
68+
2869 func main() {
2970 // Parse flags
3071 var (
31- showVersion = flag.Bool("version", false, "Show version and exit")
32- configPath = flag.String("config", "", "Path to config file")
33- debug = flag.Bool("debug", false, "Enable debug logging")
34- providerName = flag.String("p", "", "Provider name (overrides default from config)")
35- sessionName = flag.String("session", "", "Session name (resume or create named session)")
36- listSessions = flag.Bool("list-sessions", false, "List recent sessions and exit")
72+ showHelp bool
73+ showVersion bool
74+ configPath string
75+ debug bool
76+ providerName string
77+ sessionName string
78+ listSessions bool
79+ deleteSession string
3780 )
81+ flag.BoolVar(&showHelp, "help", false, "Show help and exit")
82+ flag.BoolVar(&showHelp, "h", false, "Show help and exit (shorthand)")
83+ flag.BoolVar(&showVersion, "version", false, "Show version and exit")
84+ flag.BoolVar(&showVersion, "v", false, "Show version and exit (shorthand)")
85+ flag.StringVar(&configPath, "config", "", "Path to config file")
86+ flag.StringVar(&configPath, "c", "", "Path to config file (shorthand)")
87+ flag.BoolVar(&debug, "debug", false, "Enable debug logging")
88+ flag.BoolVar(&debug, "d", false, "Enable debug logging (shorthand)")
89+ flag.StringVar(&providerName, "provider", "", "Provider name (overrides default from config)")
90+ flag.StringVar(&providerName, "p", "", "Provider name (shorthand)")
91+ flag.StringVar(&sessionName, "session", "", "Session name (resume or create named session)")
92+ flag.StringVar(&sessionName, "s", "", "Session name (shorthand)")
93+ flag.BoolVar(&listSessions, "list-sessions", false, "List recent sessions and exit")
94+ flag.BoolVar(&listSessions, "l", false, "List recent sessions and exit (shorthand)")
95+ flag.StringVar(&deleteSession, "delete-session", "", "Delete a session by name")
96+ flag.StringVar(&deleteSession, "D", "", "Delete a session by name (shorthand)")
97+
98+ // Custom usage function
99+ flag.Usage = func() {
100+ printHelp()
101+ }
102+
38103 flag.Parse()
39104
40- if *showVersion {
105+ if showHelp {
106+ printHelp()
107+ os.Exit(0)
108+ }
109+
110+ if showVersion {
41111 fmt.Printf("Mysis %s\n", Version)
42112 os.Exit(0)
43113 }
@@ -45,26 +115,26 @@ func main() {
45115 // Initialize logging
46116 zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
47117 log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
48- if *debug {
118+ if debug {
49119 zerolog.SetGlobalLevel(zerolog.DebugLevel)
50120 } else {
51121 zerolog.SetGlobalLevel(zerolog.InfoLevel)
52122 }
53123
54124 // Find config file
55- if *configPath == "" {
125+ if configPath == "" {
56126 // Try current directory first, then ~/.config/mysis/
57127 if _, err := os.Stat("config.toml"); err == nil {
58- *configPath = "config.toml"
128+ configPath = "config.toml"
59129 } else {
60130 dataDir, err := config.DataDir()
61131 if err == nil {
62- *configPath = dataDir + "/config.toml"
132+ configPath = dataDir + "/config.toml"
63133 }
64134 }
65135 }
66136
67- if *configPath == "" {
137+ if configPath == "" {
68138 fmt.Fprintln(os.Stderr, styles.Error.Render("Error: config file not found"))
69139 fmt.Fprintln(os.Stderr, "Tried: ./config.toml and ~/.config/mysis/config.toml")
70140 os.Exit(1)
@@ -72,11 +142,11 @@ func main() {
72142
73143 log.Info().
74144 Str("version", Version).
75- Str("config", *configPath).
145+ Str("config", configPath).
76146 Msg("Starting Mysis")
77147
78148 // Load config
79- cfg, err := config.Load(*configPath)
149+ cfg, err := config.Load(configPath)
80150 if err != nil {
81151 fmt.Fprintln(os.Stderr, styles.Error.Render("Failed to load config: "+err.Error()))
82152 os.Exit(1)
@@ -91,7 +161,7 @@ func main() {
91161 defer db.Close()
92162
93163 // Handle --list-sessions flag
94- if *listSessions {
164+ if listSessions {
95165 if err := listSessionsCmd(db); err != nil {
96166 fmt.Fprintln(os.Stderr, styles.Error.Render("Error: "+err.Error()))
97167 os.Exit(1)
@@ -99,6 +169,15 @@ func main() {
99169 return
100170 }
101171
172+ // Handle --delete-session flag
173+ if deleteSession != "" {
174+ if err := deleteSessionCmd(db, deleteSession); err != nil {
175+ fmt.Fprintln(os.Stderr, styles.Error.Render("Error: "+err.Error()))
176+ os.Exit(1)
177+ }
178+ return
179+ }
180+
102181 // Load credentials
103182 creds, err := config.LoadCredentials()
104183 if err != nil {
@@ -110,7 +189,7 @@ func main() {
110189 registry := initializeProviders(cfg, creds)
111190
112191 // Select provider
113- selectedProvider := *providerName
192+ selectedProvider := providerName
114193 if selectedProvider == "" {
115194 // Use first provider from config
116195 for name := range cfg.Providers {
@@ -166,26 +245,26 @@ func main() {
166245 // Initialize or resume session
167246 var sessionID string
168247 var sessionInfo string
169- if *sessionName != "" {
248+ if sessionName != "" {
170249 // Try to resume by name
171- sess, err := db.GetSessionByName(*sessionName)
250+ sess, err := db.GetSessionByName(sessionName)
172251 if err != nil {
173252 fmt.Fprintln(os.Stderr, styles.Error.Render("Failed to load session: "+err.Error()))
174253 os.Exit(1)
175254 }
176255 if sess != nil {
177256 sessionID = sess.ID
178- sessionInfo = fmt.Sprintf("Resumed session: %s", *sessionName)
179- log.Info().Str("session_id", sessionID).Str("name", *sessionName).Msg("Resumed session")
257+ sessionInfo = fmt.Sprintf("Resumed session: %s", sessionName)
258+ log.Info().Str("session_id", sessionID).Str("name", sessionName).Msg("Resumed session")
180259 } else {
181260 // Create new named session
182261 sessionID = uuid.New().String()
183- if err := db.CreateSession(sessionID, selectedProvider, providerCfg.Model, sessionName); err != nil {
262+ if err := db.CreateSession(sessionID, selectedProvider, providerCfg.Model, &sessionName); err != nil {
184263 fmt.Fprintln(os.Stderr, styles.Error.Render("Failed to create session: "+err.Error()))
185264 os.Exit(1)
186265 }
187- sessionInfo = fmt.Sprintf("New session: %s", *sessionName)
188- log.Info().Str("session_id", sessionID).Str("name", *sessionName).Msg("Created named session")
266+ sessionInfo = fmt.Sprintf("New session: %s", sessionName)
267+ log.Info().Str("session_id", sessionID).Str("name", sessionName).Msg("Created named session")
189268 }
190269 } else {
191270 // Create anonymous session
@@ -516,6 +595,40 @@ func listSessionsCmd(db *store.Store) error {
516595 return nil
517596 }
518597
598+// deleteSessionCmd deletes a session by name.
599+func deleteSessionCmd(db *store.Store, name string) error {
600+ // Get session to show info before deletion
601+ sess, err := db.GetSessionByName(name)
602+ if err != nil {
603+ return fmt.Errorf("get session: %w", err)
604+ }
605+ if sess == nil {
606+ return fmt.Errorf("session '%s' not found", name)
607+ }
608+
609+ // Get message count
610+ messages, err := db.LoadMessages(sess.ID)
611+ if err != nil {
612+ return fmt.Errorf("load messages: %w", err)
613+ }
614+
615+ // Confirm deletion
616+ fmt.Printf("Delete session '%s'?\n", styles.BrandBold.Render(name))
617+ fmt.Printf(" ID: %s\n", sess.ID[:8])
618+ fmt.Printf(" Provider: %s (%s)\n", sess.Provider, sess.Model)
619+ fmt.Printf(" Messages: %d\n", len(messages))
620+ fmt.Printf(" Created: %s\n", formatDuration(time.Since(sess.CreatedAt))+" ago")
621+ fmt.Println()
622+
623+ // Delete
624+ if err := db.DeleteSessionByName(name); err != nil {
625+ return fmt.Errorf("delete session: %w", err)
626+ }
627+
628+ fmt.Println(styles.Success.Render(fmt.Sprintf("Deleted session '%s'", name)))
629+ return nil
630+}
631+
519632 // formatDuration formats a duration in human-readable form.
520633 func formatDuration(d time.Duration) string {
521634 if d < time.Minute {
internal/store/store.go+20−0
@@ -302,3 +302,23 @@ func (s *Store) DeleteSession(id string) error {
302302 _, err := s.db.Exec(query, id)
303303 return err
304304 }
305+
306+// DeleteSessionByName deletes a session by name and all its messages.
307+func (s *Store) DeleteSessionByName(name string) error {
308+ query := `DELETE FROM sessions WHERE name = ?`
309+ result, err := s.db.Exec(query, name)
310+ if err != nil {
311+ return fmt.Errorf("delete session by name: %w", err)
312+ }
313+
314+ rowsAffected, err := result.RowsAffected()
315+ if err != nil {
316+ return fmt.Errorf("get rows affected: %w", err)
317+ }
318+
319+ if rowsAffected == 0 {
320+ return fmt.Errorf("session '%s' not found", name)
321+ }
322+
323+ return nil
324+}