Borg/pkg/ui/non_interactive_prompter.go
google-labs-jules[bot] 7adfff1d0d feat: Add TDD framework and fix build error
This commit introduces a TDD testing framework for the `collect` commands and resolves a build failure.

- A `TDD/` directory has been added to house tests for the `collect` commands.
- An environment variable `BORG_PLEXSUS=0` has been implemented to enable a mock mode, which prevents external network calls during testing.
- The `collect` commands have been updated to use the command's output streams, allowing for output capturing in tests.
- A `pkg/mocks` package has been added to provide mock implementations for testing.
- The `.gitignore` file has been updated to exclude generated `.datanode` files.
- The "flag redefined" build error has been fixed by refactoring the root command initialization in `cmd/root.go` to prevent duplicate flag definitions.
2025-11-02 18:11:04 +00:00

74 lines
1.3 KiB
Go

package ui
import (
"fmt"
"os"
"sync"
"time"
"github.com/fatih/color"
"github.com/mattn/go-isatty"
)
type NonInteractivePrompter struct {
stopChan chan struct{}
quoteFunc func() (string, error)
started bool
mu sync.Mutex
stopOnce sync.Once
}
func NewNonInteractivePrompter(quoteFunc func() (string, error)) *NonInteractivePrompter {
return &NonInteractivePrompter{
stopChan: make(chan struct{}),
quoteFunc: quoteFunc,
}
}
func (p *NonInteractivePrompter) Start() {
p.mu.Lock()
if p.started {
p.mu.Unlock()
return
}
p.started = true
p.mu.Unlock()
if p.IsInteractive() {
return // Don't start in interactive mode
}
go func() {
ticker := time.NewTicker(3 * time.Second)
defer ticker.Stop()
for {
select {
case <-p.stopChan:
return
case <-ticker.C:
quote, err := p.quoteFunc()
if err != nil {
fmt.Println("Error getting quote:", err)
continue
}
c := color.New(color.FgGreen)
c.Println(quote)
}
}
}()
}
func (p *NonInteractivePrompter) Stop() {
if p.IsInteractive() {
return
}
p.stopOnce.Do(func() {
close(p.stopChan)
})
}
func (p *NonInteractivePrompter) IsInteractive() bool {
return isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd())
}