@@ -5,12 +5,14 @@ import (
5
5
"io"
6
6
"net/http"
7
7
"os"
8
+
8
9
"path/filepath"
9
10
"strings"
10
11
11
12
"github.com/kennygrant/sanitize"
12
13
"github.com/laureanray/clibgen/internal/book"
13
14
"github.com/laureanray/clibgen/internal/console"
15
+ "github.com/manifoldco/promptui"
14
16
"github.com/schollz/progressbar/v3"
15
17
)
16
18
@@ -45,8 +47,32 @@ func (d *Downloader) Download() error {
45
47
46
48
fmt .Println ("Downloading to: " , filename )
47
49
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
+ }
50
76
51
77
bar := progressbar .DefaultBytes (
52
78
resp .ContentLength ,
0 commit comments