Skip to content

Instantly share code, notes, and snippets.

@a-nldisr
Last active October 12, 2022 21:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save a-nldisr/176f5da07187f827b6734edf2b62ca7a to your computer and use it in GitHub Desktop.
Save a-nldisr/176f5da07187f827b6734edf2b62ca7a to your computer and use it in GitHub Desktop.
Keybase CP script.
#!/usr/bin/env bash
# Script created by Rogier Dikkes.
# This script is licensed under the GNU GPL version 3.0.
# This script is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# <http://www.gnu.org/licenses/>.
# Keybase often errors out when using keybase fs on ARM64. This script will exit.
# Make sure the teams folder structure is filled with files.
# This setup only copies top level directory structures over.
# If you have a file in your top level, you need to either place it in a folder or do this by hand.
set -e
KEYBASE_BINARY="$(which keybase)"
export KEYBASE_BINARY
TEAM_NAME="$1"
export TEAM_NAME
KEYBASE_FS_PATH="/keybase/team"
export KEYBASE_FS_PATH
script_directory() {
# Get the directory where this script is located.
SCRIPT_DIR="$(dirname "$(greadlink -f "${BASH_SOURCE[0]}")")"
echo "Copying files to: ${SCRIPT_DIR}"
}
keybase_folders() {
# Get the keybase folders inside the teams.
while IFS= read -r line ; do
# create teams directory locally
create_directory "$line"
# Get only directories inside the teams.
KEYBASE_FOLDERS=$(${KEYBASE_BINARY} fs ls -w "$KEYBASE_FS_PATH"/"$line"|grep '<DIR>'|awk '{print $NF}')
# We only loop over the folders inside the teams.
if [ -n "$KEYBASE_FOLDERS" ]; then
while IFS= read -r keybase_folders ; do
# Copy the files per team to the local directory.
keybase_copy "$line" "$keybase_folders"
done <<< "${KEYBASE_FOLDERS[@]}"
fi
done <<< "${KEYBASE_TEAMS[@]}"
}
keybase_copy() {
# Constructing the keybase path to copy files from.
CON_KEYBASE_PATH="$KEYBASE_FS_PATH/$1/$2"
# Constucting the local path to copy files to.
CON_FILESYS_PATH="${SCRIPT_DIR}/$1/$2"
# Create the local directory if it doesn't exist, keybase does not allow overwriting.
if [ ! -d "$CON_FILESYS_PATH" ]; then
# Copy the files from keybase to the local filesystem.
echo "Copying keybase folder recursively from: ${CON_KEYBASE_PATH} towards: ${CON_FILESYS_PATH}"
${KEYBASE_BINARY} fs cp -r -f "$CON_KEYBASE_PATH" "$CON_FILESYS_PATH"
# for some reason keybase copies all files with root permissions, chown them to the user.
sudo chown -R "${USER}":staff "${CON_FILESYS_PATH}"
fi
}
create_directory() {
# Create the directory.
if [ ! -d "${SCRIPT_DIR}/${1}" ]; then
echo "Creating directory: ${SCRIPT_DIR}/${1}"
mkdir "${SCRIPT_DIR}/${1}"
fi
}
get_keybase_teams() {
# Get the keybase teams.
KEYBASE_TEAMS="$(keybase team list-memberships | grep -v Team | grep -v '^$' | awk '{print $1}')"
echo -e "Keybase teams: \n${KEYBASE_TEAMS}"
}
check_greadlink() {
# Check if greadlink is installed.
if ! command -v greadlink >/dev/null 2>&1; then
echo "greadlink is not installed, please install it."
echo "brew install coreutils"
exit 1
fi
}
check_os() {
# This script only works on Apple M1.
if [[ "$OSTYPE" != "darwin"* ]]; then
echo "Operating system not supported."
exit 1
fi
if uname -a | grep -v -q 'ARM64' ; then
echo "This script is only tested on ARM64."
exit 1
fi
}
main() {
check_os
check_greadlink
script_directory
get_keybase_teams
keybase_folders
}
main
# Known issues:
# - When using keybase fs ls -w, it sometimes errors out. (No such handle, name exists code 5101, Async result not found)
# - When using keybase fs cp -r -f, it sometimes errors out.
#
# - when a directory already exists, keybase will not overwrite it. It will gladly add extra contents underneath that directory.
# example: /keybase/team/team_name/folder_1/file_name
# example: /keybase/team/team_name/folder_1/folder_1/file_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment