-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
104 lines (94 loc) · 3.27 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
# Function to print usage instructions
usage() {
echo "Usage: $0 [-p PROVIDER] [-t TOKEN | -u USERNAME -w PASSWORD] URL"
echo " -p PROVIDER Specify the provider (e.g., github, gitlab, bitbucket)"
echo " -t TOKEN Use a personal access token for authentication"
echo " -u USERNAME Use a username for authentication"
echo " -w PASSWORD Use a password for authentication"
echo " URL The URL of the Git repository to clone"
exit 1
}
# Parse command-line arguments
while getopts ":p:t:u:w:" opt; do
case $opt in
p) PROVIDER="$OPTARG" ;;
t) TOKEN="$OPTARG" ;;
u) USERNAME="$OPTARG" ;;
w) PASSWORD="$OPTARG" ;;
\?) usage ;; # Invalid option
esac
done
shift $((OPTIND - 1))
# Check if URL is provided
if [ -z "$1" ]; then
usage
fi
URL="$1"
# Determine provider if not specified
if [ -z "$PROVIDER" ]; then
if [[ $URL == *"github.com"* ]]; then
PROVIDER="github"
elif [[ $URL == *"gitlab.com"* ]]; then
PROVIDER="gitlab"
elif [[ $URL == *"bitbucket.org"* ]]; then
PROVIDER="bitbucket"
else
echo "Error: Could not determine provider. Please specify using -p."
exit 1
fi
fi
# Set up authentication based on provider and credentials
case $PROVIDER in
github)
if [ -n "$TOKEN" ]; then
git config --global credential.helper 'cache --timeout=3600'
git config --global user.email "[email protected]"
git config --global user.name "Name"
echo "https://${TOKEN}:@github.com" > ~/.git-credentials
else
echo "Error: GitHub requires a personal access token. Use -t."
exit 1
fi
;;
gitlab)
if [ -n "$TOKEN" ]; then
git config --global credential.helper 'cache --timeout=3600'
git config --global user.email "[email protected]"
git config --global user.name "Name"
echo "https://oauth2:${TOKEN}@gitlab.com" > ~/.git-credentials
elif [ -n "$USERNAME" ] && [ -n "$PASSWORD" ]; then
git config --global credential.helper 'cache --timeout=3600'
git config --global user.email "[email protected]"
git config --global user.name "Name"
echo "https://$USERNAME:[email protected]" > ~/.git-credentials
else
echo "Error: GitLab requires either a personal access token or username/password."
exit 1
fi
;;
bitbucket)
if [ -n "$USERNAME" ] && [ -n "$PASSWORD" ]; then
git config --global credential.helper 'cache --timeout=3600'
git config --global user.email "[email protected]"
git config --global user.name "Name"
echo "https://$USERNAME:[email protected]" > ~/.git-credentials
else
echo "Error: Bitbucket requires a username and password. Use -u and -w."
exit 1
fi
;;
*)
echo "Error: Unsupported provider: $PROVIDER"
exit 1
;;
esac
# Clone the repository using the original URL
echo "Cloning repository from $URL..."
git clone "$URL"
# Check for errors
if [ $? -ne 0 ]; then
echo "Error: Failed to clone repository."
exit 1
fi
echo "Repository cloned successfully!"