Skip to content
Snippets Groups Projects
Commit fc3cd2aa authored by dylan.peiry's avatar dylan.peiry
Browse files

feat(gitcreate) ajout d'options pour passer les paramètres (plus lisible) ainsi qu'une aide

parent c7beb023
No related branches found
No related tags found
No related merge requests found
......@@ -9,11 +9,13 @@ Une fois le token généré, vous devez le mettre au début du script au niveau
### Fonctionnement
Pour éxecuter le script, vous devez l'appeler en passant 3 paramètres :
- Votre login AAI (prenom.nom)
- Si vous souhaitez que le répertoire soit public ou non (true/false)
- La méthode d'accès (https/ssh)
- -u Votre nom d'utilisateur AAI (prénom.nom)
- -p Si vous souhaitez que le répertoire soit public ou non (true/false)
- -m La méthode d'accès (https/ssh)
Paramètre optionnel :
- -h Affiche les options du script.
Le nom du projet sera celui du répertoire courant depuis lequel vous lancez le script.
Le nom du projet sera celui du répertoire courant depuis lequel vous lancez le script. SI un projet avec ce nom existe déjà, un message d'information est affiché.
### Rendre le script accessible globalement
Pour pouvoir éxecuter le script depuis n'importe ou, vous devez le déplacer dans le dossier **bin**.
......@@ -22,7 +24,7 @@ cp gitcreate.sh ~/bin/gitcreate
```
Vous pouvez maintenant l'éxecuter de la manière suivante.
```
gitcreate {loginAAI} {public:true,false} {method:https,ssh}
gitcreate -h {usernameAAI} -p {public:true,false} -m {method:https,ssh}
# Exemple
gitcreate dylan.peiry true ssh
gitcreate -u dylan.peiry -p true -m ssh
```
\ No newline at end of file
#!/bin/bash
# Script to automatically create and init a repo on the specified gitlab instance
# Written by Dylan PEIRY (dylan.peiry@etu.hesge.ch
# Syntax : ./gitcreate.sh {username_aai} {public:true_or_false} {method:https_or_ssh}
# Syntax : ./gitcreate.sh {username_aai} {public:true,false} {method:https,ssh}
ACCESS_TOKEN=""
# methods of access
HTTPS="https://gitedu.hesge.ch"
SSH="ssh://git@ssh.hesge.ch:10572"
# Check the the access token is set
if [[ -z $ACCESS_TOKEN ]]; then
echo 'Please set your access token in the script.'
# messages
GITIGNORE_GENERATED="\e[32m.gitignore generated.\e[0m"
REPOSITORY_NAME_TAKEN="\e[33mA repository with this name already exists.\e[0m"
ARGUMENTS_MISSING="\e[31mArguments are missing.\e[0m"
REPOSITORY_CREATED="\e[32mThe repository is now created with the files of the current folder pushed to it.\e[0m"
# Display the usage of the script
function d_usage {
echo "Syntax: $0 [-h|u|p|m]"
echo "Options:"
echo "h Display the syntax of the script."
echo "u Your AAI Username. (firstname.lastname)"
echo "p Do you want the repository to be public ? (true,false)"
echo "m The method to link the distant repo to your local repo. (https,ssh)"
exit
fi
}
# Check the the visibility arg is specified
if [[ $# -lt 3 ]]; then
echo 'The AAI username must be specified (firstname.lastname).'
echo 'The visibility level must be specified (true=public,false=private).'
echo 'The access method must be specified (https,ssh)'
# Display the help to set the access_token
function d_access_token {
echo "Please set your access_token in the script before using it."
echo "More information on the README available at https://gitedu.hesge.ch/dylan.peiry/scripts"
exit
}
while getopts ":hu:p:m:" flag
do
case "${flag}" in
h)
d_usage
exit;;
u) USERNAME=${OPTARG};;
p) PUBLIC=${OPTARG};;
m) METHOD=${OPTARG};;
\?)
echo -e "\e[31m-${OPTARG} is not a valid option.\e[0m"
d_usage
exit;;
esac
done
if [[ $# -lt 6 ]]; then
echo -e $ARGUMENTS_MISSING
d_usage
fi
# Check that the access_token is set
if [[ -z $ACCESS_TOKEN ]]; then
access_token
fi
# The name of the repository will be the name of the current directory
NAME=$(basename $PWD)
USERNAME=$1
PUBLIC=$2
ACCESS_METHOD=$3
VISIBILITY=""
if [[ $PUBLIC == "true" ]]; then
VISIBILITY="public"
else
......@@ -36,15 +68,12 @@ else
fi
# Define the remote origin (url on which we will link the local repository)
REMOTE_ORIGIN=""
if [[ $ACCESS_METHOD == "ssh" ]]; then
if [[ $METHOD == "ssh" ]]; then
REMOTE_ORIGIN="$SSH/$USERNAME/$NAME.git"
fi
if [[ $ACCESS_METHOD == "https" ]]; then
if [[ $METHOD == "https" ]]; then
REMOTE_ORIGIN="$HTTPS/$USERNAME/$NAME.git"
fi
echo $REMOTE_ORIGIN
# Call the api to create the repository with the specified name
RESPONSE=$(curl -X POST 'https://gitedu.hesge.ch/api/v4/projects?private_token='$ACCESS_TOKEN'' \
-d '{"name":"'$NAME'","path":"'$NAME'","visibility":"'$VISIBILITY'"}' \
......@@ -53,16 +82,17 @@ RESPONSE=$(curl -X POST 'https://gitedu.hesge.ch/api/v4/projects?private_token='
# Look through the response to see if the name was taken by another repository
if [[ $RESPONSE == *"taken"* ]]; then
echo 'A repository with the name ['$NAME'] already exists.'
echo -e $REPOSITORY_NAME_TAKEN
exit
fi
# Create a .gitignore specific to a c program
touch .gitignore
echo "*" >> .gitignore
echo "!*.*" >> .gitignore
echo "!Makefile" >> .gitignore
echo "*.o" >> .gitignore
echo "*" > .gitignore
echo "!*.*" > .gitignore
echo "!Makefile" > .gitignore
echo "*.o" > .gitignore
# Initialize a local repository and push the files
git init
......@@ -71,4 +101,5 @@ git add .
git commit -m "initial commit"
git push -u origin master
echo 'The repository was successfully created and the files were successfully pushed to it.'
\ No newline at end of file
echo -e $GITIGNORE_GENERATED
echo -e $REPOSITORY_CREATED
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment