nginxbeautifier icon indicating copy to clipboard operation
nginxbeautifier copied to clipboard

format with stdin

Open shuxiao9058 opened this issue 6 years ago • 8 comments

such as gofmt, LuaFormatter etc.

shuxiao9058 avatar Dec 29 '19 15:12 shuxiao9058

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

vasilevich avatar Dec 29 '19 15:12 vasilevich

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

shuxiao9058 avatar Dec 30 '19 02:12 shuxiao9058

@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)

pappasam avatar Feb 24 '21 17:02 pappasam

@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.

vasilevich avatar Feb 24 '21 21:02 vasilevich

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 avatar Feb 24 '21 22:02 pappasam

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

pappasam avatar Feb 24 '21 22:02 pappasam

@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

vasilevich avatar Feb 24 '21 23:02 vasilevich

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."

pappasam avatar Feb 24 '21 23:02 pappasam