is is an inspector for your environment. is tries to make it just a little bit
easier to run commands which rely on a specific OS or a specific CLI version.
is cli version tmux gt 3.2 && echo 🥳 || echo 😢(is os name eq darwin && echo 🍏 ) || (is os name eq linux && echo 🐧)#!/bin/bash
if is there go; then
nvim +:GoUpdateBinaries +qa || true
fiOr, as a one-liner:
is there go && nvim +:GoUpdateBinaries +qa$ is known cli version bash
5.2.15
$ is cli age gofumpt lt 7 d
5.2.15
$ echo "i'm on $(is known os name) for sure"
i'm on darwin for sure
$ is known os name --debug
{
"name": "darwin",
"version": "13.4",
"version-codename": "ventura"
}
darwin
is known os name --debug
{
"id": "ubuntu",
"id-like": "debian",
"name": "linux",
"pretty-name": "Ubuntu 22.04.2 LTS",
"version": "22.04",
"version-codename": "jammy"
}
linux
is returns an exit code of 0 on success and non-zero (usually 1) on
failure. You can leverage this in shell scripting:
In a script:
#!/bin/bash
if is os name eq darwin; then
# This is a Mac. Creating karabiner config dir."
mkdir -p "$HOME/.config/karabiner"
fiAt the command line:
is os name ne darwin && echo "this is not a mac"In bash and zsh (and possibly other shells), $? contains the value of the
last command's exit code.
$ is os name eq x
$ echo $?
1
$ is os name eq darwin
0
Compare against the last modified date of a command's file.
is cli age tmux lt 18 hoursDon't let goimports get more than a week out of date.
is cli age goimports gt 7 days && go install golang.org/x/tools/cmd/goimports@latestUpdate shfmt basically daily, but only if go is installed.
if is there go; then
if is cli age shfmt gt 18 hours; then
go install mvdan.cc/sh/v3/cmd/shfmt@latest
fi
else
echo "Go not found. Not installing shfmt"
fiSupported comparisons are:
ltgt
Supported units are:
ssecondsecondsmminuteminuteshhourhoursddaydays
Note that d|day|days is shorthand for 24 hours. DST offsets are not taken
into account here.
The --debug flag can give you some helpful information when troubleshooting
date math.
Compare versions of available commands. Returns exit code of 0 if condition is true and exit code of 1 if condition is false.
is cli version go gte 1.20.4 || bash upgrade-go.shSupported comparisons are:
ltlteeqgtegtne
Information specific to the current operating system
is os version gt 22Supported comparisons are:
ltlteeqgtegtne
Under the hood, this returns the value of runtime.GOOS, a constant which is
set at compile time. So, is reports on on the OS name which is the target of
build rather than running uname or something like that. This is "good enough"
for my purposes. If it's not good enough for yours, we probably need to add
more build targets.
Available comparisons are:
eqne
is os name eq darwinis os name ne linuxPossible values for name:
darwin
linux
Linux only.
is os pretty-name eq "Ubuntu 22.04.2 LTS"Available comparisons are:
eqne
Linux only.
is os id eq ubuntuAvailable comparisons are:
eqne
is os id-like eq debianAvailable comparisons are:
eqne
is os version-codename eq jammyAvailable comparisons are:
eqne
On Linux, the value for version-codename is taken from /etc/os-release. For
Macs, the values are mapped inside this application.
Possible values for Mac:
- ventura
- monterey
- big sur
- catalina
- mojave
- high sierra
- sierra
- el capitan
- yosemite
- mavericks
- mountain lion
Returns exit code of 0 if command exists and exit code of 1 if command cannot be found.
is there tmux && echo "we have tmux"Returns 1 if the current appears to be able to sudo without being prompted for a password.
This is useful for scripts where you want to install via sudo, but you don't
want the script to be interactive. That means you can skip installing things
that require sudo and handle them in some other place.
Prints known information about a resource to STDOUT. Returns 0 on success
and 1 if info cannot be found.
Details specific to the current operating system.
Under the hood, this returns the value of runtime.GOOS, a constant which is
set at compile time. So, is reports on on the OS name which is the target of
build rather than running uname or something like that. This is "good enough"
for my purposes. If it's not good enough for yours, we probably need to add
more build targets.
$ is known os name
linux
Possible values for name:
darwin
linux
Linux only.
$ is known os pretty-name
Ubuntu 22.04.2 LTS
Linux only.
$ is known os id
ubuntu
Linux only.
$ is known os id-like
debian
$ is known os version
22.04
$ is known os version-codename
jammy
$ is known cli version tmux
3.3a
Print some debugging information to STDOUT.
$ is os name eq darwins --debug
Comparison failed: darwin eq darwins
Top level command help.
Usage: is <command>
Flags:
-h, --help Show context-sensitive help.
--debug turn on debugging statements
--version Print version to screen
Commands:
os <attribute> <op> <val>
Check OS attributes. e.g. "is os name eq darwin"
cli version <name> <op> <val>
Check version of command. e.g. "is cli version tmux gte 3"
cli age <name> <op> <val> <unit>
Check last modified time of cli (2h, 4d). e.g. "is cli age tmux gt 1 d"
known os <attribute>
Print without check. e.g. "is known os name"
known cli <attribute> <name>
Print without check. e.g. "is known cli version git"
there <name>
Check if command exists. e.g. "is there git"
user [<sudoer>]
Info about current user. e.g. "is user sudoer"
Run "is <command> --help" for more information on a command.
Usage: is os <attribute> <op> <val>
Check OS attributes. e.g. "is os name eq darwin"
Arguments:
<attribute> [arch|id|id-like|pretty-name|name|version|version-codename]
<op> [eq|ne|gt|gte|lt|lte]
<val>
Flags:
-h, --help Show context-sensitive help.
--debug turn on debugging statements
--version Print version to screen
Print current version of is
is --version- Download a release
go installgo install github.com/oalders/is@latestgo install github.com/oalders/is@v0.1.1
- Use ubi
#!/usr/bin/env bash
set -eux
INSTALL_DIR="$HOME/local/bin"
if [ ! "$(command -v ubi)" ]; then
curl --silent --location \
https://raw.githubusercontent.com/houseabsolute/ubi/master/bootstrap/bootstrap-ubi.sh |
TARGET=$INSTALL_DIR sh
fi
ubi --project oalders/is --in "$INSTALL_DIR"Forget about the remembering the different version incantations of command
line tools. Don't make up regexes to extract the actual version number.
$ go version
go version go1.20.4 darwin/amd64
$ is known cli version go
1.20.4
$ is known os name
darwin
$ perl --version
This is perl 5, version 36, subversion 1 (v5.36.1) built for darwin-2level
Copyright 1987-2023, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at https://www.perl.org/, the Perl Home Page.
$ is known cli version perl
v5.36.1
$ tmux -V
tmux 3.3a
$ is known cli version tmux
3.3a