mirror of
https://github.com/tw93/Mole.git
synced 2026-03-22 21:20:09 +00:00
Improve update checks and cleanup UX, add timeout regressions
This commit is contained in:
@@ -24,6 +24,20 @@ var (
|
||||
jsonOutput = flag.Bool("json", false, "output metrics as JSON instead of TUI")
|
||||
)
|
||||
|
||||
func shouldUseJSONOutput(forceJSON bool, stdout *os.File) bool {
|
||||
if forceJSON {
|
||||
return true
|
||||
}
|
||||
if stdout == nil {
|
||||
return false
|
||||
}
|
||||
info, err := stdout.Stat()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return (info.Mode() & os.ModeCharDevice) == 0
|
||||
}
|
||||
|
||||
type tickMsg struct{}
|
||||
type animTickMsg struct{}
|
||||
|
||||
@@ -246,7 +260,7 @@ func runTUIMode() {
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
if *jsonOutput {
|
||||
if shouldUseJSONOutput(*jsonOutput, os.Stdout) {
|
||||
runJSONMode()
|
||||
} else {
|
||||
runTUIMode()
|
||||
|
||||
44
cmd/status/main_test.go
Normal file
44
cmd/status/main_test.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestShouldUseJSONOutput_ForceFlag(t *testing.T) {
|
||||
if !shouldUseJSONOutput(true, nil) {
|
||||
t.Fatalf("expected force JSON flag to enable JSON mode")
|
||||
}
|
||||
}
|
||||
|
||||
func TestShouldUseJSONOutput_NilStdout(t *testing.T) {
|
||||
if shouldUseJSONOutput(false, nil) {
|
||||
t.Fatalf("expected nil stdout to keep TUI mode")
|
||||
}
|
||||
}
|
||||
|
||||
func TestShouldUseJSONOutput_NonTTYPipe(t *testing.T) {
|
||||
reader, writer, err := os.Pipe()
|
||||
if err != nil {
|
||||
t.Fatalf("create pipe: %v", err)
|
||||
}
|
||||
defer reader.Close()
|
||||
defer writer.Close()
|
||||
|
||||
if !shouldUseJSONOutput(false, writer) {
|
||||
t.Fatalf("expected pipe stdout to use JSON mode")
|
||||
}
|
||||
}
|
||||
|
||||
func TestShouldUseJSONOutput_NonTTYFile(t *testing.T) {
|
||||
tmpFile, err := os.CreateTemp("", "mole-status-stdout-*.txt")
|
||||
if err != nil {
|
||||
t.Fatalf("create temp file: %v", err)
|
||||
}
|
||||
defer os.Remove(tmpFile.Name())
|
||||
defer tmpFile.Close()
|
||||
|
||||
if !shouldUseJSONOutput(false, tmpFile) {
|
||||
t.Fatalf("expected file stdout to use JSON mode")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user