Skip to content

Instantly share code, notes, and snippets.

@tiesmaster
Last active November 21, 2025 16:13
Show Gist options
  • Select an option

  • Save tiesmaster/d6c4bb9f303ccb9d24b4af2468c4f2b9 to your computer and use it in GitHub Desktop.

Select an option

Save tiesmaster/d6c4bb9f303ccb9d24b4af2468c4f2b9 to your computer and use it in GitHub Desktop.
Rancher wrapper script to follow the oauth flow to your browser
#!/usr/bin/env bash
set -ueo pipefail
# Path to the actual rancher executable
RANCHER_BIN="/opt/homebrew/bin/rancher"
# Check if the first argument is "token"
if [ "$1" = "token" ]; then
# Monitor the output file for the authentication code
"$RANCHER_BIN" "$@" 2> >( while IFS= read -r line; do
code=$(echo "$line" | perl -ne 'm/and enter the code ([A-Z0-9]*) to authenticate./ && print "$1\n"')
if [ -n "$code" ]; then
echo "$code" | pbcopy
open https://www.microsoft.com/devicelogin
fi
done)
exit
fi
# Passthrough: call rancher with all arguments
exec "$RANCHER_BIN" "$@"
@hansbogert
Copy link

hansbogert commented Nov 21, 2025

Simpler:

#!/usr/bin/env bash

set -ueo pipefail

# Path to the actual rancher executable
RANCHER_BIN="/opt/homebrew/bin/rancher"

# Check if the first argument is "token"
if [ "$1" = "token" ]; then
    "$RANCHER_BIN" "$@" 2>  >( tee /dev/tty | while IFS= read -r line; do
        code=$(echo "$line" | perl -ne 'm/and enter the code ([A-Z0-9]*) to authenticate./ && print "$1\n"')
        if [ -n "$code" ]; then
                echo "$code" | pbcopy
                open https://www.microsoft.com/devicelogin
        fi
    done)
    exit
fi

# Passthrough: call rancher with all arguments
exec "$RANCHER_BIN" "$@"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment