Skip to content

Commit 2ca04b3

Browse files
committed
fix: only create debug.log if env var is set
1 parent e12383c commit 2ca04b3

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

main.go

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
tea "github.com/charmbracelet/bubbletea"
1212
"github.com/charmbracelet/log"
1313
"github.com/charmbracelet/x/ansi"
14+
"github.com/muesli/termenv"
1415

1516
"github.com/dlvhdr/diffnav/pkg/ui"
1617
)
@@ -22,20 +23,34 @@ func main() {
2223
}
2324

2425
if stat.Mode()&os.ModeNamedPipe == 0 && stat.Size() == 0 {
25-
fmt.Println("Try piping in some text.")
26-
os.Exit(1)
26+
fmt.Println("No diff, exiting")
27+
os.Exit(0)
2728
}
2829

29-
var fileErr error
30-
logFile, fileErr := os.OpenFile("debug.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
31-
if fileErr == nil {
32-
log.SetOutput(logFile)
33-
log.SetTimeFormat(time.Kitchen)
34-
log.SetReportCaller(true)
35-
log.SetLevel(log.DebugLevel)
30+
if os.Getenv("DEBUG") == "true" {
31+
var fileErr error
32+
logFile, fileErr := os.OpenFile("debug.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
33+
if fileErr != nil {
34+
fmt.Println("Error opening debug.log:", fileErr)
35+
os.Exit(1)
36+
}
3637
defer logFile.Close()
37-
log.SetOutput(logFile)
38-
log.Debug("Starting diffnav, logging to debug.log")
38+
39+
if fileErr == nil {
40+
log.SetOutput(logFile)
41+
log.SetTimeFormat(time.Kitchen)
42+
log.SetReportCaller(true)
43+
log.SetLevel(log.DebugLevel)
44+
45+
log.SetOutput(logFile)
46+
log.SetColorProfile(termenv.TrueColor)
47+
wd, err := os.Getwd()
48+
if err != nil {
49+
fmt.Println("Error getting current working dir", err)
50+
os.Exit(1)
51+
}
52+
log.Debug("🚀 Starting diffnav", "logFile", wd+string(os.PathSeparator)+logFile.Name())
53+
}
3954
}
4055

4156
reader := bufio.NewReader(os.Stdin)
@@ -53,15 +68,10 @@ func main() {
5368
}
5469
}
5570

56-
if os.Getenv("DEBUG") == "true" {
57-
logger, _ := tea.LogToFile("debug.log", "debug")
58-
defer logger.Close()
59-
}
60-
6171
input := ansi.Strip(b.String())
6272
if strings.TrimSpace(input) == "" {
6373
fmt.Println("No input provided, exiting")
64-
os.Exit(1)
74+
os.Exit(0)
6575
}
6676
p := tea.NewProgram(ui.New(input), tea.WithMouseAllMotion())
6777

0 commit comments

Comments
 (0)