Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support capturing stdout #82

Open
jaulz opened this issue Jun 23, 2023 · 2 comments
Open

Support capturing stdout #82

jaulz opened this issue Jun 23, 2023 · 2 comments
Labels
Type: feature request New feature or request

Comments

@jaulz
Copy link

jaulz commented Jun 23, 2023

Is it possible to read any file from the VM? I tried to use cat via the run function but the run function does only return the exit code (I assume). I see that the output is logged to the console but it would be great to have a callback for the output as well.

@alexp-sssup
Copy link
Member

There is currently no APIs to either read files from the disk or to capture stdout. As things stand today your best bet is to hook the console output.

For such small questions I would like to encourage you to join our discord: https://discord.gg/yTNZgySKGa

I'll keep this bug open as a feature request

@alexp-sssup alexp-sssup changed the title Read file Support capturing stdout Jun 23, 2023
@bates64 bates64 added the Type: feature request New feature or request label Aug 4, 2023
@bates64
Copy link
Member

bates64 commented Aug 17, 2023

For the time being, you can capture console output (i.e. both stdout and stderr) with the following snippet. This works by capturing all output into an array bufs whilst the command runs, and then clearing it when it finishes.

const cx = await CheerpXApp.create(/* ... */);

const decoder = new TextDecoder("utf-8");
let bufs = [];
const writeCharCode = cx.setCustomConsole(buf => bufs.push(buf), 60, 30);

async function run(command, args, opts) {
  const code = await cx.run(command, args, opts);
  
  const output = bufs.map(buf => decoder.decode(buf)).join("");
  bufs = [];
  
  return { code, output };
}

// Run cat and capture its output
const cat = await run("/bin/cat", ["/etc/hosts"], {
  env: ["HOME=/home/user", "TERM=xterm", "USER=user", "SHELL=/bin/bash", "EDITOR=vim", "LANG=en_US.UTF-8", "LC_ALL=C"],
  cwd: "/",
  uid: 1000,
  gid: 1000,
});
console.log("cat exited with code:", cat.code);
console.log("cat output:", cat.output);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: feature request New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants