Skip to content

Commit

Permalink
fix: only create debug.log if env var is set
Browse files Browse the repository at this point in the history
  • Loading branch information
dlvhdr committed Oct 5, 2024
1 parent e12383c commit 2ca04b3
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/log"
"github.com/charmbracelet/x/ansi"
"github.com/muesli/termenv"

"github.com/dlvhdr/diffnav/pkg/ui"
)
Expand All @@ -22,20 +23,34 @@ func main() {
}

if stat.Mode()&os.ModeNamedPipe == 0 && stat.Size() == 0 {
fmt.Println("Try piping in some text.")
os.Exit(1)
fmt.Println("No diff, exiting")
os.Exit(0)
}

var fileErr error
logFile, fileErr := os.OpenFile("debug.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if fileErr == nil {
log.SetOutput(logFile)
log.SetTimeFormat(time.Kitchen)
log.SetReportCaller(true)
log.SetLevel(log.DebugLevel)
if os.Getenv("DEBUG") == "true" {
var fileErr error
logFile, fileErr := os.OpenFile("debug.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if fileErr != nil {
fmt.Println("Error opening debug.log:", fileErr)
os.Exit(1)
}
defer logFile.Close()
log.SetOutput(logFile)
log.Debug("Starting diffnav, logging to debug.log")

if fileErr == nil {
log.SetOutput(logFile)
log.SetTimeFormat(time.Kitchen)
log.SetReportCaller(true)
log.SetLevel(log.DebugLevel)

log.SetOutput(logFile)
log.SetColorProfile(termenv.TrueColor)
wd, err := os.Getwd()
if err != nil {
fmt.Println("Error getting current working dir", err)
os.Exit(1)
}
log.Debug("🚀 Starting diffnav", "logFile", wd+string(os.PathSeparator)+logFile.Name())
}
}

reader := bufio.NewReader(os.Stdin)
Expand All @@ -53,15 +68,10 @@ func main() {
}
}

if os.Getenv("DEBUG") == "true" {
logger, _ := tea.LogToFile("debug.log", "debug")
defer logger.Close()
}

input := ansi.Strip(b.String())
if strings.TrimSpace(input) == "" {
fmt.Println("No input provided, exiting")
os.Exit(1)
os.Exit(0)
}
p := tea.NewProgram(ui.New(input), tea.WithMouseAllMotion())

Expand Down

0 comments on commit 2ca04b3

Please sign in to comment.