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

feat: add support for piped input #66

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ Examples:
octoherd/cli
octoherd run -S path/to/script.js -T $TOKEN -R Avoid any prompts
octoherd/cli --octoherd-bypass-confirms
cat list.txt | octoherd run -S path/to/script.js -T $TOKEN
```

When piping a list of repositories, lines starting with a comment delimiter such as `#` or `//` will be ignored.

The `script` must export a `script` function which takes three parameters:

```js
Expand Down
6 changes: 6 additions & 0 deletions bin/octoherd.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import yargs from "yargs";
import { hideBin } from "yargs/helpers";

import chalk from "chalk";
import getStdin from 'get-stdin';

import { octoherd } from "../index.js";
import { VERSION } from "../version.js";
import { parseInput } from "./parse-input.js";
import runCommand from "./commands/run.js";

const EPILOG = chalk.gray(`Questions? Ideas? Feedback?
Expand All @@ -20,6 +22,10 @@ const argv = await yargs(hideBin(process.argv))
.version(VERSION)
.epilog(EPILOG).argv;

const stdin = await getStdin();
Copy link
Member Author

@stefanbuck stefanbuck Mar 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get-stdin is responsible to get the piped input and returns a string


argv.octoherdRepos = [...argv.octoherdRepos || [], ...parseInput(stdin)];

try {
await octoherd(argv);
} catch (error) {
Expand Down
16 changes: 16 additions & 0 deletions bin/parse-input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const commentsDelimiter = ['//', '#'];

export function parseInput(content = '') {
return content
.split('\n')
.map(line => line.trim())
.filter(Boolean) // Ignore empty lines
.filter(line => {
// Ignore lines starting with a comment delimiter
if (commentsDelimiter.some(block => line.startsWith(block))) {
return false;
}

return true;
});
}
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export async function octoherd(options) {
octokit,
script: octoherdScript,
userOptions,
octoherdReposPassedAsFlag: !!octoherdRepos,
octoherdReposPassedAsFlag: octoherdRepos.length,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

};

await runScriptAgainstRepositories(state, octoherdRepos);
Expand Down
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"chalk": "^5.0.0",
"clipboardy": "^3.0.0",
"enquirer": "^2.3.6",
"get-stdin": "^9.0.0",
"jsonfile": "^6.0.1",
"mkdirp": "^1.0.4",
"tempy": "^2.0.0",
Expand Down
31 changes: 31 additions & 0 deletions tests/parse-input.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { parseInput } from "../bin/parse-input.js";
import { suite } from "uvu";
import { equal } from "uvu/assert";

const parseTest = suite("parse-input");

parseTest("when input is empty", () => {
equal([], parseInput());
});

parseTest("when input is a single line", () => {
equal(['foo'], parseInput('foo'));
});

parseTest("when input has multipe lines", () => {
equal(['foo', 'bar'], parseInput('foo\nbar'));
});

parseTest("when input contains multipe empty lines", () => {
equal(['foo', 'bar', 'baz'], parseInput('foo\n\n\nbar\n\nbaz\n\n'));
});

parseTest("when line contains leading or trailing whitespaces", () => {
equal(['foo', 'bar'], parseInput('foo \n bar'));
});

parseTest("when lines start with a comment delimiter", () => {
equal(['foo'], parseInput('foo\n//bar\n#baz'));
});

parseTest.run();