-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgh-env-to-repo
executable file
·115 lines (99 loc) · 3.27 KB
/
gh-env-to-repo
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
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env bash
##
# Author: Enderson Menezes
# Created: 2024-02-19
# Description: This is a GitHub CLI extension that send a .env file from the local to repositorie secrets on GitHub.
# Usage: gh env-to-repo [flags]
##
set -e
## Necessary Modules
# - git
if ! type -p git >/dev/null; then
echo "Git not found on the system" >&2
exit 1
fi
# - gh
if ! type -p gh >/dev/null; then
echo "GitHub CLI not found on the system" >&2
exit 1
fi
## Necessary Functions
# - Verify if the owner and repository exists and the user has permission to access
function verify_owner_and_repository() {
gh api repos/"${OWNER}"/"${REPOSITORY}" &>/dev/null || {
echo "The owner ${OWNER} and the repository ${REPOSITORY} does not exist or you do not have permission to access."
exit 1
}
echo "The owner ${OWNER} and the repository ${REPOSITORY} exists and you have permission to access."
}
# - Verify if the .env file exists
function verify_dot_env() {
if [ ! -f .env ]; then
echo "The .env file does not exist in the current directory."
exit 1
fi
if [[ $(tail -c 1 .env | wc -l) -eq 0 ]]; then
echo "The .env file does not have a blank line at the end."
# Add a blank line at the end of the file
echo "" >>.env
echo "A blank line was added at the end of the .env file."
fi
}
# - Parse the .env file
function parse_dot_env() {
echo "Parsing the .env file..."
VALUE_NAMES=()
VALUE_KEYS=()
while IFS= read -r line; do
# Remove leading/trailing whitespace
line=$(echo "$line" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
if [[ -z $line || $line == \#* ]]; then
# Skip empty lines and lines starting with '#'
continue
fi
# Eg. KEY=VALUE or KEY="VALUE"
if [[ $line == *=* ]]; then
KEY=$(echo "$line" | cut -d'=' -f1)
VALUE=$(echo "$line" | cut -d'=' -f2)
if [[ $VALUE == *\"* ]]; then
VALUE=${VALUE//\"/}
fi
VALUE_NAMES+=("$KEY")
VALUE_KEYS+=("$VALUE")
fi
done <.env
echo "The .env file was parsed successfully."
}
function create_secrets() {
echo "Creating the secrets..."
for i in "${!VALUE_NAMES[@]}"; do
gh secret set "${VALUE_NAMES[$i]}" -b "${VALUE_KEYS[$i]}" -R "${OWNER}"/"${REPOSITORY}"
done
echo "The secrets were created successfully."
}
## Necessary Args
# Check $1 - Owner
if [ -z "$1" ]; then
echo "Owner is required. Usage: gh env-to-repo [owner] [repository]"
read -rp "Owner: " OWNER
fi
# Creck $2 - Repository
if [ -z "$2" ]; then
echo "Repository is required. Usage: gh env-to-repo [owner] [repository]"
read -rp "Repository: " REPOSITORY
fi
# -- Main
echo "Welcome to the GitHub CLI extension env-to-repo!"
USERNAME=$(git config user.name)
EMAIL=$(git config user.email)
OWNER=${1:-$OWNER}
REPOSITORY=${2:-$REPOSITORY}
echo "Hello, ${USERNAME} <${EMAIL}>! I will help you to send your .env file to the repository secrets ${OWNER}/${REPOSITORY}."
# Check if the owner and repository exists and the user has permission to access
verify_owner_and_repository
# Check if .env file exists
verify_dot_env
# Parse the .env file
parse_dot_env
# Create the secrets using the GitHub CLI
create_secrets