szabodanika / microbin

A secure, configurable file-sharing and URL shortening web app written in Rust.
https://microbin.eu
BSD 3-Clause "New" or "Revised" License
2.51k stars 151 forks source link

a better CLI way to use microbin #243

Open uxfion opened 7 months ago

uxfion commented 7 months ago

I wrote a bash function that makes it easy to use the CLI, It is tested on bash and zsh

config

vim ~/.bashrc, Attention, please replace with your own Microbin URL.

mb() {
  mbclean() {
    perl -lne 's!/upload/!/p/!; print $1 if /^location: (.*)$/i'
  }

  upload_file() {
    curl -is -X POST https://<YOURL_MICROBIN_URL>/upload -F "file=@$1" | mbclean
  }

  upload_text() {
    curl -is -X POST https://<YOURL_MICROBIN_URL>/upload -F "content=$1" | mbclean
  }

  # Check if an argument is provided
  if [[ -n $1 ]]; then
    # Check if the argument is a file
    if [[ -f $1 ]]; then
      # Prompt user for choice
      echo "File detected. Choose an option:"
      echo "1. Upload file (default)"
      echo "2. Upload text from file"
      echo "Enter choice (1 or 2): "
      read choice
      choice=${choice:-1}
      case $choice in
        1) upload_file "$1" ;;
        2) upload_text "@$1" ;;
        *) echo "Invalid choice"; return 1 ;;
      esac
    # Check if the argument is a directory
    elif [[ -d $1 ]]; then
      echo "Error: '$1' is a directory. This script can only handle files and text."
      return 1
    else
      # If it's not a file or directory, assume it's a string and upload the string
      upload_text "$1"
    fi
  elif [[ ! -t 0 ]]; then
    # If no argument is provided and stdin is not from the terminal, read from stdin
    upload_text "@/dev/stdin"
  else
    # If no input is provided, print usage instructions
    echo "Error: No input provided."
    echo "Usage:"
    echo "  mb <text_string>"
    echo "  mb <file_path>"
    echo "  mb < file_path"
    echo "  echo \"hello world\" | mb"
    echo "  command | mb (e.g., 'python test.py | mb')"
    echo "  command 2>&1 | mb (e.g., 'python test.py 2>&1 | mb')"
    echo "  command 2>&1 | tee /dev/tty | mb (e.g., 'python test.py 2>&1 | tee /dev/tty | mb')"
    return 1
  fi
}

Don't forget source ~/.bashrc

how to use?

mb "hello world"
mb test.py  # It will ask you whether to directly upload the file or upload the text inside the file
mb < test.py
echo "hello world" | mb
python test | mb

Thanks for the suggestions provided by @vaskozl. https://github.com/szabodanika/microbin/issues/170#issuecomment-1800875718

scargill commented 2 months ago

hi, is there a way to set the syntax highlight with this? as it's not possible to modify it after publishing on the service...

fragolinux commented 2 months ago

hi, is there a way to set the syntax highlight with this? as it's not possible to modify it after publishing on the service...

support this, or at least allow us to change the syntax highlight AFTER a snippet is created, by editing it, thanks!

editing the url or allowing to have a custom title would be nice, too