-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall
executable file
·149 lines (122 loc) · 4.24 KB
/
install
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
146
147
148
149
#!/bin/zsh
# Exit immediately if a command exits with a non-zero status,
# if using an undefined variable, or if any command in a pipeline fails
set -euo pipefail
# Determine the directory where the script is located
SCRIPT_DIR="$(cd "$(dirname "${(%):-%x}")" && pwd)"
BASE_DIR="$SCRIPT_DIR"
# Common paths and URLs
HOME_DIR="$HOME"
CONFIG_DIR="$HOME_DIR/.config"
VAULT_URL="https://vault.8567153.xyz/v1/shell_envs/data"
VAULT_KEYS=("envs.zsh") # Add more keys as needed
# Function to check if a command exists
has() { command -v "${1:-}" >/dev/null 2>&1; }
# Function to print banners
print_banner() {
printf "\n#\n%s\n#\n" "$1"
}
# Function to install symlinks using stow
install_with_stow() {
local stow_opts=("$@")
echo "Running stow with: ${stow_opts[*]}"
# Perform a dry run first
if ! stow -v "${stow_opts[@]}" -n; then
echo "Error: Stow dry run failed with options: ${stow_opts[*]}" >&2
exit 1
fi
# Prompt the user to apply changes
read -r "response?Do you want to apply the changes? [y/N] "
case "$response" in
[Yy]*)
echo "Applying changes..."
stow -v "${stow_opts[@]}"
;;
*)
echo "Changes were not applied."
;;
esac
}
# Function to add secret environment variables from Vault
add_secret_envs_from_vault() {
read -r "response?Would you like to add secret envs from Vault? [y/N] "
if [[ "$response" =~ ^[Yy]$ ]]; then
read -rs "VAULT_TOKEN?Please provide your Vault token: "
echo
# Ensure the target directory exists
mkdir -p "$HOME_DIR/.zshrc.d"
for key in "${VAULT_KEYS[@]}"; do
echo "Fetching data for key: $key"
# Use a subshell with a localized IFS to safely handle JSON parsing
(
IFS=$'\n\t'
local response
response=$(curl -sf -H "X-Vault-Token: $VAULT_TOKEN" -X GET "$VAULT_URL/$key") || {
echo "Failed to fetch data for $key from Vault." >&2
exit 1
}
echo "# Generated with install script from dotfiles" > "$HOME_DIR/.zshrc.d/$key"
# Process and save environment variables
echo "$response" | jq -r '.data.data | to_entries[] | "export \(.key)=\(.value)"' >> "$HOME_DIR/.zshrc.d/$key"
)
echo "Environment variables for $key have been saved to $HOME_DIR/.zshrc.d/$key"
done
else
echo "Skipped adding secret environment variables from Vault."
fi
}
# Function to configure Git conflict style
configure_git_conflict_style() {
local required_git_version="2.35.0"
local current_version
current_version=$(git --version | awk '{print $3}')
autoload -Uz is-at-least
if ! is-at-least "$required_git_version" "$current_version"; then
echo "Your Git version ($current_version) is older than $required_git_version."
git config --file "$HOME_DIR/.gitconfig.local" merge.conflictstyle "diff3"
echo "Set 'merge.conflictstyle' to 'diff3' in $HOME_DIR/.gitconfig.local."
fi
}
# Function to setup Atuin
setup_atuin() {
if has "atuin"; then
print_banner "Setting up Atuin configuration..."
mkdir -p "$CONFIG_DIR/atuin/"
ln -sfn "$BASE_DIR/.config/atuin/config.toml" "$CONFIG_DIR/atuin/config.toml"
fi
}
# Function to setup SSH configuration
setup_ssh() {
print_banner "Ensuring SSH configuration directory exists..."
mkdir -p "$HOME_DIR/.ssh/config.d/"
}
# Function to setup Midnight Commander
setup_mc() {
if has "mc"; then
print_banner "Setting up Midnight Commander..."
mkdir -p "$HOME_DIR/.mc/"
curl -fsSL "https://raw.githubusercontent.com/peel/mc/master/solarized.ini" -o "$HOME_DIR/.mc/solarized.ini"
fi
}
# Function to setup Vim
setup_vim() {
if has "vim"; then
print_banner "Setting up Vim..."
mkdir -p "$HOME_DIR/.vim/"{autoload,undo,swapfiles,backupfiles}
curl -fsSLo "$HOME_DIR/.vim/autoload/plug.vim" --create-dirs "https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim"
vim +PlugInstall +qall
fi
}
# Install symlinks using stow
install_with_stow -t "$HOME_DIR" --ignore ".config/atuin/config.toml" "."
install_with_stow -t "$HOME_DIR" -d "prv" "."
# Add secret environment variables from Vault
add_secret_envs_from_vault
# Configure Git conflict style
configure_git_conflict_style
# Setup various tools
setup_atuin
setup_ssh
setup_mc
setup_vim
print_banner "Installation completed successfully."