Skip to content

Commit 2edc764

Browse files
authored
Update main.go
1 parent c58b37d commit 2edc764

File tree

1 file changed

+190
-120
lines changed

1 file changed

+190
-120
lines changed

cmd-cli/main.go

+190-120
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,30 @@ import (
44
"flag"
55
"fmt"
66
"os"
7+
"io/fs"
78
"strings"
89
api "hugger/apiv2"
10+
"runtime"
911
"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"
1314
)
1415

16+
type HError struct {
17+
Error string `json:"error"`
18+
}
19+
1520
func main() {
21+
22+
// check for updates
23+
api.UpdateApp()
24+
1625
if len(os.Args) < 2 {
1726
printHelp()
1827
os.Exit(1)
1928
}
2029

30+
2131
switch os.Args[1] {
2232
case "help":
2333
printHelp()
@@ -29,6 +39,8 @@ func main() {
2939
handleDownload()
3040
case "upload":
3141
handleUpload()
42+
case "repo":
43+
handleRepo()
3244
case "repo-files":
3345
handleRepoFiles()
3446
case "meta":
@@ -57,11 +69,20 @@ func printHelp() {
5769
fmt.Println(" -repo-type Type of the repository")
5870
fmt.Println(" -token A User Access Token generated from https://huggingface.co/settings/tokens")
5971
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("")
6080
fmt.Println(" repo-files Perform actions on repository files")
6181
fmt.Println(" Arguments:")
6282
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")
6586
fmt.Println(" -token A User Access Token generated from https://huggingface.co/settings/tokens")
6687
fmt.Println("")
6788
fmt.Println(" meta Show meta information about repository")
@@ -85,37 +106,15 @@ func handleMeta() {
85106
fmt.Println("meta subcommand requires repo_id, repo-type and token arguments")
86107
os.Exit(1)
87108
}
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)
110115
}
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())
118116
}
117+
119118
}
120119

121120

@@ -132,22 +131,16 @@ func handleDownload() {
132131
os.Exit(1)
133132
}
134133

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)
149142
}
150-
bar.Add(1)
143+
151144
}
152145
}
153146

@@ -164,101 +157,178 @@ func handleUpload() {
164157
fmt.Println("upload subcommand requires repo_id, filenames, and repo-type arguments")
165158
os.Exit(1)
166159
}
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)
177169
}
170+
}
171+
}
178172

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")
179180

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)
183194
}
184-
bar.Add(1)
185195
}
186196
}
187197

188198
func handleRepoFiles() {
189199

190200
repoFiles := flag.NewFlagSet("repo-files", flag.ExitOnError)
191201
repoID := repoFiles.String("repo-id", "", "Repository ID")
202+
repoType := repoFiles.String("repo-type", "", "Type of the repository")
192203
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")
194205
token := repoFiles.String("token", "", "User Access Token generated from https://huggingface.co/settings/tokens")
195206
repoFiles.Parse( os.Args[2:] )
196207

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")
199210
os.Exit(1)
200211
}
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+
}
201222

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+
}
207243
} else {
208-
fmt.Println("File successfully deleted")
244+
res = append( res, fn )
209245
}
210-
default:
211-
fmt.Println("Error: no such action:", *action)
212-
fmt.Println("Available actions: {delete}")
213246
}
247+
248+
249+
if len(res) == 0 {
250+
res = tmpres
251+
}
252+
return res
214253
}
215254

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+
216283
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+
" " }
255315
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 )
263333
}
264334
}

0 commit comments

Comments
 (0)