-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
167 lines (134 loc) · 4.37 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"fmt"
"io/ioutil"
"log"
"github.com/dgageot/getme/cache"
"github.com/dgageot/getme/files"
"github.com/dgageot/getme/tar"
"github.com/dgageot/getme/urls"
"github.com/dgageot/getme/zip"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
force bool
)
func main() {
var rootCmd = &cobra.Command{Use: "getme"}
options := files.Options{}
rootCmd.PersistentFlags().StringVar(&options.AuthToken, "authToken", "", "Api authentication token")
rootCmd.PersistentFlags().StringVar(&options.AuthTokenEnvVariable, "authTokenEnvVariable", "", "Env variable containing an api authentication token")
rootCmd.PersistentFlags().StringVar(&options.S3AccessKey, "s3AccessKey", "", "Amazon S3 access key")
rootCmd.PersistentFlags().StringVar(&options.S3SecretKey, "s3SecretKey", "", "Amazon S3 secret key")
rootCmd.PersistentFlags().StringVar(&options.Sha256, "sha256", "", "Checksum to check downloaded files")
rootCmd.PersistentFlags().BoolVar(&force, "force", false, "Force download")
rootCmd.AddCommand(&cobra.Command{
Use: "Download",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("An url must be provided")
}
url := args[0]
return Download(url, options)
},
})
rootCmd.AddCommand(&cobra.Command{
Use: "Copy",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return errors.New("An url and a destination must be provided")
}
url := args[0]
destination := args[1]
return Copy(url, options, destination)
},
})
rootCmd.AddCommand(&cobra.Command{
Use: "Extract",
Aliases: []string{"Unzip", "UnzipSingleFile"},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return errors.New("An url, a file name and a destination must be provided")
}
url := args[0]
// All files
if len(args) == 2 {
destinationFolder := args[1]
return Extract(url, options, destinationFolder)
}
// Some files
extractedFiles := []files.ExtractedFile{}
for i := 1; i < len(args); i += 2 {
extractedFiles = append(extractedFiles, files.ExtractedFile{
Source: args[i],
Destination: args[i+1],
})
}
return ExtractFiles(url, options, extractedFiles)
},
})
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
}
}
// Download retrieves an url from the cache or downloads it if it's absent.
// Then, prints the file's path to stdout.
func Download(url string, options files.Options) error {
// Discard all the logs. We only want to output the path to the file
log.SetOutput(ioutil.Discard)
source, err := cache.Download(url, options, force)
if err != nil {
return err
}
fmt.Println(source)
return nil
}
// Copy retrieves an url from the cache or downloads it if it's absent.
// Then, copies the file to a destination path.
func Copy(url string, options files.Options, destination string) error {
// Discard all the logs. We only want to output the path to the file
if destination == "-" {
log.SetOutput(ioutil.Discard)
}
source, err := cache.Download(url, options, force)
if err != nil {
return err
}
log.Println("Copy", url, "to", destination)
return files.Copy(source, destination)
}
// Extract retrieves an url from the cache or downloads it if it's absent.
// Then, unzips the file to a destination directory.
func Extract(url string, options files.Options, destinationDirectory string) error {
source, err := cache.Download(url, options, force)
if err != nil {
return err
}
log.Println("Extract", url, "to", destinationDirectory)
if urls.IsZipArchive(url) {
return zip.Extract(source, destinationDirectory)
}
if urls.IsTarArchive(url) {
return tar.Extract(url, source, destinationDirectory)
}
return errors.New("Unsupported archive: " + source)
}
// ExtractFiles retrieves an url from the cache or downloads it if it's absent.
// Then, unzips some files from that zip to a destination path.
func ExtractFiles(url string, options files.Options, files []files.ExtractedFile) error {
source, err := cache.Download(url, options, force)
if err != nil {
return err
}
for _, file := range files {
log.Println("Extract", file.Source, "from", url, "to", file.Destination)
}
if urls.IsZipArchive(url) {
return zip.ExtractFiles(source, files)
}
if urls.IsTarArchive(url) {
return tar.ExtractFiles(url, source, files)
}
return errors.New("Unsupported archive: " + source)
}