Skip to content

Commit 04cddb8

Browse files
committed
feat ✨ (helpers): implement snapshot helper (#51)
Signed-off-by: Luis Mayta <[email protected]>
1 parent 6688f23 commit 04cddb8

File tree

10 files changed

+91
-5
lines changed

10 files changed

+91
-5
lines changed

.ci/linters/.codespell-ignores

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ cas
22
wit
33
NotIn
44
notin
5+
fo

.pre-commit-config.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ repos:
9292
exclude: >
9393
(?x)^(
9494
zsh-core.plugin.zsh$|
95+
pkg/helper/core.zsh$|
9596
.+\.provision/git/hooks/prepare-commit-msg$|
9697
.+\.tpl.sh$|
9798
)$
@@ -104,4 +105,4 @@ repos:
104105
- --verbose
105106
- id: hadolint
106107
args:
107-
- --config=.ci/linters/.hadolint.yaml
108+
- --config=.ci/linters/.hadolint.yaml

config/base.zsh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export CORE_MESSAGE_YAY="Please install Yay or use antibody bundle hadenlabs/zsh
66
export CORE_MESSAGE_RVM="Please install rvm or use antibody bundle hadenlabs/zsh-rvm"
77
export CORE_MESSAGE_RUST="Please install rust or use antibody bundle hadenlabs/zsh-rust"
88
export CORE_MESSAGE_NVM="Please install nvm or use antibody bundle hadenlabs/zsh-nvm"
9+
export CORE_PROJECTS_BACKUP_PATH="${HOME}/backup"
910

1011
export ANDROID_HOME="${HOME}/Library/Android/sdk"
1112
export ANDROID_PLATFORM_VERSION="35"

internal/backup.zsh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env ksh
2+
# -*- coding: utf-8 -*-
3+
4+
function core::internal::backup::snapshot {
5+
if [[ -z "$CORE_PROJECTS_BACKUP_PATH" ]]; then
6+
core::internal::message::error "❌ CORE_PROJECTS_BACKUP_PATH is not set"
7+
return 1
8+
fi
9+
10+
local module_path
11+
module_path="$(core::internal::git::get_module_path)"
12+
13+
local branch="no-git"
14+
if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
15+
branch="$(git rev-parse --abbrev-ref HEAD | tr '/' '-')"
16+
fi
17+
18+
local dest="${CORE_PROJECTS_BACKUP_PATH}/${module_path}/${branch}"
19+
20+
core::internal::message::info "📦 Syncing backup → ${dest}"
21+
22+
mkdir -p "${dest}"
23+
24+
rsync -avhP --delete \
25+
--exclude=".git" \
26+
--exclude=".task" \
27+
--exclude=".terraform" \
28+
--exclude=".venv" \
29+
--exclude="__pycache__" \
30+
--exclude=".mypy_cache" \
31+
--exclude=".pytest_cache" \
32+
--exclude="node_modules" \
33+
--exclude=".DS_Store" \
34+
./ "${dest}"
35+
36+
core::internal::message::success "✅ Backup saved to ${dest}"
37+
}

internal/git.zsh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env ksh
2+
# -*- coding: utf-8 -*-
3+
#
4+
function core::internal::git::get_module_path {
5+
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
6+
echo "unknown/unknown/$(basename "$(pwd)")"
7+
return
8+
fi
9+
10+
local remote_url
11+
remote_url="$(git config --get remote.origin.url)"
12+
13+
if [[ "$remote_url" =~ ^git@([^:]+):(.+)\.git$ ]]; then
14+
# [email protected]:hadenlabs/zsh-core.git → github.com/hadenlabs/zsh-core
15+
echo "${BASH_REMATCH[1]}/${BASH_REMATCH[2]}"
16+
elif [[ "$remote_url" =~ ^https?://([^/]+)/(.+)\.git$ ]]; then
17+
# https://github.com/hadenlabs/zsh-core.git → github.com/hadenlabs/zsh-core
18+
echo "${BASH_REMATCH[1]}/${BASH_REMATCH[2]}"
19+
else
20+
echo "unknown/unknown/$(basename "$(pwd)")"
21+
fi
22+
}

internal/main.zsh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ function core::internal::main::factory {
1616
esac
1717
# shellcheck source=/dev/null
1818
source "${ZSH_CORE_PATH}"/internal/helper.zsh
19+
20+
# shellcheck source=/dev/null
21+
source "${ZSH_CORE_PATH}"/internal/git.zsh
22+
23+
# shellcheck source=/dev/null
24+
source "${ZSH_CORE_PATH}"/internal/backup.zsh
1925
}
2026

21-
core::internal::main::factory
27+
core::internal::main::factory

pkg/helper/backup.zsh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env ksh
2+
# -*- coding: utf-8 -*-
3+
4+
function core::snapshot {
5+
core::internal::backup::snapshot
6+
}

pkg/helper.zsh renamed to pkg/helper/core.zsh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,4 @@ fi
182182
function net {
183183
# check connection
184184
ping 8.8.8.8 | grep -E --only-match --color=never '[0-9\.]+ ms'
185-
}
185+
}

pkg/helper/main.zsh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env ksh
2+
# -*- coding: utf-8 -*-
3+
4+
function core::pkg::helper::factory {
5+
# shellcheck source=/dev/null
6+
source "${ZSH_CORE_PATH}"/pkg/helper/backup.zsh
7+
8+
# shellcheck source=/dev/null
9+
source "${ZSH_CORE_PATH}"/pkg/helper/core.zsh
10+
}
11+
12+
core::pkg::helper::factory

pkg/main.zsh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function core::pkg::main::factory {
1515
;;
1616
esac
1717
# shellcheck source=/dev/null
18-
source "${ZSH_CORE_PATH}"/pkg/helper.zsh
18+
source "${ZSH_CORE_PATH}"/pkg/helper/main.zsh
1919

2020
# shellcheck source=/dev/null
2121
source "${ZSH_CORE_PATH}"/pkg/docker.zsh
@@ -24,4 +24,4 @@ function core::pkg::main::factory {
2424
source "${ZSH_CORE_PATH}"/pkg/alias.zsh
2525
}
2626

27-
core::pkg::main::factory
27+
core::pkg::main::factory

0 commit comments

Comments
 (0)