|
#!/usr/bin/env expect |
|
|
|
# Set the timeout period for the script to wait for the output |
|
set timeout -1 |
|
|
|
# Check if KEYBASE_USERNAME is provided; if not, exit with a message |
|
if {[info exists env(KEYBASE_USERNAME)]} { |
|
set username $env(KEYBASE_USERNAME) |
|
} else { |
|
puts stderr "Error: Please provide the username via `KEYBASE_USERNAME`." |
|
puts stderr "Usage: ./keybase-login.exp <username> <provisioning_devicenum> <new_devicename>" |
|
exit 1 |
|
} |
|
|
|
# Check if a KEYBASE_DEVICENUM is provided; if not, set the default value of 1 |
|
if {[info exists env(KEYBASE_DEVICENUM)]} { |
|
set prov_devicenum $env(KEYBASE_DEVICENUM) |
|
} else { |
|
set prov_devicenum 1 |
|
} |
|
|
|
# Check if a KEYBASE_DEVICENAME is provided; if not, set the default value of hostname |
|
if {[info exists env(KEYBASE_DEVICENAME)]} { |
|
set new_devicename $env(KEYBASE_DEVICENAME) |
|
} else { |
|
set new_devicename [exec hostname -s] |
|
} |
|
|
|
# Remove the first argument (program name) and store the remaining arguments |
|
if {$argc > 0} { |
|
set program_name [lindex $argv 0] |
|
set remaining_args [lrange $argv 1 end] |
|
} |
|
|
|
# Spawn the keybase cli that generates the output |
|
spawn keybase login $username |
|
|
|
expect "Choose a device:" |
|
sleep 1; send -- "$prov_devicenum\r" |
|
|
|
expect "Enter a public name for this device:" |
|
sleep 1; send -- "$new_devicename\r" |
|
|
|
expect "Type this verification code into your other device:" |
|
|
|
# Extract the verification code using regular expression and save it in the variable 'code' |
|
expect -re {(\w+ \w+ \w+ \w+ \w+ \w+ \w+ \w+)} |
|
set code $expect_out(1,string) |
|
|
|
expect "It will then prompt you for the verification code above." |
|
|
|
# Print the extracted verification code to stderr |
|
# puts stderr "Verification code for `keybase device add`:\n$code" |
|
|
|
# Wait for the spawned program to close |
|
expect eof |
|
|
|
if {[info exists program_name]} { |
|
# Spawn the user program with additional arguments |
|
spawn $program_name {*}$remaining_args |
|
|
|
# Enable direct interaction between the user and the spawned process |
|
interact |
|
} |