Skip to content

Commit e1cb51f

Browse files
author
andrew
committed
2 parents 8aa7ecc + 23484d5 commit e1cb51f

File tree

10 files changed

+345
-1
lines changed

10 files changed

+345
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ Put the following in your shell's startup file:
174174

175175
```Shell
176176
export PATH="$PATH:${BMA_HOME:-$HOME/.bash-my-aws}/bin"
177+
export BMA_COLUMNISE_ONLY_WHEN_TERMINAL_PRESENT=true
177178
source ${BMA_HOME:-$HOME/.bash-my-aws}/aliases
178179

179180
# For ZSH users, uncomment the following two lines:

TODO.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# TODO
22

3+
- Only `add_to_path_if_missing()`
34
- (aws-)backup-functions
45

aliases

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ alias buckets='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma buckets'
7777
alias cert-chain='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma cert-chain'
7878
alias cert-delete='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma cert-delete'
7979
alias cert-ificate='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma cert-ificate'
80+
alias cert-resource-record-valid='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma cert-resource-record-valid'
8081
alias cert-users='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma cert-users'
8182
alias cert-verify='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma cert-verify'
8283
alias certs='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma certs'
@@ -120,6 +121,7 @@ alias elbv2s='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma elbv2s'
120121
alias hosted-zone-a-records='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma hosted-zone-a-records'
121122
alias hosted-zone-ns-records='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma hosted-zone-ns-records'
122123
alias hosted-zones='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma hosted-zones'
124+
alias iam-access-key-rotate='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma iam-access-key-rotate'
123125
alias iam-role-principal='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma iam-role-principal'
124126
alias iam-roles='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma iam-roles'
125127
alias iam-users='${BMA_HOME:-$HOME/.bash-my-aws}/bin/bma iam-users'

bash_completion.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ complete -F _bma_buckets_completion buckets
179179
complete -F _bma_certs_completion cert-chain
180180
complete -F _bma_certs_completion cert-delete
181181
complete -F _bma_certs_completion cert-ificate
182+
complete -F _bma_certs_completion cert-resource-record-valid
182183
complete -F _bma_certs_completion cert-users
183184
complete -F _bma_certs_completion cert-verify
184185
complete -F _bma_certs_completion certs

docs/command-reference.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,16 @@ Return Cert Chain for ACM Cert(s)
13191319
USAGE: cert-chain cert-arn [cert-arn]
13201320

13211321

1322+
### cert-resource-record-valid
1323+
1324+
cert-resource-record-valid - Check if a specified DNS resource record matches the expected value and includes input validation.
1325+
1326+
Usage: cert-resource-record-valid <name> <type> <value>
1327+
Perform the DNS query
1328+
Print the output of the DNS query
1329+
Check if the expected value is in the output
1330+
1331+
13221332
### cert-verify
13231333

13241334
Verify ACM cert(s)
@@ -1678,6 +1688,10 @@ List IAM Users
16781688
ecsInstanceRole AROAJFQ3WMZXESGIKW5YD 2017-03-09T05:31:39Z
16791689

16801690

1691+
### iam-access-key-rotate
1692+
1693+
1694+
16811695
## image-commands
16821696

16831697

functions

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ buckets
7777
cert-chain
7878
cert-delete
7979
cert-ificate
80+
cert-resource-record-valid
8081
cert-users
8182
cert-verify
8283
certs
@@ -120,6 +121,7 @@ elbv2s
120121
hosted-zone-a-records
121122
hosted-zone-ns-records
122123
hosted-zones
124+
iam-access-key-rotate
123125
iam-role-principal
124126
iam-roles
125127
iam-users

lib/cert-functions

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,36 @@ cert-chain() {
130130
done
131131
}
132132

133+
cert-resource-record-valid() {
134+
135+
# cert-resource-record-valid - Check if a specified DNS resource record matches the expected value and includes input validation.
136+
#
137+
# Usage: cert-resource-record-valid <name> <type> <value>
138+
139+
[[ "$#" -lt 2 ]] && __bma_usage "<name> <value> [type]" && return 1
140+
141+
local name="$1"
142+
local value="$2"
143+
local type="${3:-CNAME}"
144+
145+
# Perform the DNS query
146+
local output=$(dig +noall +answer "$name" "$type")
147+
148+
# Print the output of the DNS query
149+
echo "Query output:"
150+
echo "$output"
151+
152+
# Check if the expected value is in the output
153+
if echo "$output" | grep -q "$value"; then
154+
echo "The DNS record is valid."
155+
return 0
156+
else
157+
echo "The DNS record is invalid or does not exist."
158+
return 1
159+
fi
160+
}
161+
162+
133163
# openssl verify -CAfile trusted-ca.crt -untrusted chain.crt cert.crt
134164

135165
cert-verify() {

lib/extras/git/git-functions

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
# git functions
2+
3+
# Just some local things Mike uses (release early, release often)
4+
#
5+
# Worth a look:
6+
#
7+
# - git-ls-noticed: List files from git index (based on a gitinclude file)
8+
# - git-tarball-create: Creates a tarball based on a gitinclude (special gitginore) file
9+
# - git-repos: List all git repos (remote_url and git_dir)
10+
11+
git-tarball-create() {
12+
13+
# git-tarball-create - Creates a tarball based on a single gitinclude file
14+
#
15+
# Usage: git-tarball-create <gitinclude_file>
16+
#
17+
18+
local gitinclude_file="${1:-}"
19+
20+
# Verify gitinclude_file argument is provided
21+
if [[ -z $gitinclude_file ]]; then
22+
echo "Usage: tarball-create <gitinclude_file>" >&2
23+
return 1
24+
elif [[ ! -f $gitinclude_file ]]; then
25+
# Verify gitinclude_file file exists
26+
echo "Error: gitinclude file '$gitinclude_file' not found." >&2
27+
return 1
28+
fi
29+
30+
local last_commit_date="$(git log -1 --format=%cd --date=format:%Y-%m-%d)"
31+
local git_repo_path="$(git rev-parse --show-toplevel)"
32+
local git_repo_name="${git_repo_path##*/}"
33+
local variant="$(basename "${gitinclude_file}")"
34+
local release="${git_repo_name}-${variant}-${last_commit_date}"
35+
local build_dir="${git_repo_path}/build"
36+
local tarball="${build_dir}/${release}.tar.gz"
37+
38+
(
39+
cd "${git_repo_path}" || exit
40+
# Ensure the build directory exists
41+
mkdir -p "$build_dir" || { echo "Failed to create build directory: $build_dir" >&2; return 1; }
42+
43+
# Use git to list files for inclusion in the tarball
44+
if ! git-ls-noticed "$gitinclude_file" \
45+
| tar \
46+
--transform "flags=r;s,^,${release}/," \
47+
-czf "${tarball}" -T -
48+
then
49+
echo "Error: Failed to create tarball." >&2
50+
return 1
51+
fi
52+
)
53+
54+
echo "Tarball created: ${tarball}"
55+
}
56+
57+
git-archive() {
58+
59+
local last_commit_date="$(git log -1 --format=%cd --date=format:%Y-%m-%d)"
60+
local git_repo_path="$(git rev-parse --show-toplevel)"
61+
local git_repo_name="${git_repo_path##*/}"
62+
local release="${git_repo_name}-${last_commit_date}"
63+
local build_dir="${git_repo_path}/build"
64+
local tarball="${build_dir}/${release}.tar.gz"
65+
local label="$(basename $(git rev-parse --show-toplevel))-$(git log -1 --format=%cd --date=format:%Y-%m-%d)"
66+
67+
(
68+
cd "${git_repo_path}" || exit
69+
70+
# Ensure the build directory exists
71+
mkdir -p "$build_dir" || { echo "Failed to create build directory: $build_dir" >&2; return 1; }
72+
73+
git archive \
74+
--prefix="${release}/" \
75+
--format=tar.gz \
76+
-o "${build_dir}/${release}.tar.gz" \
77+
HEAD
78+
)
79+
}
80+
81+
git-remote-add-github() {
82+
git remote add github "github.com_mbailey:mbailey/$(git-repo-name).git"
83+
git branch -M master
84+
git push -u github master
85+
}
86+
87+
git-repo-name() {
88+
basename "$(git rev-parse --show-toplevel)"
89+
}
90+
91+
git-repos() {
92+
93+
local search_dirs="$(skim-stdin "$@")"
94+
[[ -z $search_dirs ]] && search_dirs='.'
95+
96+
# debug "$search_dirs"
97+
98+
# Find all .git directories under the specified directories
99+
for search_dir in $search_dirs; do
100+
find -L "$search_dir" -type d -name .git 2>/dev/null | while read -r git_dir; do
101+
local repo_dir
102+
repo_dir=$(dirname "$git_dir") # Get the repository directory by removing the .git part
103+
local repo_url
104+
repo_url=$(git -C "$repo_dir" config --get remote.origin.url) # Get the repository's remote URL
105+
printf "%s\t%s\n" "$repo_url" "${repo_dir}" # Print the repository URL and directory path, separated by a tab
106+
done \
107+
| sort -k1,1 \
108+
| bma columnise
109+
done
110+
}
111+
112+
git-ls-noticed() {
113+
# This uses gitignore but flips the meaning. Any file matched here is allowed (instead of ignored).
114+
#
115+
# List files with:
116+
#
117+
# git ls-files --ignored --cached --exclude-from=<path/to/this_file>
118+
#
119+
# How it works:
120+
#
121+
# `--ignored`: Lists all files that are ignored by git
122+
# `--cached`: Lists all files that are staged for commit. (Required to use `--ignored`.)
123+
# `--exclude-from`: Use specified file as the ignore file (and ignore all others)
124+
#
125+
# Test with:
126+
#
127+
# diff <(git-ls-noticed path/to/.gitinclude/target) <(tar-ls build/example-target-2024-04-17.tar.gz)
128+
129+
local gitinclude_file="${1:-}"
130+
131+
if [[ -z "$gitinclude_file" ]]; then
132+
echo "Usage: git-ls-noticed <gitinclude_file>" >&2
133+
return 1
134+
elif [[ ! -f "$gitinclude_file" ]]; then
135+
echo "Error: File '$gitinclude_file' not found." >&2
136+
return 1
137+
fi
138+
139+
# Convert the relative path to an absolute path
140+
gitinclude_file="$(realpath "$gitinclude_file")"
141+
142+
# Ensure command is run within a git repository
143+
local git_repo_path
144+
if ! git_repo_path=$(git rev-parse --show-toplevel); then
145+
echo "Error: Current directory is not within a git repository." >&2
146+
return 1
147+
fi
148+
149+
# Use subshell do directory change is not persistent
150+
(
151+
cd "${git_repo_path}" || return 1
152+
# Use git to list files that are not ignored
153+
git ls-files --ignored --cached --exclude-from="$gitinclude_file"
154+
)
155+
}
156+
157+
158+
159+
git-repo-dirs(){
160+
161+
# list dirs under a path that contain git repos
162+
163+
local search_dir="${1:-~/git}" # Use provided directory or default to current directory if not provided
164+
165+
git-repos "${search_dir}" | cut -f2 | xargs dirname | sort -u | rg -v .old | sed "s#$search_dir/##"
166+
}
167+
168+
git-repos-save() {
169+
(
170+
cd ~/.mt/git
171+
for dir in $(git-repo-dirs ~/git); do
172+
echo $dir
173+
mkdir -p $dir
174+
git-repos ~/git/$dir | tee $dir/git-repos.txt
175+
done
176+
)
177+
}
178+
179+
git-repo-create() {
180+
local repo_name=$(basename "$1" .git) # Ensures repo_name is derived cleanly
181+
182+
# Prevent cloning into the same repository directory
183+
if [ -d "$repo_name.git" ]; then
184+
echo "A directory named '$repo_name.git' already exists. Exiting to avoid conflict."
185+
return 1
186+
fi
187+
188+
# Ensure all files are committed in $repo_name
189+
(
190+
cd "$repo_name" || return 1
191+
if ! git diff --quiet; then
192+
echo "Uncommitted changes found in '$repo_name'. Please commit all changes before proceeding."
193+
return 1
194+
fi
195+
)
196+
197+
# Clone as bare repository
198+
if ! git clone --bare "$1" "$repo_name.git"; then
199+
echo "Cloning failed. Please check the repository path."
200+
return 1
201+
fi
202+
203+
# Secure copy to remote server
204+
if ! scp -r "$repo_name.git" git:git; then
205+
echo "Failed to copy repository to remote server."
206+
return 1
207+
fi
208+
209+
# clone it
210+
git clone "git.failmode.com_m:git/${repo_name}" "${repo_name}.cloned-from-remote"
211+
212+
# Move old repo
213+
mv "${repo_name}" "${repo_name}.cloned_to_remote"
214+
215+
# Move new repo
216+
mv "${repo_name}.cloned-from-remote" "${repo_name}"
217+
218+
echo "Repository '$repo_name.git' successfully cloned, copied to remote server and cloned back."
219+
}
220+
221+
222+

0 commit comments

Comments
 (0)