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

format with stdin #14

Open
shuxiao9058 opened this issue Dec 29, 2019 · 8 comments
Open

format with stdin #14

shuxiao9058 opened this issue Dec 29, 2019 · 8 comments

Comments

@shuxiao9058
Copy link

such as gofmt, LuaFormatter etc.

@vasilevich
Copy link
Owner

Hi, Thanks for posting an issue, sorry I do not understand what you mean, can you please explain?
I have no experience with the project you linked and I don't know what is "gofmt".
thanks

@shuxiao9058
Copy link
Author

the gofmt get the text from standard input, then output the formatted text to the standard output.

@pappasam
Copy link

@vasilevich Love the progress on this tool!

Standard input and standard output support will make this tool compatible with the majority of text editor tooling on POSIX-like systems. I use a custom formatter plugin for Vim that relies on tools reading from standard input and writing to standard output: https://github.com/pappasam/vim-filetype-formatter. nginxbeautifier is not currently compatible with this, requiring annoying workarounds for interactive use.

POSIX-like: https://en.wikipedia.org/wiki/POSIX#Mostly_POSIX-compliant
Standard input: https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin)
Standard output: https://en.wikipedia.org/wiki/Standard_streams#Standard_output_(stdout)

@vasilevich
Copy link
Owner

@pappasam technically, stdin is already supported except the ability to recieve the actual nginx config,
how would I go about doing that? do you want me to add an additional input parameter that accepts maybe a base64 encoded nginx config? or what is the actual approach of doing that to avoid damaging the content?

and I believe you want the executable to respond with the resulting nginx config, maybe I can check if I recieved the script dynamically, the response will be dynamic as well when no file is provided to write to.

@pappasam
Copy link

pappasam commented Feb 24, 2021

One somewhat standard way to do it: if a file named - is presented at the command line, you should use something like this to read the contents of standard input. In the case where a filename is necessary(eg, if your standard input contains any include directives), you might want to also pass a filepath option so tools understand where the standard input is coming from.

To write to standard output, you can just do console.log, although maybe there's a more "official" way to do that (I'm more familiar with Python for this sort of problem).

My current workaround without any library-specific updates:

cat nginx.conf | dd status=none of=/tmp/nginx.conf >& /dev/null && nginxbeautifier --space 4 /tmp/nginx.conf >& /dev/null && cat /tmp/nginx.conf && rm /tmp/nginx.conf

@pappasam
Copy link

For standard output, you can provide - as the filename too. That said, usually the default behavior of tools is to print to standard output if standard input is passed without an output filename specified.

Check out the interface for my toml-sort cli tool; I tried to follow best practices for CLI design as much as possible: https://toml-sort.readthedocs.io/en/latest/cli.html

@vasilevich
Copy link
Owner

vasilevich commented Feb 24, 2021

@pappasam Hey, thank you for the time,
I am quite new in the creating cli tools sphere so I apologize for asking nooby questions,
according to the doc you sent, their example looks like so:

const readline = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
})

readline.question(`What's your name?`, name => {
  console.log(`Hi ${name}!`)
  readline.close()
})

I just don't understand what you mean by -.
1.
maybe something like this:

const readline = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
})

readline.question(`-`, nginxConfig => {
  console.log(nginxConfig)
  readline.close()
})

or, do you mean,
when the arg - is met,

eg

nginxbeautifier - %NGINX CONFING CONTENT SHOULD GO HERE%
so which way do you mean?
1 or 2?

or there is something I am missing?
it seems like a simple enogh thing to put together, I just want to understand how its done properly.

or do you mean this:
nginxbeautifier -input %NGINX CONFING CONTENT SHOULD GO HERE%

Thanks

@pappasam
Copy link

pappasam commented Feb 24, 2021

My bad, I gave you the wrong example. Ignore the readline docs I sent.

I just whipped together a command line program that re-implements a stripped down version of cat that only takes standard input and prints to standard output. I think that might be the simplest way to demonstrate how to read stdin in a relevant way for this CLI tool.

let fileContentStdIn = ''

process.stdin.on('data', (inputStdin) => {
  fileContentStdIn += inputStdin
})

process.stdin.on('end', () => {
  process.stdout.write(fileContentStdIn)
  console.log('*********************************')
  console.log("Text above *'s is written with `process.stdout.write`")
})

Put that code in a file called cat_stdin.js and run: echo "hello, world" | node cat_stdin.js. The following text should print to your console:

hello, world
*********************************
Text above *'s is written with `process.stdout.write`

Using something like the above, in your code, you'd basically say: "If the user passes me the filename - as input, then I'll assume that means they want me to read the nginx.conf file contents from standard input. If they pass me the filename - as output, then I'll assume that means they want me to write the formatted file contents to standard output."

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

No branches or pull requests

3 participants