From 6f87980d4b70ab3eb7fa11c9457c32af2eabbab6 Mon Sep 17 00:00:00 2001 From: Arsen Musayelyan Date: Tue, 30 Aug 2022 13:01:36 -0700 Subject: [PATCH] Add resource loading to itctl --- cmd/itctl/main.go | 29 ++++++++++++++++++------- cmd/itctl/resources.go | 48 ++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 3 ++- 4 files changed, 72 insertions(+), 10 deletions(-) create mode 100644 cmd/itctl/resources.go diff --git a/cmd/itctl/main.go b/cmd/itctl/main.go index b548126..0de3d53 100644 --- a/cmd/itctl/main.go +++ b/cmd/itctl/main.go @@ -1,11 +1,11 @@ package main import ( - "time" "context" "os" "os/signal" "syscall" + "time" "github.com/rs/zerolog" "github.com/rs/zerolog/log" @@ -24,17 +24,17 @@ func main() { syscall.SIGINT, syscall.SIGTERM, ) - + // This goroutine ensures that itctl will exit // at most 200ms after the user sends SIGINT/SIGTERM. go func() { <-ctx.Done() - time.Sleep(200*time.Millisecond) + time.Sleep(200 * time.Millisecond) os.Exit(0) }() app := cli.App{ - Name: "itctl", + Name: "itctl", HideHelpCommand: true, Flags: []cli.Flag{ &cli.StringFlag{ @@ -46,10 +46,23 @@ func main() { }, Commands: []*cli.Command{ { - Name: "help", + Name: "help", ArgsUsage: "", - Usage: "Display help screen for a command", - Action: helpCmd, + Usage: "Display help screen for a command", + Action: helpCmd, + }, + { + Name: "resources", + Aliases: []string{"res"}, + Usage: "Handle InfiniTime resource loading", + Subcommands: []*cli.Command{ + { + Name: "load", + ArgsUsage: "", + Usage: "Load an InifiniTime resources package", + Action: resourcesLoad, + }, + }, }, { Name: "filesystem", @@ -284,4 +297,4 @@ func isHelpCmd() bool { } } return false -} \ No newline at end of file +} diff --git a/cmd/itctl/resources.go b/cmd/itctl/resources.go new file mode 100644 index 0000000..6bbca0e --- /dev/null +++ b/cmd/itctl/resources.go @@ -0,0 +1,48 @@ +package main + +import ( + "path/filepath" + + "github.com/cheggaaa/pb/v3" + "github.com/urfave/cli/v2" + "go.arsenm.dev/infinitime" +) + +func resourcesLoad(c *cli.Context) error { + if c.Args().Len() == 0 { + 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) + + path, err := filepath.Abs(c.Args().Get(0)) + if err != nil { + return err + } + + progCh, err := client.LoadResources(c.Context, path) + if err != nil { + return err + } + + for evt := range progCh { + 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 +} diff --git a/go.mod b/go.mod index d7eb013..793fb3c 100644 --- a/go.mod +++ b/go.mod @@ -38,7 +38,7 @@ require ( github.com/gopherjs/gopherjs v1.17.2 // indirect github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect github.com/mattn/go-colorable v0.1.8 // indirect - github.com/mattn/go-runewidth v0.0.12 // indirect + github.com/mattn/go-runewidth v0.0.13 // indirect github.com/mitchellh/copystructure v1.2.0 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/mitchellh/reflectwalk v1.0.2 // indirect diff --git a/go.sum b/go.sum index 98aeb44..1812906 100644 --- a/go.sum +++ b/go.sum @@ -270,8 +270,9 @@ github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNx github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= -github.com/mattn/go-runewidth v0.0.12 h1:Y41i/hVW3Pgwr8gV+J23B9YEY0zxjptBuCWEaxmAOow= github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= +github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU= +github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mattn/go-sqlite3 v1.14.12 h1:TJ1bhYJPV44phC+IMu1u2K/i5RriLTPe+yc68XDJ1Z0= github.com/mattn/go-sqlite3 v1.14.12/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/metal3d/fyne-x v0.0.0-20220508095732-177117e583fb h1:+fP6ENsbd+BUOmD/kSjNtrOmi2vgJ/JfWDSWjTKmTVY=