Skip to content

Commit

Permalink
refactor: Disable piping of stderr and stdout to support non-line…
Browse files Browse the repository at this point in the history
… based guests in favor of plain stdout and stderr attach
  • Loading branch information
pojntfx committed Apr 19, 2024
1 parent 0d47d72 commit 0947793
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 49 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/hydrun.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
52 changes: 9 additions & 43 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package main

import (
"bufio"
"context"
"fmt"
"log"
"os"
osutils "os"
"os/exec"
"strings"

Expand Down Expand Up @@ -65,7 +63,7 @@ Usage: %s [OPTION...] "<COMMAND...>"
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)
}
Expand Down Expand Up @@ -140,6 +138,8 @@ Usage: %s [OPTION...] "<COMMAND...>"
}

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`,
Expand Down Expand Up @@ -185,53 +185,19 @@ Usage: %s [OPTION...] "<COMMAND...>"
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)
}

Expand Down

0 comments on commit 0947793

Please sign in to comment.