-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost-create-command.sh
145 lines (139 loc) · 3.76 KB
/
post-create-command.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env bash
WORKDIR="/workspace"
function install_basics() {
export DEBIAN_FRONTEND=noninteractive
sudo apt update && sudo apt -y dist-upgrade
sudo apt install -y --no-install-recommends \
iputils-ping \
python3-pip \
python3-argcomplete
sudo rm -rf /var/lib/apt/lists/*
sudo apt -y clean
sudo apt -y autoclean
sudo apt -y autoremove
if [[ ! -f /usr/bin/python ]]; then
sudo ln -s /usr/bin/python3 /usr/bin/python
fi
}
function setup_python_argcomplete() {
if type -p activate-global-python-argcomplete &>/dev/null; then
sudo activate-global-python-argcomplete --dest /etc/bash_completion.d/
elif type -p activate-global-python-argcomplete3 &>/dev/null; then
sudo activate-global-python-argcomplete3 --dest /etc/bash_completion.d/
fi
}
function generate_config_files() {
if find "${HOME}" -type f -name '.functions' | grep -q .; then
return 0
fi
MY_SHELL=$(awk -F: '/^'$(whoami)'/ {print $NF}' /etc/passwd)
MY_SHELL=${MY_SHELL##*/}
cat <<_EOF >>"${HOME}/.${MY_SHELL}rc"
########################################
###### ADDED BY 'post-create-command.sh'
########################################
files=(.functions .paths)
for file in "\${files[@]}"; do
if [[ ! -f "\${HOME}/\${file}" ]]; then
touch "\${HOME}/\${file}"
else
source "\${HOME}/\${file}"
fi
done
if [[ -f /etc/bash_completion ]]; then
MY_SHELL=\$(awk -F: '/^'\$(whoami)'/ {print \$NF}' /etc/passwd)
MY_SHELL=\${MY_SHELL##*/}
if [[ \${MY_SHELL} == "bash" ]]; then
. /etc/bash_completion
fi
fi
function source_saltstack_aliases() {
if ! grep -q 'logs-master'< <(alias); then
. /workspace/.devcontainer/scripts/.aliases
return 0
fi
}
source_saltstack_aliases
########################################
_EOF
files=(.functions .paths)
for file in "${files[@]}"; do
touch "${HOME}/${file}"
done
EXPAND_PATHS=("${HOME}/.local/bin")
for EXPAND_PATH in "${EXPAND_PATHS[@]}"; do
if ! grep -q "{EXPAND_PATH}" "${HOME}/.paths"; then
echo "PATH=\${PATH}:${EXPAND_PATH}" >>"${HOME}/.paths"
fi
done
}
### PRE COMMIT
function command_exists() {
# Function to check if a command exists
command -v "$1" >/dev/null 2>&1
}
function install_pre_commit_linux() {
# Function to install pre-commit on Linux
echo "Installing pre-commit on Linux..."
$PIP_CMD install pre-commit
}
function install_pre_commit_macos() {
# Function to install pre-commit on macOS
echo "Installing pre-commit on macOS..."
$PIP_CMD install pre-commit
}
function check_pip_command() {
# Check if pip is installed
if command_exists pip; then
PIP_CMD="pip"
elif command_exists pip3; then
PIP_CMD="pip3"
else
echo "pip is not installed. Please install pip (https://pip.pypa.io/en/stable/installing/) and run the script again."
exit 1
fi
}
function install_salt-lint() {
# Function to install salt-lint
echo "Installing salt-lint..."
$PIP_CMD install salt-lint
}
function install_pre_commit() {
# Check if pre-commit is already installed
if command_exists pre-commit; then
echo "pre-commit is already installed."
return 0
else
case "$(uname)" in
Linux)
install_pre_commit_linux
;;
Darwin)
# Check if Homebrew is installed
if command_exists brew; then
install_pre_commit_macos
else
echo "Homebrew is not installed. Please install Homebrew (https://brew.sh/) and run the script again."
exit 1
fi
;;
*)
echo "Unsupported OS. This script only supports Linux and macOS."
exit 1
;;
esac
fi
}
function activate_pre_commit() {
pre-commit install
}
function main() {
install_basics
setup_python_argcomplete
generate_config_files
check_pip_command
install_salt-lint
# install_pre_commit
# activate_pre_commit
}
main