Skip to content

feat: add stdinContent parameter to shell commands #332

New issue

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

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

Already on GitHub? # to your account

Merged
merged 8 commits into from
Mar 20, 2025

Conversation

bhouston
Copy link
Member

Add stdinContent parameter to shell commands

This PR implements the solution for issue #301 by adding a new stdinContent parameter to the shellStart and shellExecute functions, which allows passing content directly to shell commands via stdin.

Changes

  • Added stdinContent parameter to shellStart and shellExecute functions
  • Implemented cross-platform approach using base64 encoding:
    • For Windows: Uses PowerShell's encoding capabilities
    • For POSIX (Linux/macOS): Uses base64 encoding through pipes
  • Updated GitHub mode instructions to use stdinContent instead of temporary files
  • Added tests for the new functionality
  • Added example test files demonstrating usage

Benefits

  1. Simplifies GitHub Integration: No need to create temporary files for GitHub CLI commands
  2. Cross-Platform Compatibility: Works consistently on Windows, macOS, and Linux
  3. Preserves Formatting: Correctly handles special characters, newlines, and formatting
  4. Cleaner Code: Eliminates the need for file creation/cleanup logic

Testing

The implementation has been tested with various input strings containing special characters, including:

  • Quotes (single and double)
  • Newlines
  • Shell operators
  • Variables and backticks
  • Complex combinations of the above

Example Usage

// Before (using temporary files)
const tempFile = path.join(os.tmpdir(), `issue-${Date.now()}.md`);
fs.writeFileSync(tempFile, issueContent);
await shellExecute({
  command: `gh issue create --title "Test Issue" --body-file "${tempFile}"`,
  description: 'Creating GitHub issue'
});
fs.unlinkSync(tempFile); // Cleanup needed

// After (using stdinContent)
await shellExecute({
  command: 'gh issue create --title "Test Issue" --body-stdin',
  stdinContent: issueContent,
  description: 'Creating GitHub issue'
});
// No cleanup needed!

Closes #301

This commit adds a new `stdinContent` parameter to the shellStart and shellExecute functions,
which allows passing content directly to shell commands via stdin. This is particularly useful
for GitHub CLI commands that accept stdin input.

Key changes:
- Add stdinContent parameter to shellStart and shellExecute
- Implement cross-platform approach using base64 encoding
- Update GitHub mode instructions to use stdinContent instead of temporary files
- Add tests for the new functionality
- Add example test files demonstrating usage

Closes #301
Fix TypeScript errors and test failures:
- Fix variable naming in shellStart.ts to avoid conflicts
- Simplify test files to ensure they build correctly
- Add timeout parameter to avoid test timeouts
Apply code formatting changes to test files:
- Fix formatting in test-gh-stdin.mjs
- Fix formatting in test-stdin-content.mjs
- Remove unused imports in shellExecute.test.ts
@bhouston bhouston merged commit df2a713 into main Mar 20, 2025
1 check passed
Copy link

🎉 This PR is included in version mycoder-agent-v1.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Copy link

🎉 This PR is included in version mycoder-v1.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can I auto encode string arguments to shell commands to simply gh operations?
1 participant