-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
64 lines (51 loc) · 1.27 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"flag"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
)
func main() {
// specify the URL of the file to download
urlFlag := flag.String("u", "", "URL of the file to download")
// parse the command-line flags
flag.Parse()
// make sure the URL and folder path are provided
if *urlFlag == "" {
fmt.Println("Please provide the URL using the -u flag")
return
}
// Get the current working directory
folderPath, err := os.Getwd()
if err != nil {
// Handle error
fmt.Println("Error:", err)
return
}
// extract the file name from the URL
fileName := filepath.Base(*urlFlag)
// create the file to save the download
filePath := filepath.Join(folderPath, fileName)
file, err := os.Create(filePath)
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
// download the file
response, err := http.Get(*urlFlag)
if err != nil {
fmt.Println(err)
return
}
defer response.Body.Close()
// copy the downloaded content to the file
_, err = io.Copy(file, response.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Download completed successfully.")
}