[Feature Request] better CLI upload
I found that rustypaste provide a CLI tool, do we support that?
This is talked about in the Document at chapter "Use MicroBin from the console with cURL":
curl -d "expiration=10min&content=$( < mypastafile.txt )" -X POST https://microbin.myserver.com/upload
But this seems buggy in some case:
- if there is special characters which needs URIencode.
- when the file is not small, I got
zsh: argument list too long: curl
I hope to define a microbin-upload command so that we can use it like:
microbin-upload test-code.py
cat test-code.py | microbin-upload
And hoping that:
- all special charactors are processed correctly.
- if there is an file postfix, then the highlight format is detectely automatically.
- the file size is not limited until it hits the hard limit of the microbin server config.
Is there any easy way to implement that?
Hi there, thanks for your feature request! Currently curl is the only way to upload contents from the terminal, and I am sure it is quite difficult to use, especially on v2. I think it is a good idea to create a little CLI tool for this and I don't imagine it being too difficult. I would like to have v2 ready to go and published, and then we can start working on this tool (in a new repository)! 🙂
My alias for microbin v1 has been this
python -c 'import sys; sys.stdout.write(sys.stdin.read().strip())' | curl -is -X POST https://microbin.example.com/upload -F content=@- -F expiration=10d | grep ^location: | cut -d -f2-
breakdown:
- use perl to remove trailing newlines, needed for microbin to recognize urls
- upload stdin using curl, printing response headers
- filter for the
locationheader - isolate the destination url
This command could be simplified greatly with the two following additions:
- be more lenient on whitespace when determining if the paste is a url and should support redirection
- return the resulting url in the body in addition to the
locationheader
I am against making a cli utility, and would rather prefer a sanctioned way to use curl.
For reference, here are some great examples which make using curl/nc alone very simple:
- http://ix.io/
- http://termbin.com/
EDIT: the perl chomp stripped newlines. Now i use python -c 'import sys; sys.stdout.write(sys.stdin.read().strip())'
I am against making a cli utility, and would rather prefer a sanctioned way to use curl.
That's right, It will be great if we can define microbin-upload as simple as an alias of curl, but that need us to provide an extra server API which doesn't exists right now (or extend current API to support more use case), I think that is also a good solution of current feature request :)
Also interesting is that one can expand a shell alias with Ctrl+alt+e, which allows you to modify default parameters in the alias
PS: I think "auto detect file type (for syntax highlighting) via filename extension" will be a sweet feature also.
I can no longer find documentation regarding usage from a shell, so I had to refer to this issue and dev tools to figure out what params microbin takes after installing it.
I've added the following zsh functions:
mbclean() {
perl -lne 's!/upload/!/p/!; print $1 if /^location: (.*)$/i'
}
mbf() {
if [[ -n $1 && -f $1 ]]; then
curl -is -X POST https://$HOST/upload -F "file=@$1" -F "uploader_password=$MICROBIN_PASS" | mbclean
else
echo "Usage: mbf <filename>"
fi
}
mbt() {
FILE="${1:-/dev/stdin}"
curl -is -X POST https://$HOST/upload -F "content=@$FILE" -F "uploader_password=$MICROBIN_PASS" | mbclean
}
Where mbt allows both piping and specifying a text file via the first argument, while mbf allows uploading arbitrary files.
While I think having microbin be easy to use with curl is great, some examples in the docs would be helpful for people. Not sure if there is API documentation anywhere.
My use case might be a bit different, but IMO it still counts for something: upload from cli and return the QR code directly, maybe determined via curl header like --header 'Accept: image/svg'.
To be honest I have no idea if it's possible without some RESTful API. For now it's possible to upload via cli, get the URL, and change upload to qr in it, then curl & parse the results into a svg that can be saved. But I have to manually open it, which makes no difference to just return the qr URL and open it in browser.