Last active
March 30, 2023 08:49
-
-
Save casaper/7fbb715a8c63d1d2dc1830d9a8fed49f to your computer and use it in GitHub Desktop.
MacOs system info zsh functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The OS is macOS (darwin) | |
function is_mac() { [[ "$OSTYPE" == darwin* ]]; } | |
# The OS is macOS (darwin) running on an M1 (arm64) CPU | |
function is_mac_arm() { is_mac && [[ "$CPUTYPE" == arm64 ]]; } | |
# The OS is macOS (darwin) running on an Intel (amd64) CPU | |
function is_mac_intel() { is_mac && [[ "$CPUTYPE" == x86_64 ]]; } | |
# Does shell env have command | |
function is_cmd() { command -v "$1" &>/dev/null; } | |
# timestamp sane for filenames | |
function timestamp_filename () { date +%y-%m-%dT%H%M%S; } | |
if ! is_mac; then | |
echo "This script is made for MacOS computers" | |
exit 1 | |
fi | |
if is_mac_arm; then | |
# Add rosetta homebrew alias | |
alias ibrew='arch -x86_64 /usr/local/bin/brew' | |
alias brew='arch -arm64e /opt/homebrew/bin/brew' | |
alias iarch='arch -x86_64' | |
alias march='arch -arm64e' | |
else | |
alias ibrew='echo "not a aarch64 mac"; exit 1;' | |
alias iarch='echo "not a aarch64 mac"; exit 1;' | |
fi | |
alias brewu="brew update && brew upgrade; brew cleanup" | |
brew install m-cli | |
npm i -g prettier | |
function __macos_sysinfo_yml() { | |
if ! is_cmd 'json2yaml'; then | |
if ! is_cmd 'npm'; then | |
brew install node | |
export PATH="$(brew --prefix node)/bin:$(brew --prefix node)/libexec/bin:$PATH" | |
fi | |
npm install -g json2yaml | |
fi | |
echo '---' | |
m info | |
echo 'kernel:' | |
echo " name: '$(uname -s)'" | |
echo " nodename: '$(uname --nodename)'" | |
echo " release: '$(uname -r)'" | |
echo " version: '$(uname -v)'" | |
echo " machine: '$(uname -m)'" | |
echo " processor: '$(uname -p)'" | |
echo " hardware_platform: '$(uname -i)'" | |
echo " operating_system: '$(uname -o)'" | |
system_profiler -json \ | |
SPSoftwareDataType \ | |
SPHardwareDataType \ | |
SPDeveloperToolsDataType | json2yaml | | |
grep --invert-match 'serial_number: ' | | |
grep --invert-match 'platform_UUID: ' | | |
grep --invert-match 'provisioning_UDID: ' | | |
tail -n +2 | |
} | |
function system_update_report() { | |
if ! [[ "$SHELL" == *zsh* ]]; then | |
echo "This function is made for Z-Shell (ZSH)"; | |
return 1 | |
fi | |
if ! is_cmd 'json2yaml' || ! is_cmd 'prettier'; then | |
if ! is_cmd 'npm' || ! is_cmd 'node'; then | |
brew install nvm | |
cat <<EOD >> ~/.zshrc | |
export NVM_DIR=~/.nvm | |
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" | |
EOD | |
fi | |
source ~/.zshrc | |
nvm install --lts | |
nvm install-latest-npm | |
nvm alias default stable | |
npm install -g json2yaml prettier | |
fi | |
if ! is_cmd 'm'; then | |
brew install m-cli | |
fi | |
TIMESTAMP="$(date +%Y-%m-%dT%H%M%S)" | |
ISO_TIMESTAMP="$(date --iso-8601=seconds)" | |
MD_REPORT_PATH="mac_info_${TIMESTAMP}.md" | |
system_profiler -json SPInstallHistoryDataType | json2yaml >"mac_info_install_history_${TIMESTAMP}.yml" | |
system_profiler -json SPApplicationsDataType | json2yaml >"mac_info_applications_${TIMESTAMP}.yml" | |
__macos_sysinfo_yml >"mac_info_general_${TIMESTAMP}.yml" | |
touch "$MD_REPORT_PATH" | |
alias echo='echo -e' | |
# yaml frontmatter | |
echo '---' >"$MD_REPORT_PATH" | |
m info >>"$MD_REPORT_PATH" | |
echo "date: ${ISO_TIMESTAMP}" >>"$MD_REPORT_PATH" | |
echo $'---\n' >>"$MD_REPORT_PATH" | |
# markdown title | |
echo "# Computer summary (${ISO_TIMESTAMP})"$'\n' >>"$MD_REPORT_PATH" | |
echo $'## macOS\n' >>"$MD_REPORT_PATH" | |
echo $'### System update settings\n' >>"$MD_REPORT_PATH" | |
echo '```sh' >>"$MD_REPORT_PATH" | |
echo $'$ softwareupdate --list' >>"$MD_REPORT_PATH" | |
softwareupdate --list >>"$MD_REPORT_PATH" 2>&1 | |
echo $'\n$ softwareupdate --schedule' >>"$MD_REPORT_PATH" | |
softwareupdate --schedule >>"$MD_REPORT_PATH" 2>&1 | |
echo $'```\n' >>"$MD_REPORT_PATH" | |
echo $'### Info yaml\n' >>"$MD_REPORT_PATH" | |
echo '```yaml' >>"$MD_REPORT_PATH" | |
tail -n +2 "mac_info_general_${TIMESTAMP}.yml" >>"$MD_REPORT_PATH" | |
echo $'```\n' >>"$MD_REPORT_PATH" | |
echo $'## Homebrew Status\n' >>"$MD_REPORT_PATH" | |
if is_mac_arm; then | |
echo $'### Arch arm64\n' >>"$MD_REPORT_PATH" | |
fi | |
echo '```sh' >>"$MD_REPORT_PATH" | |
echo $'$ brew update && brew upgrade' >>"$MD_REPORT_PATH" | |
brew update && brew upgrade >>"$MD_REPORT_PATH" 2>&1 | |
echo $'\n$ brew autoupdate status' >>"$MD_REPORT_PATH" | |
brew autoupdate status >>"$MD_REPORT_PATH" 2>&1 | |
echo $'```\n' >>"$MD_REPORT_PATH" | |
if is_mac_arm; then | |
echo $'### Arch amd64\n' >>"$MD_REPORT_PATH" | |
echo $'```sh' >>"$MD_REPORT_PATH" | |
echo $'$ which ibrew' >>"$MD_REPORT_PATH" | |
which ibrew >>"$MD_REPORT_PATH" 2>&1 | |
echo $'\n$ ibrew update && ibrew upgrade' >>"$MD_REPORT_PATH" | |
ibrew update && ibrew upgrade >>"$MD_REPORT_PATH" 2>&1 | |
echo $'\n$ brew autoupdate status' >>"$MD_REPORT_PATH" | |
ibrew autoupdate status >>"$MD_REPORT_PATH" 2>&1 | |
echo $'```\n' >>"$MD_REPORT_PATH" | |
fi | |
echo $'## Homebrew Versions\n' >>"$MD_REPORT_PATH" | |
if is_mac_arm; then | |
echo $'### Arch arm64\n' >>"$MD_REPORT_PATH" | |
fi | |
echo '```sh' >>"$MD_REPORT_PATH" | |
echo $'$ brew list --versions' >>"$MD_REPORT_PATH" | |
brew list --versions >>"$MD_REPORT_PATH" 2>&1 | |
echo $'```\n' >>"$MD_REPORT_PATH" | |
if is_mac_arm; then | |
echo $'### Arch amd64\n' >>"$MD_REPORT_PATH" | |
echo $'```sh' >>"$MD_REPORT_PATH" | |
echo $'$ brew list --versions' >>"$MD_REPORT_PATH" | |
brew list --versions >>"$MD_REPORT_PATH" 2>&1 | |
echo $'```\n' >>"$MD_REPORT_PATH" | |
fi | |
prettier -w *"_${TIMESTAMP}".{yml,md} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment