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

a better CLI way to use microbin #243

Open
uxfion opened this issue Dec 1, 2023 · 2 comments
Open

a better CLI way to use microbin #243

uxfion opened this issue Dec 1, 2023 · 2 comments

Comments

@uxfion
Copy link

uxfion commented Dec 1, 2023

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 <YOURL_MICROBIN_URL> 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. #170 (comment)

@scargill
Copy link

scargill commented May 5, 2024

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
Copy link

fragolinux commented May 5, 2024

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

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