@@ -4,20 +4,30 @@ import (
4
4
"flag"
5
5
"fmt"
6
6
"os"
7
+ "io/fs"
7
8
"strings"
8
9
api "hugger/apiv2"
10
+ "runtime"
9
11
"github.com/fatih/color"
10
- "github.com/jedib0t/go-pretty/v6/table"
11
- "github.com/jedib0t/go-pretty/v6/text"
12
- progressbar "github.com/schollz/progressbar/v3"
12
+ "path/filepath"
13
+ "encoding/json"
13
14
)
14
15
16
+ type HError struct {
17
+ Error string `json:"error"`
18
+ }
19
+
15
20
func main () {
21
+
22
+ // check for updates
23
+ api .UpdateApp ()
24
+
16
25
if len (os .Args ) < 2 {
17
26
printHelp ()
18
27
os .Exit (1 )
19
28
}
20
29
30
+
21
31
switch os .Args [1 ] {
22
32
case "help" :
23
33
printHelp ()
@@ -29,6 +39,8 @@ func main() {
29
39
handleDownload ()
30
40
case "upload" :
31
41
handleUpload ()
42
+ case "repo" :
43
+ handleRepo ()
32
44
case "repo-files" :
33
45
handleRepoFiles ()
34
46
case "meta" :
@@ -57,11 +69,20 @@ func printHelp() {
57
69
fmt .Println (" -repo-type Type of the repository" )
58
70
fmt .Println (" -token A User Access Token generated from https://huggingface.co/settings/tokens" )
59
71
fmt .Println ("" )
72
+ fmt .Println (" repo Perform actions on repository" )
73
+ fmt .Println (" Arguments:" )
74
+ fmt .Println (" -repo-id Repository ID" )
75
+ fmt .Println (" -repo-type Type of the repository" )
76
+ fmt .Println (" -action Action to perform on repo files ({delete,create})" )
77
+ fmt .Println (" -token A User Access Token generated from https://huggingface.co/settings/tokens" )
78
+ fmt .Println (" -private Create/delete private repository" )
79
+ fmt .Println ("" )
60
80
fmt .Println (" repo-files Perform actions on repository files" )
61
81
fmt .Println (" Arguments:" )
62
82
fmt .Println (" -repo-id Repository ID" )
63
- fmt .Println (" -action Action to perform on repo files" )
64
- fmt .Println (" -file File to do action with" )
83
+ fmt .Println (" -repo-type Type of the repository" )
84
+ fmt .Println (" -action Action to perform on repo files ({delete,list})" )
85
+ fmt .Println (" -file File to do action with. Optionally, you can pass a directory name here" )
65
86
fmt .Println (" -token A User Access Token generated from https://huggingface.co/settings/tokens" )
66
87
fmt .Println ("" )
67
88
fmt .Println (" meta Show meta information about repository" )
@@ -85,37 +106,15 @@ func handleMeta() {
85
106
fmt .Println ("meta subcommand requires repo_id, repo-type and token arguments" )
86
107
os .Exit (1 )
87
108
}
88
-
89
- client := api.HuggingFaceClient { * token }
90
- meta , err := client .GetMetadata ( * repoType , * repoID )
91
- if err != nil {
92
- fmt .Println ("Error: failed to get metadata for " + * repoID + " :" , err )
93
- } else {
94
-
95
-
96
- description := strings .Replace ( strings .Replace ( meta .Description , "\n \n " , "\n " , - 1 ), "\n \t \n " , "\n " , - 1 )
97
-
98
- tw := table .NewWriter ()
99
- tw .AppendHeader ( table.Row { meta .Type [3 :] + " " + meta .Name + " by " + meta .Creator ["name" ] } )
100
- tw .AppendRow ( table.Row { "URL" , meta .URL } )
101
- tw .AppendRow ( table.Row { "License" , meta .License } )
102
- tw .AppendRow ( table.Row { "Description" , description } )
103
-
104
- kw := ""
105
- for _ , k := range meta .Keywords {
106
- if strings .Contains ( k , "Region" ) {
107
- k = k [ strings .Index (k , "Region" ) :]
108
- }
109
- kw += k + " "
109
+ if err := api .ServeRequest ( "meta" , * repoID , * repoType , * token , "" , nil , false ); err != nil {
110
+ var e HError
111
+ if nerr := json .Unmarshal ( []byte (err .Error ()), & e ); nerr != nil {
112
+ fmt .Println (err )
113
+ } else {
114
+ fmt .Println ("Error:" , e .Error )
110
115
}
111
-
112
- tw .AppendFooter ( table.Row { "Keywords" , kw } )
113
- tw .SetStyle ( table .StyleColoredDark )
114
- tw .Style ().Color .Header = text.Colors { text .BgGreen , text .FgBlack }
115
- tw .Style ().Color .Footer = text.Colors { text .BgGreen , text .FgBlack }
116
-
117
- fmt .Printf ("%s\n " , tw .Render ())
118
116
}
117
+
119
118
}
120
119
121
120
@@ -132,22 +131,16 @@ func handleDownload() {
132
131
os .Exit (1 )
133
132
}
134
133
135
- client := api.HuggingFaceClient { * token }
136
- files := strings .Split ( * filenames , "," )
137
- bar := progressbar .Default ( int64 (len (files )) )
138
-
139
- for _ , file := range files {
140
- data , err := client .DownloadFile ( * repoType , * repoID , file )
141
- if err != nil {
142
- fmt .Println ("Error downloading file:" , err )
143
- os .Exit (2 )
144
- }else {
145
- if err := os .WriteFile ( file , data , 0666 ); err != nil {
146
- fmt .Println ("Error saving file:" , err )
147
- os .Exit (2 )
148
- }
134
+ files := strings .Split ( * filenames , "," ) //retrieveFiles( *filenames )
135
+ if err := api .ServeRequest ( "download" , * repoID , * repoType , * token , "" , files , false ); err != nil {
136
+ fmt .Println ("" )
137
+ var e HError
138
+ if nerr := json .Unmarshal ( []byte (err .Error ()), & e ); nerr != nil {
139
+ fmt .Println (err )
140
+ } else {
141
+ fmt .Println ("Error:" , e .Error )
149
142
}
150
- bar . Add ( 1 )
143
+
151
144
}
152
145
}
153
146
@@ -164,101 +157,178 @@ func handleUpload() {
164
157
fmt .Println ("upload subcommand requires repo_id, filenames, and repo-type arguments" )
165
158
os .Exit (1 )
166
159
}
167
-
168
- client := api.HuggingFaceClient { * token }
169
- files := strings .Split ( * filenames , "," )
170
- bar := progressbar .Default ( int64 (len (files )) )
171
-
172
- for _ , file := range files {
173
- contents , err := os .ReadFile ( file )
174
- if err != nil {
175
- fmt .Println ("Error reading file" , file , ":" , err )
176
- os .Exit (2 )
160
+
161
+ files := retrieveFiles ( * filenames )
162
+ if err := api .ServeRequest ( "upload" , * repoID , * repoType , * token , "" , files , false ); err != nil {
163
+ fmt .Println ("" )
164
+ var e HError
165
+ if nerr := json .Unmarshal ( []byte (err .Error ()), & e ); nerr != nil {
166
+ fmt .Println (err )
167
+ } else {
168
+ fmt .Println ("Error:" , e .Error )
177
169
}
170
+ }
171
+ }
178
172
173
+ func handleRepo () {
174
+ repo := flag .NewFlagSet ("repo" , flag .ExitOnError )
175
+ repoID := repo .String ("repo-id" , "" , "Repository ID" )
176
+ repoType := repo .String ("repo-type" , "" , "Type of the repository" )
177
+ action := repo .String ("action" , "" , "Action to perform on repo files" )
178
+ token := repo .String ("token" , "" , "User Access Token generated from https://huggingface.co/settings/tokens" )
179
+ private := repo .Bool ("private" , false , "Flag for private repositories" )
179
180
180
- if err := client .UploadFile ( * repoType , * repoID , file , contents ); err != nil {
181
- fmt .Println ("Error downloading file:" , err )
182
- os .Exit (2 )
181
+ repo .Parse ( os .Args [2 :] )
182
+
183
+ if * repoID == "" || * repoType == "" || * action == "" || * token == "" {
184
+ fmt .Println ("repo subcommand requires repo-id,repo-type, action and token arguments" )
185
+ os .Exit (1 )
186
+ }
187
+ if err := api .ServeRequest ( "repo" , * repoID , * repoType , * token , * action , nil , * private ); err != nil {
188
+ //fmt.Println("")
189
+ var e HError
190
+ if nerr := json .Unmarshal ( []byte (err .Error ()), & e ); nerr != nil {
191
+ fmt .Println (err )
192
+ } else {
193
+ fmt .Println ("Error:" , e .Error )
183
194
}
184
- bar .Add (1 )
185
195
}
186
196
}
187
197
188
198
func handleRepoFiles () {
189
199
190
200
repoFiles := flag .NewFlagSet ("repo-files" , flag .ExitOnError )
191
201
repoID := repoFiles .String ("repo-id" , "" , "Repository ID" )
202
+ repoType := repoFiles .String ("repo-type" , "" , "Type of the repository" )
192
203
action := repoFiles .String ("action" , "" , "Action to perform on repo files" )
193
- file := repoFiles .String ("file" , "" , "File to do some action with" )
204
+ file := repoFiles .String ("file" , "" , "File to do some action with. Optionally, you can pass directory here " )
194
205
token := repoFiles .String ("token" , "" , "User Access Token generated from https://huggingface.co/settings/tokens" )
195
206
repoFiles .Parse ( os .Args [2 :] )
196
207
197
- if * repoID == "" || * action == "" || * token == "" || * file == "" {
198
- fmt .Println ("repo-files subcommand requires repo_id and action arguments" )
208
+ if * repoID == "" || * repoType == "" || * action == "" || * token == "" {
209
+ fmt .Println ("repo-files subcommand requires repo-id, repo-type, token and action arguments" )
199
210
os .Exit (1 )
200
211
}
212
+ files := retrieveFiles ( * file )
213
+ if err := api .ServeRequest ( "repo-files" , * repoID , * repoType , * token , * action , files , false ); err != nil {
214
+ var e HError
215
+ if nerr := json .Unmarshal ( []byte (err .Error ()), & e ); nerr != nil {
216
+ fmt .Println (err )
217
+ } else {
218
+ fmt .Println ("Error:" , e .Error )
219
+ }
220
+ }
221
+ }
201
222
202
- client := api.HuggingFaceClient { * token }
203
- switch * action {
204
- case "delete" :
205
- if err := client .DeleteFile ( "dataset" , * repoID , * file ); err != nil {
206
- fmt .Println ("Failed to delete file:" , err )
223
+
224
+ func retrieveFiles ( filenames string ) []string {
225
+
226
+ res := []string {}
227
+ tmpres := strings .Split ( filenames , "," )
228
+ for _ , fn := range tmpres {
229
+
230
+ fileInfo , err := os .Stat (fn )
231
+ if err == nil {
232
+ if fileInfo .IsDir () {
233
+ // recursively retrieve all files from folder
234
+ dirfiles := listAllFiles ( fn )
235
+ if dirfiles != nil {
236
+ res = append ( res , dirfiles ... )
237
+ } else {
238
+ res = append ( res , fn )
239
+ }
240
+ } else {
241
+ res = append ( res , fn )
242
+ }
207
243
} else {
208
- fmt . Println ( "File successfully deleted" )
244
+ res = append ( res , fn )
209
245
}
210
- default :
211
- fmt .Println ("Error: no such action:" , * action )
212
- fmt .Println ("Available actions: {delete}" )
213
246
}
247
+
248
+
249
+ if len (res ) == 0 {
250
+ res = tmpres
251
+ }
252
+ return res
214
253
}
215
254
255
+ func listAllFiles ( folder string ) []string {
256
+
257
+ res := []string {}
258
+
259
+ err := filepath .WalkDir ( folder , func (path string , d fs.DirEntry , err error ) error {
260
+ if path != folder {
261
+ res = append ( res , path )
262
+ }
263
+ return nil
264
+ })
265
+ if err != nil {
266
+ return nil
267
+ }
268
+ return res
269
+ }
270
+
271
+ func isTerminal () bool {
272
+ switch runtime .GOOS {
273
+ case "windows" :
274
+ return os .Getenv ("TERM" ) == "xterm" || os .Getenv ("ANSICON" ) != ""
275
+ case "darwin" , "linux" :
276
+ return true
277
+ default :
278
+ return false
279
+ }
280
+ }
281
+
282
+
216
283
func Banner () {
217
- lightyellow := color .New (color .FgCyan , color .Italic ).SprintFunc ()
218
- yellow := color .New ( color .FgYellow , color .Bold ).SprintFunc ()
219
- red := color .New (color .FgRed ).SprintFunc ()
220
- lightred := color .New (color .FgRed , color .Bold ).SprintFunc ()
221
- white := color .New (color .FgWhite ).SprintFunc ()
222
- black := color .New (color .FgBlack ).SprintFunc ()
223
-
224
- banner := `
225
- @@@@@@@@@@@@@@@@@
226
- @@@@@@@@%###########&@@@@@@@@@
227
- @@@@@@######%%%%%%%%%%%%%%%######@@@@@@
228
- @@@@@####%%%%%%%%%%%%%%%%%%%%%%%%%%%####@@@@@
229
- @@@@###%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%###@@@@@
230
- @@@###%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%###@@@@
231
- @@@###%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%###@@@
232
- @@@##%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%##@@@@
233
- @@@###%%%%%%%%%%%%.....%%%%%%%%%%%%%%%%.....%%%%%%%%%%%%%###@@@
234
- @@@###%%%%%%%%%%%%........%%%%%%%%%%%%........%%%%%%%%%%%%%##%@@@
235
- @@@##%%%%%%#####%%....%%.%%%%%%%%%%%%%%.%%....%%%#####%%%%%%##@@@@
236
- @@@##%%%%%%%#####%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%####%%%%%%%##@@@
237
- @@@##%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%##@@@
238
- @@@##%%%%%%%%%%%%%%%%%%,%%%%%%%%%%%%%%%%%%,,%%%%%%%%%%%%%%%%%%##@@@
239
- @@@##%%%%%%%%%%%%%%%%%%,,,,,,,%%%%%%%,,,,,,,%%%%%%%%%%%%%%%%%%##@@@
240
- @@@##%%%%%%%%%%%%%%%%%%,,,,,,,,,,,,,,,,,,,,%%%%%%%%%%%%%%%%%%%##@@@
241
- @@@##%#####%%%%%%%%%%%%%,,,,///,,,///,,,,%%%%%%%%%%%%%%#######@@@@
242
- @@@@@@####%%%###%######%%%%%%/////////////%%%%%%######%###%%%%###@@@@@@
243
- @@@########%%%%%###%%%%###%%%%%%%%%%%%%%%%%%%%%%##%%%%###%%%%%########@@@@
244
- @@###%%%%%####%%%%##%%%%###%%%%%%%%%%%%%%%%%%%###%%%%##%%%%%###%%%%%%##@@@
245
- @@@@####%%%%%%###%%%###%%%%###%%%%%%%%%%%%%%%###%%%%###%%%###%%%%%%####@@@@
246
- @@###%%%####%%%%%##%%%%%%%%%%###%%%%%%%%%%%###%%%%%%%%%%##%%%%%####%%%%##@@@
247
- @@@###%%%%%%%###%%%%%%%%%%%%%%%##%%%%%%%%%##%%%%%%%%%%%%%%%%##%%%%%%%###@@@@
248
- @@@##%%%####%%%%%%%%%%%%%%%%%%%%##%%%%%%%##%%%%%%%%%%%%%%%%%%%%####%%%###@@@
249
- @@@###%%%%%%%%%%%%%%%%%%%%%%%%%##%%%%%%%%%##%%%%%%%%%%%%%%%%%%%%%%%%%###@@@@
250
- @@@@######%%%%%%%%%%%%%%%%%#################%%%%%%%%%%%%%%%%%######@@@@@
251
- @@@@@@@@################%@@@@@@@@@@@@@@@@################@@@@@@@@@
252
- @@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@
253
- `
254
- lines := strings .Split ( banner , "\n " )
284
+
285
+ lines := []string {"" ,
286
+ " @@@@@@@@@@@@@@@@@ " ,
287
+ " @@@@@@@@#############@@@@@@@@@ " ,
288
+ " @@@@@@######%%%%%%%%%%%%%%%######@@@@@@ " ,
289
+ " @@@@@####%%%%%%%%%%%%%%%%%%%%%%%%%%%####@@@@@ " ,
290
+ " @@@@###%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%###@@@@@ " ,
291
+ " @@@###%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%###@@@@ " ,
292
+ " @@@###%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%###@@@ " ,
293
+ " @@@##%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%##@@@@ " ,
294
+ " @@@###%%%%%%%%%%%%.....%%%%%%%%%%%%%%%%.....%%%%%%%%%%%%%###@@@ " ,
295
+ " @@@###%%%%%%%%%%%%........%%%%%%%%%%%%........%%%%%%%%%%%%%###@@@ " ,
296
+ " @@@##%%%%%%#####%%....%%.%%%%%%%%%%%%%%.%%....%%%#####%%%%%%##@@@@ " ,
297
+ " @@@##%%%%%%%#####%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%####%%%%%%%##@@@ " ,
298
+ " @@@##%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%##@@@ " ,
299
+ " @@@##%%%%%%%%%%%%%%%%%%,%%%%%%%%%%%%%%%%%%,,%%%%%%%%%%%%%%%%%%##@@@ " ,
300
+ " @@@##%%%%%%%%%%%%%%%%%%,,,,,,,%%%%%%%,,,,,,,%%%%%%%%%%%%%%%%%%##@@@ " ,
301
+ " @@@##%%%%%%%%%%%%%%%%%%,,,,,,,,,,,,,,,,,,,,%%%%%%%%%%%%%%%%%%%##@@@ " ,
302
+ " @@@##%#####%%%%%%%%%%%%%,,,,///,,,///,,,,%%%%%%%%%%%%%%#######@@@@ " ,
303
+ " @@@@@@####%%%###%######%%%%%%/////////////%%%%%%######%###%%%%###@@@@@@ " ,
304
+ " @@@########%%%%%###%%%%###%%%%%%%%%%%%%%%%%%%%%%##%%%%###%%%%%########@@@@ " ,
305
+ " @@###%%%%%####%%%%##%%%%###%%%%%%%%%%%%%%%%%%%###%%%%##%%%%%###%%%%%%##@@@ " ,
306
+ " @@@@####%%%%%%###%%%###%%%%###%%%%%%%%%%%%%%%###%%%%###%%%###%%%%%%####@@@@ " ,
307
+ " @@###%%%####%%%%%##%%%%%%%%%%###%%%%%%%%%%%###%%%%%%%%%%##%%%%%####%%%%##@@@ " ,
308
+ " @@@###%%%%%%%###%%%%%%%%%%%%%%%##%%%%%%%%%##%%%%%%%%%%%%%%%%##%%%%%%%###@@@@ " ,
309
+ " @@@##%%%####%%%%%%%%%%%%%%%%%%%%##%%%%%%%##%%%%%%%%%%%%%%%%%%%%####%%%###@@@ " ,
310
+ " @@@###%%%%%%%%%%%%%%%%%%%%%%%%%##%%%%%%%%%##%%%%%%%%%%%%%%%%%%%%%%%%%###@@@@ " ,
311
+ " @@@@######%%%%%%%%%%%%%%%%%#################%%%%%%%%%%%%%%%%%######@@@@@ " ,
312
+ " @@@@@@@@#################@@@@@@@@@@@@@@@@################@@@@@@@@@ " ,
313
+ " @@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@ " ,
314
+ " " }
255
315
for _ , l := range lines {
256
- l = strings .Replace (l , "@" , white ("@" ), - 1 )
257
- l = strings .Replace (l , "%" , yellow ("%" ), - 1 )
258
- l = strings .Replace (l , "#" , lightyellow ("#" ), - 1 )
259
- l = strings .Replace (l , "," , red ("," ), - 1 )
260
- l = strings .Replace (l , "/" , lightred ("/" ), - 1 )
261
- l = strings .Replace (l , "." , black ("@" ), - 1 )
262
- fmt .Println (l )
316
+ if isTerminal () { // if terminal supports ANSI colors, make banner prettier
317
+
318
+ lightyellow := color .New (color .FgCyan , color .Italic ).SprintFunc ()
319
+ yellow := color .New ( color .FgYellow , color .Bold ).SprintFunc ()
320
+ red := color .New (color .FgRed ).SprintFunc ()
321
+ lightred := color .New (color .FgRed , color .Bold ).SprintFunc ()
322
+ white := color .New (color .FgWhite ).SprintFunc ()
323
+ black := color .New (color .FgBlack ).SprintFunc ()
324
+
325
+ l = strings .Replace (l , "@" , white ("@" ), - 1 )
326
+ l = strings .Replace (l , "%" , yellow ("%" ), - 1 )
327
+ l = strings .Replace (l , "#" , lightyellow ("#" ), - 1 )
328
+ l = strings .Replace (l , "," , red ("," ), - 1 )
329
+ l = strings .Replace (l , "/" , lightred ("/" ), - 1 )
330
+ l = strings .Replace (l , "." , black ("@" ), - 1 )
331
+ }
332
+ fmt .Println ( l )
263
333
}
264
334
}
0 commit comments