From 09477931ef37c96b03a80756c387be8c261b8085 Mon Sep 17 00:00:00 2001 From: Felicitas Pojtinger Date: Thu, 18 Apr 2024 23:39:47 -0700 Subject: [PATCH] refactor: Disable piping of `stderr` and `stdout` to support non-line based guests in favor of plain stdout and stderr attach --- .github/workflows/hydrun.yaml | 4 +-- README.md | 8 +++--- main.go | 52 ++++++----------------------------- 3 files changed, 15 insertions(+), 49 deletions(-) diff --git a/.github/workflows/hydrun.yaml b/.github/workflows/hydrun.yaml index 393291e..3f54a23 100644 --- a/.github/workflows/hydrun.yaml +++ b/.github/workflows/hydrun.yaml @@ -16,9 +16,9 @@ jobs: - name: Set up Docker Buildx uses: docker/setup-buildx-action@v1 - name: Set up Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v5 with: - go-version: 1.16 + go-version: "1.22.2" - name: Build with hydrun run: go run main.go -o golang ./Hydrunfile - name: Publish branch preview release to GitHub releases diff --git a/README.md b/README.md index 6d5b6a8..1009c04 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ PS> Invoke-WebRequest https://github.com/pojntfx/hydrun/releases/latest/download You can find binaries for more operating systems and architectures on [GitHub releases](https://github.com/pojntfx/hydrun/releases). -## Usage +## Tutorial Before continuing, please ensure that you have both [Docker buildx](https://github.com/docker/buildx) and [qemu-user-static](https://github.com/multiarch/qemu-user-static) installed on the host. @@ -133,7 +133,7 @@ hello-world.linux-aarch64 hello-world.linux-x86_64 Hydrunfile main.c Most of the time you'll probably want to use the `Hydrunfile` to install toolchains/dependencies and then call your `Makefile`, the Go compiler, `cargo` etc. -For an example, check out [pojntfx/liwasc](https://github.com/pojntfx/liwasc). +For an example, check out [pojntfx/panrpc](https://github.com/pojntfx/panrpc). ### Usage in GitHub Actions @@ -174,7 +174,7 @@ jobs: *.linux* ``` -For an example, check out [pojntfx/liwasc](https://github.com/pojntfx/liwasc). +For an example, check out [pojntfx/panrpc](https://github.com/pojntfx/panrpc). ## Reference @@ -217,6 +217,6 @@ If you want to quickly cross-compile your Go app, check out [bagop](https://gith ## License -hydrun (c) 2021 Felicitas Pojtinger and contributors +hydrun (c) 2024 Felicitas Pojtinger and contributors SPDX-License-Identifier: AGPL-3.0 diff --git a/main.go b/main.go index 8f47d75..9c0e521 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,10 @@ package main import ( - "bufio" "context" "fmt" "log" "os" - osutils "os" "os/exec" "strings" @@ -65,7 +63,7 @@ Usage: %s [OPTION...] "" arches := strings.Split(*archFlag, ",") oses := strings.Split(*osFlag, ",") command := strings.Join(pflag.Args(), " ") - pwd, err := osutils.Getwd() + pwd, err := os.Getwd() if err != nil { log.Fatalln("could not get working directory:", err) } @@ -140,6 +138,8 @@ Usage: %s [OPTION...] "" } go func(t Target) { + defer sem.Release(1) + // Construct the arguments dockerArgs := fmt.Sprintf( `run %v%v--platform linux/%v%v %v /bin/sh -c`, @@ -185,53 +185,19 @@ Usage: %s [OPTION...] "" log.Println(cmd) } - // Handle interactivity - if *itFlag { - // Attach stdin, stdout and stderr - cmd.Stdin = osutils.Stdin - cmd.Stdout = osutils.Stdout - cmd.Stderr = osutils.Stderr - } else { - // Get stdout and stderr pipes - stdout, err := cmd.StdoutPipe() - if err != nil { - log.Fatalln("could not get stdout:", err) - } - stderr, err := cmd.StderrPipe() - if err != nil { - log.Fatalln("could not get stderr:", err) - } - - // Read from stderr and stdout - stdoutScanner := bufio.NewScanner(stdout) - stderrScanner := bufio.NewScanner(stderr) - - // Split into lines - stdoutScanner.Split(bufio.ScanLines) - stderrScanner.Split(bufio.ScanLines) + // Attach stdout and stderr + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr - // Print to stdout with prefix - prefix := fmt.Sprintf("%v/%v/%v", t.Architecture, t.OS, t.Command) - go func() { - for stdoutScanner.Scan() { - fmt.Println(prefix+"/stdout\t", stdoutScanner.Text()) - } - }() - - go func() { - for stderrScanner.Scan() { - fmt.Println(prefix+"/stderr\t", stderrScanner.Text()) - } - }() + // Attach stdin + if *itFlag { + cmd.Stdin = os.Stdin } // Run the command if err := cmd.Run(); err != nil { log.Fatalln("could not run command:", err) } - - // Release lock - sem.Release(1) }(target) }