Creating a simple command line to post snippets on Gitlab

I’m trying to get away from Github, and one thing that I find useful is the gist utility they’re providing. Seems that gitlab provides a similar tool.

You can use it using python-gitlab:

pipx install python-gitlab

And then :

gitlab snippet create --title="youpi" --file-name="" --content --visibility="public"

I now wanted a small bash script which will just get the name of the file and infer all the parameters. I asked GPT-4, and iterated on its answer.

Here’s the resulting bash script:


if [ -z "$1" ]
    echo "Please provide a filename"
    exit 1

base=$(basename "$file")

# Use `cat` to fetch the content of the file
content=$(cat "$file")

result=$(gitlab snippet create --title="$title" --file-name="$title" --content="$content" --visibility="$visibility")

id=$(echo "$result" | awk '/id: / { print $2 }')
echo "$id"

I can now do snip and that will create the snippet for me :-)