summaryrefslogtreecommitdiffstats
path: root/cmd
diff options
context:
space:
mode:
authorRose Hogenson <rosehogenson@posteo.net>2025-04-05 13:45:16 -0700
committerRose Hogenson <rosehogenson@posteo.net>2025-04-05 13:45:16 -0700
commit3f578882a651f74d4e701d8682e6c76752da76b5 (patch)
tree5df45d42b57127e088d3d71576938ad091669ea8 /cmd
parent2fec8a20fb8f485f2107deb00d29c99dfdfbde47 (diff)
downloadprogress-bar-3f578882a651f74d4e701d8682e6c76752da76b5.tar.zst
Refactor into reusable libraries
Diffstat (limited to 'cmd')
-rw-r--r--cmd/progress-bar/progress-bar.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/cmd/progress-bar/progress-bar.go b/cmd/progress-bar/progress-bar.go
new file mode 100644
index 0000000..65079ef
--- /dev/null
+++ b/cmd/progress-bar/progress-bar.go
@@ -0,0 +1,53 @@
+package main
+
+import (
+ "bytes"
+ "errors"
+ "flag"
+ "fmt"
+ "os"
+ "os/exec"
+ "strconv"
+ "time"
+
+ "gitlab.com/rhogenson/progress-bar/progress"
+)
+
+var n = flag.Duration("n", 2*time.Minute, "polling interval")
+
+func run() error {
+ args := flag.Args()
+ if len(args) < 2 {
+ return errors.New("usage error")
+ }
+ maxStr := args[0]
+ maxVal, err := strconv.Atoi(maxStr)
+ if err != nil {
+ return fmt.Errorf("max: %s", err)
+ }
+ cmd := args[1:]
+ bar := progress.New(maxVal)
+ for i := 0; ; i++ {
+ outputBytes, err := exec.Command(cmd[0], cmd[1:]...).Output()
+ if err != nil {
+ fmt.Println(err)
+ continue
+ }
+ output, err := strconv.Atoi(string(bytes.TrimSuffix(outputBytes, []byte("\n"))))
+ if err != nil {
+ fmt.Println(err)
+ continue
+ }
+ bar.Set(output)
+ bar.Print()
+ time.Sleep(*n)
+ }
+}
+
+func main() {
+ flag.Parse()
+ if err := run(); err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ os.Exit(1)
+ }
+}