Skip to content

Commit d160d8b

Browse files
Updated renaming script to rename only required files and directories (#40)
1 parent 016cff9 commit d160d8b

1 file changed

Lines changed: 70 additions & 37 deletions

File tree

set_app_name.sh

Lines changed: 70 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
set -e
44

5+
# Defining ANSI color codes for terminal output
6+
RED='\e[0;31m'
7+
LIGHT_RED='\e[0;91m'
8+
ORANGE='\e[38;5;208m'
9+
YELLOW='\e[0;33m'
10+
GREEN='\e[38;5;10m'
11+
BLUE='\e[38;5;81m'
12+
DEFAULT='\e[0m'
13+
514
# More reliable than `sed -i` if we're on WSL and handling Windows paths
615
wsl_safe_sed_replace() {
716
local pattern=$1
@@ -12,54 +21,74 @@ wsl_safe_sed_replace() {
1221
sed "$pattern" "$file" > "$tmpfile" && mv -f "$tmpfile" "$file"
1322
}
1423

15-
if [[ -z "$APP_NAME_SUFFIX" ]]; then
16-
echo "Error: APP_NAME_SUFFIX environment variable must be set."
24+
echo -e "\n${ORANGE}=================Executing renaming script=================${DEFAULT}"
25+
26+
if [[ -z "$APP_NAME" ]]; then
27+
echo -e "${RED}\n===========================================================${DEFAULT}"
28+
echo -e "${RED}Error${DEFAULT}: ${BLUE}APP_NAME${DEFAULT} environment variable must be set. Exiting..."
29+
echo -e "${RED}===========================================================\n${DEFAULT}"
1730
exit 1
1831
fi
1932

20-
PATTERN="^[a-z0-9]([-a-z0-9]*[a-z0-9])?$"
21-
22-
if [[ "$APP_NAME_SUFFIX" != "APP_NAME_SUFFIX" ]]; then
23-
if ! [[ "$APP_NAME_SUFFIX" =~ $PATTERN ]]; then
24-
echo "Error: New app name '$APP_NAME_SUFFIX' is not a valid Helm chart name."
25-
echo "It must:"
26-
echo " - Use only lowercase letters, numbers, and dashes (-)"
27-
echo " - Start and end with an alphanumeric character"
33+
if [[ "$APP_NAME" != "APP_NAME" ]]; then
34+
if ! [[ "$APP_NAME" =~ ^[a-z0-9]([-a-z0-9]*[a-z0-9])?$ ]]; then
35+
echo -e "${RED}\n===========================================================${DEFAULT}"
36+
echo -e "${RED}Error${DEFAULT}: New App name '$APP_NAME' is not a valid Helm chart name."
37+
echo -e "It must:"
38+
echo -e " - Use only lowercase letters, numbers, and dashes (-)"
39+
echo -e " - Start and end with an alphanumeric character"
40+
echo -e "Exiting..."
41+
echo -e "${RED}===========================================================\n${DEFAULT}"
2842
exit 1
2943
fi
3044
else
31-
echo "WARNING: APP_NAME_SUFFIX can be set only as a placeholder. APP_NAME_SUFFIX is not valid for development purposes"
45+
echo -e "\n${LIGHT_RED}WARNING${DEFAULT}: APP_NAME can be set only as a placeholder. APP_NAME is not a valid name for development purposes."
3246
fi
3347

34-
APP_NAME_PREFIX="eric-oss-hello-world-python-"
35-
NEW_APP_NAME="$APP_NAME_PREFIX$APP_NAME_SUFFIX"
36-
37-
if [[ ${#NEW_APP_NAME} -gt 53 ]]; then
38-
echo "$NEW_APP_NAME exceeds 53 characters. Helm chart name must be at most 53 characters."
48+
if [[ ${#APP_NAME} -gt 53 ]]; then
49+
echo -e "${RED}\n===========================================================${DEFAULT}"
50+
echo -e "The ${BLUE}APP_NAME${DEFAULT} exceeds 53 characters. Helm chart name must be at most 53 characters. ${RED}Exiting...${DEFAULT}"
51+
echo -e "${RED}===========================================================\n${DEFAULT}"
3952
exit 1
4053
fi
4154

42-
# Accept list of target paths as arguments in case we want to limit to specific paths
43-
INPUT_PATHS=("$@")
55+
# Setting target directory
56+
if [[ -z "$1" ]]; then
57+
TARGET_DIR="${TARGET_DIR:-$(pwd)}"
58+
else
59+
TARGET_DIR=$1
60+
fi
4461

62+
echo -e "\nUsing ${BLUE}TARGET_DIR${DEFAULT} as: $TARGET_DIR"
63+
64+
# List of target paths
65+
INPUT_PATHS=("charts/" "csar/" "eric-oss-hello-world-python-app/")
66+
67+
# Print directories
4568
if [[ ${#INPUT_PATHS[@]} -gt 0 ]]; then
46-
echo "Using provided paths as targets:"
69+
echo -e "\nExecuting the script in the following directories:"
4770
for path in "${INPUT_PATHS[@]}"; do
48-
if [[ ! -e "$path" ]]; then
49-
echo "Error: '$path' does not exist."
71+
if [[ ! -e "${TARGET_DIR}/${path}" ]]; then
72+
echo -e "${RED}\n===========================================================${DEFAULT}"
73+
echo -e "${RED}Error${DEFAULT}: directory '$path' does not exist. Exiting..."
74+
echo -e "${RED}===========================================================\n${DEFAULT}"
5075
exit 1
5176
fi
5277
echo " - $path"
5378
done
79+
80+
count=0
81+
for path in "${INPUT_PATHS[@]}"; do
82+
INPUT_PATHS[$count]="${TARGET_DIR}/${path}"
83+
count=$((count + 1))
84+
done
5485
else
55-
TARGET_DIR="${TARGET_DIR:-$(pwd)}"
56-
echo "Using TARGET_DIR as: $TARGET_DIR"
5786
INPUT_PATHS=("$TARGET_DIR")
5887
fi
5988

6089
chart_dirs=()
6190

62-
# Collect the app's chart(s)
91+
# Collect the App's chart(s)
6392
for path in "${INPUT_PATHS[@]}"; do
6493
if [[ -d "$path" && -d "$path/charts" ]]; then
6594
chart_dirs+=("$path/charts"/*)
@@ -69,54 +98,58 @@ for path in "${INPUT_PATHS[@]}"; do
6998
fi
7099
done
71100

72-
# Expect only one chart present and assume we can use its name as our current APP_NAME
101+
# Expect only one chart present and assume we can use its name as our APP_NAME
73102
if [[ ! -d "${chart_dirs[0]}" || ${#chart_dirs[@]} -ne 1 ]]; then
74-
echo "Error: No valid subdirectories found in provided paths' charts/ directories."
103+
echo -e "${RED}\n===========================================================${DEFAULT}"
104+
echo -e "${RED}Error${DEFAULT}: No valid subdirectories found in provided paths' charts/ directories."
105+
echo -e "${RED}===========================================================\n${DEFAULT}"
75106
exit 1
76107
fi
77108

78109
CURRENT_APP_NAME=$(basename "${chart_dirs[0]}")
79-
echo "Detected CURRENT_APP_NAME as '$CURRENT_APP_NAME'"
110+
echo -e "\nDetected ${BLUE}CURRENT_APP_NAME${DEFAULT} as '$CURRENT_APP_NAME'"
80111

81-
if [[ "$CURRENT_APP_NAME" == "$NEW_APP_NAME" ]]; then
82-
echo "Provided NEW_APP_NAME is the same as CURRENT_APP_NAME. Exiting..."
112+
if [[ "$CURRENT_APP_NAME" == "$APP_NAME" ]]; then
113+
echo -e "\n${RED}Provided ${BLUE}APP_NAME${RED} is the same as ${BLUE}CURRENT_APP_NAME${RED}. Exiting...${DEFAULT}"
83114
exit 1
84115
fi
85116

86117
script_file=$(basename "$0")
87118

88-
echo "Replacing '$CURRENT_APP_NAME' with '$NEW_APP_NAME' in provided paths..."
119+
echo -e "\nReplacing '$CURRENT_APP_NAME' with '$APP_NAME'...\n"
89120

90-
echo "Updating file contents..."
121+
echo -e "${YELLOW}Updating file contents...${DEFAULT}"
91122
for path in "${INPUT_PATHS[@]}"; do
92123
grep -rl "$CURRENT_APP_NAME" "$path" | while read -r file; do
93124
if [[ $(basename "$file") != "$script_file" && $(basename "$file") != docker.tar* ]]; then
94-
wsl_safe_sed_replace "s/$CURRENT_APP_NAME/$NEW_APP_NAME/g" "$file"
125+
wsl_safe_sed_replace "s/$CURRENT_APP_NAME/$APP_NAME/g" "$file"
95126
fi
96127
done
97128
done
98129

99-
echo "Renaming files..."
130+
echo -e "${YELLOW}Renaming files...${DEFAULT}"
100131
for path in "${INPUT_PATHS[@]}"; do
101132
find "$path" -depth -type f -name "*$CURRENT_APP_NAME*" | while read -r file; do
102133
if [[ $(basename "$file") != "$script_file" ]]; then
103-
newfile="$(dirname "$file")/$(basename "$file" | sed "s/$CURRENT_APP_NAME/$NEW_APP_NAME/g")"
134+
newfile="$(dirname "$file")/$(basename "$file" | sed "s/$CURRENT_APP_NAME/$APP_NAME/g")"
104135
mv -f "$file" "$newfile"
105136
fi
106137
done
107138
done
108139

109-
echo "Renaming directories..."
140+
echo -e "${YELLOW}Renaming directories...${DEFAULT}"
110141
for path in "${INPUT_PATHS[@]}"; do
111142
find "$path" -depth -type d -name "*$CURRENT_APP_NAME*" | while read -r dir; do
112143
# Skip renaming the outermost directory itself
113144
if [[ "$(realpath "$dir")" == "$(realpath "$path")" ]]; then
114145
continue
115146
fi
116147

117-
newdir="$(dirname "$dir")/$(basename "$dir" | sed "s/$CURRENT_APP_NAME/$NEW_APP_NAME/g")"
148+
newdir="$(dirname "$dir")/$(basename "$dir" | sed "s/$CURRENT_APP_NAME/$APP_NAME/g")"
118149
mv -f "$dir" "$newdir"
119150
done
120151
done
121152

122-
echo "Finished renaming."
153+
echo -e "${GREEN}\n===========================================================${DEFAULT}"
154+
echo -e "${GREEN}Finished renaming.${DEFAULT}"
155+
echo -e "${GREEN}===========================================================\n${DEFAULT}"

0 commit comments

Comments
 (0)