probo/cmd/util/util.go

50 lines
785 B
Go
Raw Normal View History

2024-04-22 14:24:29 +02:00
package util
import (
"bufio"
"fmt"
"io"
"os"
"strings"
tea "github.com/charmbracelet/bubbletea"
)
func LogToFile() *os.File {
if len(os.Getenv("DEBUG")) > 0 {
f, err := tea.LogToFile("probo-debug.log", "[DEBUG]")
if err != nil {
fmt.Println("fatal:", err)
os.Exit(1)
}
return f
}
return nil
}
func ReadStdin() string {
var b strings.Builder
stat, err := os.Stdin.Stat()
if err != nil {
panic(err)
}
if stat.Mode()&os.ModeNamedPipe != 0 || stat.Size() != 0 {
reader := bufio.NewReader(os.Stdin)
for {
r, _, err := reader.ReadRune()
if err != nil && err == io.EOF {
break
}
_, err = b.WriteRune(r)
if err != nil {
fmt.Println("Error getting input:", err)
os.Exit(1)
}
}
}
return strings.TrimSpace(b.String())
}