2022-08-30 20:01:36 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-10-16 20:17:12 +00:00
|
|
|
"context"
|
2022-08-30 20:01:36 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/cheggaaa/pb/v3"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"go.arsenm.dev/infinitime"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourcesLoad(c *cli.Context) error {
|
2022-10-16 20:17:12 +00:00
|
|
|
return resLoad(c.Context, c.Args().Slice())
|
|
|
|
}
|
|
|
|
|
|
|
|
func resLoad(ctx context.Context, args []string) error {
|
|
|
|
if len(args) == 0 {
|
2022-08-30 20:01:36 +00:00
|
|
|
return cli.Exit("Command load requires one argument.", 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create progress bar templates
|
|
|
|
rmTmpl := `Removing {{string . "filename"}}`
|
|
|
|
upTmpl := `Uploading {{string . "filename"}} {{counters . }} B {{bar . "|" "-" (cycle .) " " "|"}} {{percent . }} {{rtime . "%s"}}`
|
|
|
|
// Start full bar at 0 total
|
|
|
|
bar := pb.ProgressBarTemplate(rmTmpl).Start(0)
|
|
|
|
|
2022-10-16 20:17:12 +00:00
|
|
|
path, err := filepath.Abs(args[0])
|
2022-08-30 20:01:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-16 20:17:12 +00:00
|
|
|
progCh, err := client.LoadResources(ctx, path)
|
2022-08-30 20:01:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for evt := range progCh {
|
2022-10-17 19:23:06 +00:00
|
|
|
if evt.Err != nil {
|
|
|
|
return evt.Err
|
|
|
|
}
|
|
|
|
|
2022-08-30 20:01:36 +00:00
|
|
|
if evt.Operation == infinitime.ResourceOperationRemoveObsolete {
|
|
|
|
bar.SetTemplateString(rmTmpl)
|
|
|
|
bar.Set("filename", evt.Name)
|
|
|
|
} else {
|
|
|
|
bar.SetTemplateString(upTmpl)
|
|
|
|
bar.Set("filename", evt.Name)
|
|
|
|
|
|
|
|
bar.SetTotal(evt.Total)
|
|
|
|
bar.SetCurrent(evt.Sent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bar.Finish()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|