Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ import (
"golang.org/x/term"
)

// processFile handles opening, processing, and closing a single file with proper defer scope
func processFile(g *gat.Gat, filename string, opts ...gat.PrintOption) error {
f, err := os.Open(filename)
if err != nil {
return err
}
defer func() { _ = f.Close() }()
Copy link

Copilot AI Jun 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider capturing and logging an error from f.Close() in case the close operation fails, to aid in debugging potential file system issues.

Suggested change
defer func() { _ = f.Close() }()
defer func() {
if err := f.Close(); err != nil {
log.Printf("failed to close file %s: %v", filename, err)
}
}()

Copilot uses AI. Check for mistakes.

return g.Print(os.Stdout, f, opts...)
}

var rootCmd = &cobra.Command{
Use: "gat [file]...",
Short: "cat alternative written in Go",
Expand Down Expand Up @@ -52,12 +63,7 @@ var rootCmd = &cobra.Command{
}

for _, filename := range args {
f, err := os.Open(filename)
if err != nil {
return err
}
defer func() { _ = f.Close() }()
if err := g.Print(os.Stdout, f, gat.WithPretty(flagPretty), gat.WithFilename(filename)); err != nil {
if err := processFile(g, filename, gat.WithPretty(flagPretty), gat.WithFilename(filename)); err != nil {
return err
}
}
Expand Down