|
| 1 | +package apiv2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "github.com/jedib0t/go-pretty/v6/table" |
| 8 | + "github.com/jedib0t/go-pretty/v6/text" |
| 9 | + "github.com/k0kubun/go-ansi" |
| 10 | + progressbar "github.com/schollz/progressbar/v3" |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +func ServeRequest( reqType, repoName, repoType, token, action string, files []string, private bool ) error { |
| 15 | + |
| 16 | + client := HuggingFaceClient{ token } |
| 17 | + |
| 18 | + switch reqType { |
| 19 | + case "meta": |
| 20 | + //fmt.Println("getting metadata...") |
| 21 | + meta, err := client.GetMetadata( repoType, repoName ) |
| 22 | + if err != nil { |
| 23 | + return err |
| 24 | + } |
| 25 | + //fmt.Println("collected metadata...") |
| 26 | + description := strings.Replace( strings.Replace( meta.Description, "\n\n", "\n", -1 ), "\n\t\n", "\n", -1 ) |
| 27 | + |
| 28 | + tw := table.NewWriter() |
| 29 | + tw.AppendHeader( table.Row{ meta.Type[3:] + " " + meta.Name + " by " + meta.Creator["name"] } ) |
| 30 | + tw.AppendRow( table.Row{ "URL", meta.URL } ) |
| 31 | + tw.AppendRow( table.Row{ "License", meta.License } ) |
| 32 | + tw.AppendRow( table.Row{ "Description", description } ) |
| 33 | + |
| 34 | + kw := "" |
| 35 | + for _, k := range meta.Keywords { |
| 36 | + if strings.Contains( k, "Region" ) { |
| 37 | + k = k[ strings.Index(k, "Region") :] |
| 38 | + } |
| 39 | + kw += k + " " |
| 40 | + } |
| 41 | + |
| 42 | + tw.AppendFooter( table.Row{ "Keywords", kw } ) |
| 43 | + tw.SetStyle( table.StyleColoredDark ) |
| 44 | + tw.Style().Color.Header = text.Colors{ text.BgCyan, text.FgWhite, text.Bold } |
| 45 | + tw.Style().Color.Footer = text.Colors{ text.BgCyan, text.FgWhite, text.Bold } |
| 46 | + |
| 47 | + fmt.Printf("%s\n", tw.Render()) |
| 48 | + |
| 49 | + case "download": |
| 50 | + bar := setupProgressBar( int(len(files)), "Downloading..." ) |
| 51 | + for _, file := range files { |
| 52 | + data, err := client.DownloadFile( repoType, repoName, file ) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + }else { |
| 56 | + if err := os.WriteFile( file, data, 0666 ); err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + } |
| 60 | + bar.Add(1) |
| 61 | + } |
| 62 | + fmt.Println() |
| 63 | + case "upload": |
| 64 | + bar := setupProgressBar( int(len(files)), "Uploading..." ) |
| 65 | + |
| 66 | + for _, file := range files { |
| 67 | + contents, err := os.ReadFile( file ) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + if err := client.UploadFile( repoType, repoName, file, contents ); err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + bar.Add(1) |
| 76 | + } |
| 77 | + fmt.Println() |
| 78 | + |
| 79 | + case "repo": |
| 80 | + switch action { |
| 81 | + case "create": |
| 82 | + if err := client.CreateRepo( repoType, repoName, private ); err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + fmt.Println("Repo was created") |
| 86 | + case "delete": |
| 87 | + if err := client.DeleteRepo( repoName ); err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + fmt.Println("Repo was deleted") |
| 91 | + } |
| 92 | + case "repo-files": |
| 93 | + switch action { |
| 94 | + case "delete": |
| 95 | + if len(files) < 1 { |
| 96 | + return nil |
| 97 | + } |
| 98 | + if err := client.DeleteFile( repoType, repoName, files[0] ); err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + case "list": |
| 102 | + file := "/" |
| 103 | + if len(files) > 0 { |
| 104 | + if files[0] != "" { |
| 105 | + file = files[0] |
| 106 | + } |
| 107 | + } |
| 108 | + repoFiles, err := client.ListFilesInRepo( repoType, repoName, file, false ) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + tw := table.NewWriter() |
| 114 | + tw.AppendHeader( table.Row{"Directory listing of " + file} ) |
| 115 | + for _, file := range repoFiles { |
| 116 | + tw.AppendRow( table.Row{ file } ) |
| 117 | + } |
| 118 | + tw.AppendFooter( table.Row{"Total", fmt.Sprintf("%d", len(repoFiles))} ) |
| 119 | + tw.SetStyle( table.StyleColoredDark ) |
| 120 | + tw.Style().Color.Header = text.Colors{ text.BgCyan, text.FgWhite, text.Bold } |
| 121 | + tw.Style().Color.Footer = text.Colors{ text.BgCyan, text.FgWhite, text.Bold } |
| 122 | + |
| 123 | + fmt.Printf("%s\n", tw.Render()) |
| 124 | + default: |
| 125 | + return fmt.Errorf("Error: no such action: %s. Choose from {delete,list}\n") |
| 126 | + } |
| 127 | + default: |
| 128 | + return fmt.Errorf("Invalid command: %s\n", reqType) |
| 129 | + } |
| 130 | + return nil |
| 131 | +} |
| 132 | + |
| 133 | +func setupProgressBar( x int, msg string ) *progressbar.ProgressBar { |
| 134 | + bar := progressbar.NewOptions( x, |
| 135 | + progressbar.OptionSetWriter( ansi.NewAnsiStdout()), |
| 136 | + progressbar.OptionEnableColorCodes(true), |
| 137 | + progressbar.OptionSetDescription( |
| 138 | + fmt.Sprintf("[cyan] %s [reset]", msg ), |
| 139 | + ), |
| 140 | + progressbar.OptionSetTheme( progressbar.Theme{ |
| 141 | + Saucer: "[green]=[reset]", |
| 142 | + SaucerHead: "[yellow]>[reset]", |
| 143 | + SaucerPadding: " ", |
| 144 | + BarStart: "[", |
| 145 | + BarEnd: "]", |
| 146 | + } ), |
| 147 | + ) |
| 148 | + return bar |
| 149 | +} |
0 commit comments