- Alexis Métaireau
- Journal
- Code, etc.
- Notes hebdo
- Lectures
- Projets

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="snip.py" --content snip.py --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:

    #!/bin/bash

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

    file="$1"
    base=$(basename "$file")
    title="$base"
    visibility="public"

    # 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 "https://gitlab.com/-/snippets/$id"

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