itd/cmd/itctl/resources.go

58 lines
1.2 KiB
Go
Raw Permalink Normal View History

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
}
2023-01-03 06:30:17 +00:00
progCh, err := client.FS().LoadResources(ctx, path)
2022-08-30 20:01:36 +00:00
if err != nil {
return err
}
for evt := range progCh {
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
}