blog.notmyidea.org/content/code/2023-09-18.md
Alexis Métaireau 91159ecc80 New version of the website.
- More categories are displayed
- Change the URL Scheme for categories
- Add a view for weeknotes, readings and code.
2023-09-25 16:50:07 +02:00

47 lines
No EOL
1.2 KiB
Markdown

# 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](https://gist.github.com) utility they're providing. Seems that gitlab provides a similar tool.
You can use it using [python-gitlab](https://python-gitlab.readthedocs.io/):
```bash
pipx install python-gitlab
```
And then :
```bash
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:
```bash
#!/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 :-)