Skip to content

Commit 9be8087

Browse files
committed
fix(init): improve worktree removal and ensure safe file closing
1 parent 63042e5 commit 9be8087

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

cmd/init.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,9 @@ func runInit(cmd *cobra.Command, args []string) error {
166166
if utils.PathExists(cfg.DocWorktreeDir) && force {
167167
printInfo("Removing existing worktree")
168168
if err := utils.RunGit("", "worktree", "remove", "-f", cfg.DocWorktreeDir); err != nil {
169-
os.RemoveAll(cfg.DocWorktreeDir)
169+
if err := os.RemoveAll(cfg.DocWorktreeDir); err != nil {
170+
return fmt.Errorf("failed to remove worktree %v", err)
171+
}
170172
}
171173
}
172174

cmd/root.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ func printSuccess(format string, args ...interface{}) {
4545
color.Green("✓ "+format, args...)
4646
}
4747

48-
func printError(format string, args ...interface{}) {
49-
color.Red("✗ "+format, args...)
50-
}
51-
5248
func printWarning(format string, args ...interface{}) {
5349
color.Yellow("⚠ "+format, args...)
5450
}

utils/file.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"fmt"
66
"io"
7+
"log"
78
"os"
89
"os/exec"
910
"path/filepath"
@@ -16,7 +17,11 @@ func FileContains(path, line string) bool {
1617
if err != nil {
1718
return false
1819
}
19-
defer file.Close()
20+
defer func() {
21+
if err := file.Close(); err != nil {
22+
log.Printf("failed to close file: %v", err)
23+
}
24+
}()
2025

2126
scanner := bufio.NewScanner(file)
2227
for scanner.Scan() {
@@ -33,7 +38,11 @@ func AppendToFile(path string, lines []string) error {
3338
if err != nil {
3439
return err
3540
}
36-
defer file.Close()
41+
defer func() {
42+
if err := file.Close(); err != nil {
43+
log.Printf("failed to close file: %v", err)
44+
}
45+
}()
3746

3847
for _, line := range lines {
3948
if _, err := file.WriteString(line + "\n"); err != nil {
@@ -46,7 +55,9 @@ func AppendToFile(path string, lines []string) error {
4655

4756
func EnsureSymlink(from, to string) error {
4857
if _, err := os.Lstat(from); err == nil {
49-
os.Remove(from)
58+
if err := os.Remove(from); err != nil {
59+
return fmt.Errorf("fail to remove %s", from)
60+
}
5061
}
5162

5263
if runtime.GOOS == "windows" {
@@ -135,13 +146,21 @@ func copyFile(src, dst string) error {
135146
if err != nil {
136147
return err
137148
}
138-
defer source.Close()
149+
defer func() {
150+
if err := source.Close(); err != nil {
151+
log.Printf("failed to close file: %v", err)
152+
}
153+
}()
139154

140155
destination, err := os.Create(dst)
141156
if err != nil {
142157
return err
143158
}
144-
defer destination.Close()
159+
defer func() {
160+
if err := destination.Close(); err != nil {
161+
log.Printf("failed to close file: %v", err)
162+
}
163+
}()
145164

146165
_, err = io.Copy(destination, source)
147166
return err

0 commit comments

Comments
 (0)