persistent.go 931 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package shell
  2. import (
  3. "log/slog"
  4. "sync"
  5. )
  6. // PersistentShell is a singleton shell instance that maintains state across the application
  7. type PersistentShell struct {
  8. *Shell
  9. }
  10. var (
  11. once sync.Once
  12. shellInstance *PersistentShell
  13. )
  14. // GetPersistentShell returns the singleton persistent shell instance
  15. // This maintains backward compatibility with the existing API
  16. func GetPersistentShell(cwd string) *PersistentShell {
  17. once.Do(func() {
  18. shellInstance = &PersistentShell{
  19. Shell: NewShell(&Options{
  20. WorkingDir: cwd,
  21. Logger: &loggingAdapter{},
  22. }),
  23. }
  24. })
  25. return shellInstance
  26. }
  27. // INFO: only used for tests
  28. func Reset(cwd string) {
  29. once = sync.Once{}
  30. _ = GetPersistentShell(cwd)
  31. }
  32. // slog.dapter adapts the internal slog.package to the Logger interface
  33. type loggingAdapter struct{}
  34. func (l *loggingAdapter) InfoPersist(msg string, keysAndValues ...any) {
  35. slog.Info(msg, keysAndValues...)
  36. }