Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add log-cmd zsh function #28

Merged
merged 4 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .cz.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ commitizen:
template: .cz.changelog.md.j2
update_changelog_on_bump: true
use_shortcuts: true
version: 1.5.1
version: 1.6.0
version_scheme: semver
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## v1.6.0 (2024-07-07)

### Feature

- **omz**: add log-cmd function to log the stderr and stdout of a command

### Refactor

- **omz**: order functions alphabetically

## v1.5.1 (2024-06-28)

### Fix
Expand Down
164 changes: 115 additions & 49 deletions omz/functions.zsh
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
# functions

joincsv () {
if [[ $1 == "--help" || $1 == "-h" || $1 == "-?" ]]; then
echo "Usage: joincsv [ file1 ] [ file2 ]"
echo -e "Purpose: add data from the second csv as a new column in the first csv"
echo "Example 1:"
echo -e " server> joincsv svrChkr-2015-08-27.csv OWNERS.csv\n"
else
join -t, -a1 --header --nocheck-order <(cat <(head -n1 "$1") <(sed 1d "$1" | sort) | sed 's/\r//') <(cat <(head -n1 "$2") <(sed 1d "$2"| sort) | sed 's/\r//')
fi
}

getsudohash() {
#######################################
# ssh to the server, concatenate the sudo files, and calculate the resulting SHA1 hash
# Arguments:
# $1 - hostname of the server to ssh into
# Outputs:
# Writes the server hostname and resulting SHA1 hash to stdout
#######################################
function getsudohash() {
if [[ $1 == "--help" || $1 == "-h" || $1 == "-?" ]]; then
echo "Usage: getsudohash [ hostname ]"
echo -e "Purpose: ssh to server and report hash"
Expand All @@ -22,19 +18,57 @@ getsudohash() {
fi
}

update_forge_modules () {
for dir in $(ls -1); do
echo $dir
git -C "$dir" fetch -p origin
git -C "$dir" push --mirror
echo
echo

#######################################
# check child directories for pull requests using the GitHub CLI
# Arguments:
# None
# Outputs:
# Writes any existing GitHub pull requests to stdout
#######################################
function git-pr-check() {
if [[ $1 == "--help" || $1 == "-h" || $1 == "-?" ]]; then
echo "Usage: git-pr-check [ --help ]"
echo -e "Purpose: check child directories for pull requests"
echo "Example 1:"
echo -e " server> git-pr-check\n"
return
fi
for dir in $(find . -mindepth 1 -maxdepth 1 -type d -printf '%P\n'); do
cd "$dir"
# has a .git directory
if [ -d .git ]; then
# has a GitHub remote
if git remote -v | grep -iq github; then
echo
echo "$(tput setaf 2)🟢 $dir$(tput sgr0)"
gh pr list
echo
# does not have a GitHub remote
else
echo "$(tput setaf 214)🟠 $dir, not using GitHub$(tput sgr0)"
cd ..
continue
fi
# does not have a .git directory
else
echo "$(tput setaf 7)➖ $dir, not a git repo$(tput sgr0)"
fi
cd ..
done
}

# function to semantically tag a git repository, optionally with floating tags
# usage: git-tag-semver <major|minor|patch> [float] [push] [--help]
git-tag-semver() {

#######################################
# add signed & annontated semantic version tags(either single or floating tags) to a git repository
# Arguments:
# $1 - major|minor|patch (required)
# $2 - 'float' to add floating tags, null otherwise
# $3 - 'push' the tags to the remote
# Outputs:
# Writes the latest tag, the new tag, and the floating tags (if requested) to stdout
#######################################
function git-tag-semver() {
if [[ $1 == "--help" || $1 == "-h" || $1 == "-?" ]]; then
echo "Usage: git-tag-semver [ major | minor | patch ] [float] [push] [--help]"
echo -e "Purpose: semantically tag a git repository"
Expand Down Expand Up @@ -96,36 +130,68 @@ git-tag-semver() {
fi
}

# function to check child directories for pull requests
# usage: git-check-prs [ --help ]
git-pr-check() {

#######################################
# join two csv files on the first column. The first row of each file is assumed to be the header.
# Arguments:
# $1 - 'file1' the first csv file
# $2 - 'file2' the second csv file
# Outputs:
# Writes the joined csv to stdout
#######################################
function joincsv () {
if [[ $1 == "--help" || $1 == "-h" || $1 == "-?" ]]; then
echo "Usage: git-pr-check [ --help ]"
echo -e "Purpose: check child directories for pull requests"
echo "Usage: joincsv [ file1 ] [ file2 ]"
echo -e "Purpose: add data from the second csv as a new column in the first csv"
echo "Example 1:"
echo -e " server> git-pr-check\n"
return
echo -e " server> joincsv svrChkr-2015-08-27.csv OWNERS.csv\n"
else
join -t, -a1 --header --nocheck-order <(cat <(head -n1 "$1") <(sed 1d "$1" | sort) | sed 's/\r//') <(cat <(head -n1 "$2") <(sed 1d "$2"| sort) | sed 's/\r//')
fi
for dir in $(find . -mindepth 1 -maxdepth 1 -type d -printf '%P\n'); do
cd "$dir"
# has a .git directory
if [ -d .git ]; then
# has a GitHub remote
if git remote -v | grep -iq github; then
echo
echo "$(tput setaf 2)🟢 $dir$(tput sgr0)"
gh pr list
echo
# does not have a GitHub remote
else
echo "$(tput setaf 214)🟠 $dir, not using GitHub$(tput sgr0)"
cd ..
continue
fi
# does not have a .git directory
else
echo "$(tput setaf 7)➖ $dir, not a git repo$(tput sgr0)"
}


#######################################
# log a shell command, its stdout, and stderr to a file
# Globals:
# CMD_LOG_FILE - the file to log the command and its output to
# Arguments:
# $* - the command to execute
# Outputs:
# Writes the command and its output to the log file and stdout
#######################################
function log-cmd () {
local header="####################################################\n"

# check if log file has been named
if [ -z "${CMD_LOG_FILE}" ]; then
local CMD_LOG_FILE="/tmp/command.log"
echo "No log file specified, using default: ${CMD_LOG_FILE}\n"
fi
cd ..

{
printf ${header}
printf "\`%s\` executed at $(date '+%Y-%m-%d %H:%M:%S')\n" "$*"
printf ${header}
"$@"
printf "\n"
} 2>&1 | tee -a "${CMD_LOG_FILE}"
}


#######################################
# update all mirrored puppet forge modules in the current directory
# Arguments:
# None
# Outputs:
# Writes the git fetch and push status to stdout
#######################################
function update_forge_modules () {
for dir in $(ls -1); do
echo $dir
git -C "$dir" fetch -p origin
git -C "$dir" push --mirror
echo
echo
done
}