some reorga, added pretty progress bar

This commit is contained in:
2023-03-14 16:59:21 +01:00
parent e88399d76d
commit 3f91774a87
4 changed files with 107 additions and 62 deletions

View File

@@ -22,6 +22,7 @@ import (
"errors"
"fmt"
"github.com/imroc/req/v3"
"github.com/schollz/progressbar/v3"
"github.com/tlinden/up/upctl/cfg"
"os"
"path/filepath"
@@ -134,14 +135,20 @@ func UploadFiles(c *cfg.Config, args []string) error {
return err
}
// progres bar
bar := progressbar.Default(100)
var left float64
// actual post w/ settings
resp, err := rq.R.
SetFormData(map[string]string{
"expire": c.Expire,
}).
SetUploadCallbackWithInterval(func(info req.UploadInfo) {
fmt.Printf("\r%q uploaded %.2f%%", info.FileName, float64(info.UploadedSize)/float64(info.FileSize)*100.0)
fmt.Println()
left = float64(info.UploadedSize) / float64(info.FileSize) * 100.0
bar.Add(int(left))
//fmt.Printf("\r%q uploaded %.2f%%", info.FileName, float64(info.UploadedSize)/float64(info.FileSize)*100.0)
//fmt.Println()
}, 10*time.Millisecond).
Post(rq.Url)
@@ -149,7 +156,7 @@ func UploadFiles(c *cfg.Config, args []string) error {
return err
}
return printUploadsResponse(resp)
return RespondExtended(resp)
}
func HandleResponse(c *cfg.Config, resp *req.Response) error {
@@ -200,24 +207,7 @@ func List(c *cfg.Config, args []string) error {
return err
}
uploads := Uploads{}
if err := json.Unmarshal([]byte(resp.String()), &uploads); err != nil {
return errors.New("Could not unmarshall JSON response: " + err.Error())
}
if !uploads.Success {
return errors.New(uploads.Message)
}
// tablewriter
data := [][]string{}
for _, entry := range uploads.Entries {
data = append(data, []string{
entry.Id, entry.Expire, entry.Context, entry.Uploaded.Format("2006-01-02 15:04:05"),
})
}
return WriteTable([]string{"ID", "EXPIRE", "CONTEXT", "UPLOADED"}, data)
return RespondTable(resp)
}
func Delete(c *cfg.Config, args []string) error {
@@ -250,25 +240,5 @@ func Describe(c *cfg.Config, args []string) error {
return err
}
return printUploadsResponse(resp)
}
func printUploadsResponse(resp *req.Response) error {
uploads := Uploads{}
if err := json.Unmarshal([]byte(resp.String()), &uploads); err != nil {
return errors.New("Could not unmarshall JSON response: " + err.Error())
}
if !uploads.Success {
return errors.New(uploads.Message)
}
if uploads.Message != "" {
fmt.Println(uploads.Message)
}
WriteExtended(&uploads)
return nil
return RespondExtended(resp)
}

View File

@@ -18,22 +18,34 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package lib
import (
"encoding/json"
"errors"
"fmt"
"github.com/imroc/req/v3"
"github.com/olekukonko/tablewriter"
"os"
"time"
)
func WriteTable(headers []string, data [][]string) error {
// make a human readable version of the expire setting
func prepareExpire(expire string, start Timestamp) string {
switch expire {
case "asap":
return "On first access"
default:
return time.Unix(start.Unix()+int64(duration2int(expire)), 0).Format("2006-01-02 15:04:05")
}
return ""
}
// generic table writer
func WriteTable(headers []string, data [][]string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(headers)
table.AppendBulk(data)
// for _, row := range data.entries {
// table.Append(trimRow(row))
// }
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
@@ -47,21 +59,9 @@ func WriteTable(headers []string, data [][]string) error {
table.SetNoWhiteSpace(true)
table.Render()
return nil
}
func prepareExpire(expire string, start Timestamp) string {
switch expire {
case "asap":
return "On first access"
default:
return time.Unix(start.Unix()+int64(duration2int(expire)), 0).Format("2006-01-02 15:04:05")
}
return ""
}
// output like psql \x
func WriteExtended(uploads *Uploads) {
format := fmt.Sprintf("%%%ds: %%s\n", Maxwidth)
@@ -76,3 +76,58 @@ func WriteExtended(uploads *Uploads) {
fmt.Println()
}
}
// extract an Uploads{} struct from json response
func GetUploadsFromResponse(resp *req.Response) (*Uploads, error) {
uploads := Uploads{}
if err := json.Unmarshal([]byte(resp.String()), &uploads); err != nil {
return nil, errors.New("Could not unmarshall JSON response: " + err.Error())
}
if !uploads.Success {
return nil, errors.New(uploads.Message)
}
return &uploads, nil
}
// turn the Uploads{} struct into a table and print it
func RespondTable(resp *req.Response) error {
uploads, err := GetUploadsFromResponse(resp)
if err != nil {
return err
}
if uploads.Message != "" {
fmt.Println(uploads.Message)
}
// tablewriter
data := [][]string{}
for _, entry := range uploads.Entries {
data = append(data, []string{
entry.Id, entry.Expire, entry.Context, entry.Uploaded.Format("2006-01-02 15:04:05"),
})
}
WriteTable([]string{"ID", "EXPIRE", "CONTEXT", "UPLOADED"}, data)
return nil
}
// turn the Uploads{} struct into xtnd output and print it
func RespondExtended(resp *req.Response) error {
uploads, err := GetUploadsFromResponse(resp)
if err != nil {
return err
}
if uploads.Message != "" {
fmt.Println(uploads.Message)
}
WriteExtended(uploads)
return nil
}