Skip to content

Commit 948671d

Browse files
committed
add replace file prompt
1 parent 26ab71d commit 948671d

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

internal/downloader/downloader.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import (
55
"io"
66
"net/http"
77
"os"
8+
89
"path/filepath"
910
"strings"
1011

1112
"github.com/kennygrant/sanitize"
1213
"github.com/laureanray/clibgen/internal/book"
1314
"github.com/laureanray/clibgen/internal/console"
15+
"github.com/manifoldco/promptui"
1416
"github.com/schollz/progressbar/v3"
1517
)
1618

@@ -45,8 +47,32 @@ func (d *Downloader) Download() error {
4547

4648
fmt.Println("Downloading to: ", filename)
4749

48-
f, _ := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0666)
49-
defer f.Close()
50+
var f *os.File
51+
// Check if file exists
52+
if _, err := os.Stat(filename); err == nil {
53+
prompt := promptui.Select{
54+
Label: "File Exists. Do you want to replace file?",
55+
Items: []string{"Yes", "No"},
56+
}
57+
_, result, err := prompt.Run()
58+
59+
if err != nil {
60+
fmt.Printf("Prompt failed %v\n", err)
61+
return err
62+
}
63+
64+
if result == "Yes" {
65+
// file doesnt exists
66+
f, _ = os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0666)
67+
defer f.Close()
68+
} else {
69+
os.Exit(1)
70+
}
71+
} else {
72+
// file doesnt exists
73+
f, _ = os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0666)
74+
defer f.Close()
75+
}
5076

5177
bar := progressbar.DefaultBytes(
5278
resp.ContentLength,

0 commit comments

Comments
 (0)