Files
snapd/tests/lib/tools/os.query
2021-07-20 13:01:53 -03:00

159 lines
3.1 KiB
Bash
Executable File

#!/bin/bash
show_help() {
echo "usage: is-core, is-classic"
echo " is-core16, is-core18, is-core20"
echo " is-trusty, is-xenial, is-bionic, is-focal, is-groovy, is-hirsute"
echo " is-ubuntu, is-debian, is-fedora, is-amazon-linux, is-arch-linux, is-centos, is-centos-7, is-opensuse"
echo " is-opensuse-tumbleweed, is-debian-sid, is-debian-10"
echo " is-pc-amd64, is-pc-i386, is-arm, is-armhf, is-arm64"
echo ""
echo "Get general information about the current system"
}
is_core() {
[[ "$SPREAD_SYSTEM" == ubuntu-core-* ]]
}
is_core16() {
[[ "$SPREAD_SYSTEM" == ubuntu-core-16-* ]]
}
is_core18() {
[[ "$SPREAD_SYSTEM" == ubuntu-core-18-* ]]
}
is_core20() {
[[ "$SPREAD_SYSTEM" == ubuntu-core-20-* ]]
}
is_classic() {
! is_core
}
is_trusty() {
grep -qFx 'ID=ubuntu' /etc/os-release && grep -qFx 'VERSION_ID="14.04"' /etc/os-release
}
is_xenial() {
grep -qFx 'UBUNTU_CODENAME=xenial' /etc/os-release
}
is_bionic() {
grep -qFx 'UBUNTU_CODENAME=bionic' /etc/os-release
}
is_focal() {
grep -qFx 'UBUNTU_CODENAME=focal' /etc/os-release
}
is_groovy() {
grep -qFx 'UBUNTU_CODENAME=groovy' /etc/os-release
}
is_hirsute() {
grep -qFx 'UBUNTU_CODENAME=hirsute' /etc/os-release
}
is_impish() {
grep -qFx 'UBUNTU_CODENAME=impish' /etc/os-release
}
is_ubuntu() {
grep -qFx 'ID=ubuntu' /etc/os-release || grep -qFx 'ID=ubuntu-core' /etc/os-release
}
is_debian() {
grep -qFx 'ID=debian' /etc/os-release
}
is_debian_10() {
grep -qFx 'ID=debian' /etc/os-release && grep -qFx 'VERSION_ID="10"' /etc/os-release
}
is_debian_sid() {
[[ "$SPREAD_SYSTEM" == debian-sid-* ]]
}
is_fedora() {
grep -qFx 'ID=fedora' /etc/os-release
}
is_amazon_linux() {
grep -qFx 'ID="amzn"' /etc/os-release
}
is_centos() {
grep -qFx 'ID="centos"' /etc/os-release
}
is_centos_7() {
grep -qFx 'ID="centos"' /etc/os-release && grep -qFx 'VERSION_ID="7"' /etc/os-release
}
is_arch_linux() {
grep -qFx 'ID=arch' /etc/os-release
}
is_opensuse() {
grep -qFx 'ID="opensuse-leap"' /etc/os-release || grep -qFx 'ID="opensuse-tumbleweed"' /etc/os-release
}
is_opensuse_tumbleweed() {
grep -qFx 'ID="opensuse-tumbleweed"' /etc/os-release
}
is_pc_amd64() {
uname -m | grep -qFx 'x86_64'
}
is_pc_i386() {
uname -m | grep -Eq '(i686|i386)'
}
is_arm() {
uname -m | grep -Eq '(^arm.*|^aarch*)'
}
is_armhf() {
uname -m | grep -qx 'armv7.*'
}
is_arm64() {
uname -m | grep -Eq '(aarch64.*|armv8.*)'
}
main() {
if [ $# -eq 0 ]; then
show_help
exit 0
fi
local subcommand="$1"
local action=
while [ $# -gt 0 ]; do
case "$1" in
-h|--help)
show_help
exit 0
;;
*)
action=$(echo "$subcommand" | tr '-' '_')
shift
break
;;
esac
done
if [ -z "$(declare -f "$action")" ]; then
echo "os.query: no such command: $subcommand" >&2
show_help
exit 1
fi
"$action" "$@"
}
main "$@"