Skip to content

Commit

Permalink
feat: allow running command from util
Browse files Browse the repository at this point in the history
  • Loading branch information
chetan committed Dec 6, 2024
1 parent 0ec462c commit 1d54e2c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 14 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/chetan/elapsed

go 1.17
go 1.23

require github.com/mitchellh/go-homedir v1.1.0
75 changes: 62 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import (
"bufio"
"flag"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"time"

"github.com/mitchellh/go-homedir"
Expand Down Expand Up @@ -77,18 +80,7 @@ func parseFlags() {
}
}

func main() {
parseFlags()
if !(showElapsed || showDelta) {
fmt.Println("error: must enable either elapsed or delta")
os.Exit(1)
}
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) != 0 {
fmt.Println("error: stdin not connected to a pipe")
os.Exit(1)
}

func run(r io.Reader) {
f := "["
if showTS != "" {
f += "%s"
Expand All @@ -107,7 +99,7 @@ func main() {
}
f += "] %s"

reader := bufio.NewReader(os.Stdin)
reader := bufio.NewReader(r)
start := time.Now()
last := time.Now()
for {
Expand Down Expand Up @@ -151,3 +143,60 @@ func main() {
}
}
}

func getReader() (io.Reader, []string) {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) != 0 {
// look for cmd
args := os.Args[1:]
if len(args) != 0 {
for i, arg := range args {
if arg == "--" && i+1 < len(args) && args[i+1] != "" {
return nil, args[i+1:]
}
}
}

fmt.Println("error: stdin not connected to a pipe")
os.Exit(1)
}

return os.Stdin, nil
}

func runCmd(args []string) {
cmd := exec.Command(args[0], args[1:]...)
r, w := io.Pipe()
cmd.Stdout = w
cmd.Stderr = w
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
err := cmd.Start()
if err != nil {
fmt.Println("error running command: ", err)
}
go run(r)
err = cmd.Wait()
if err != nil {
fmt.Println("error waiting for command: ", err)
}
os.Exit(cmd.ProcessState.ExitCode())
}()
wg.Wait()
}

func main() {
parseFlags()
if !(showElapsed || showDelta) {
fmt.Println("error: must enable either elapsed or delta")
os.Exit(1)
}

r, args := getReader()
if args != nil {
runCmd(args)
} else {
run(r)
}
}

0 comments on commit 1d54e2c

Please sign in to comment.