Skip to content

Instantly share code, notes, and snippets.

@adriangl
Created April 29, 2020 07:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adriangl/f8949a47104fad2c41416844a5df68a6 to your computer and use it in GitHub Desktop.
Save adriangl/f8949a47104fad2c41416844a5df68a6 to your computer and use it in GitHub Desktop.
Firebase CLI utils to ease switching user accounts

What this is about

These scripts provide a set of utilities to manage different Firebase CLI user accounts (from now on called profiles) in the same computer. As a summary, these scripts switch between different firebase-tools.json files which contain the login information for a given Google account.

This solution comes after reading suggestion from this open issue in firebase-tools: firebase/firebase-tools#181

The different scripts do a different set of actions:

  • firebase-profile-copy copies the currently set profile to a named profile, and sets said profile as the active one.
  • firebase-profile-set changes the active profile to another one previously registered with firebase-copy-profile.
  • firebase-profile-list lists the active profile and the list of available profiles registered by the user.

Usage

  1. Add the scripts to your PATH for easier usage with the method of your choice
  2. First, do firebase login and log in with one of your accounts. If you're already logged in with the account, there's no need to redo the login process.
  3. Execute firebase-profile-copy <your_profile_name> to copy your profile with a given name. The profile will be registered and saved as the active one.
  4. If you want to add more accounts, do firebase logout and re-do steps 2. and 3. (and 4. if you need more accounts)
  5. When you have all your profiles copied, you can switch between them with firebase-profile-set <your_profile_name>. Note: you may have to log in again with the account that the profile has since you might have logged out of the account in earlier steps. As soon as you do the login again, you shouldn't have any more log in problems.
  6. To see which profile is active, and a list of profiles, just do firebase-profile-list

License

Copyright (C) 2020 Adrián García

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Firebase-CLI-Profile-Utils
#!/bin/sh
# Copyright (C) 2020 Adrián García
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Directory where Firebase config is stored
FIREBASE_TOOLS_DIRECTORY_PATH="$HOME/.config/configstore"
# Main Firebase file
FIREBASE_TOOLS_FILE_PATH="$FIREBASE_TOOLS_DIRECTORY_PATH/firebase-tools.json"
# Simple file that contains the active profile name
PROFILE_CONFIG_FILE_PATH="$FIREBASE_TOOLS_DIRECTORY_PATH/.profile_config"
# File where all defined profiles are saved
PROFILE_LIST_FILE_PATH="$FIREBASE_TOOLS_DIRECTORY_PATH/.profile_list"
# Profile name to use
PROFILE_NAME=$1
if [ $# -ne 1 ]; then
echo "You need to provide a profile name in order to copy your current profile"
exit 1
fi
COPY_FILE_PATH="$FIREBASE_TOOLS_DIRECTORY_PATH/firebase-tools-$PROFILE_NAME.json"
echo "Copying current profile to $COPY_FILE_PATH and setting as active profile..."
cp "$FIREBASE_TOOLS_FILE_PATH" "$COPY_FILE_PATH"
echo "$PROFILE_NAME" >"$PROFILE_CONFIG_FILE_PATH"
printf '%s\n' "$PROFILE_NAME" >>"$PROFILE_LIST_FILE_PATH"
echo "Copied profile $PROFILE_NAME to $COPY_FILE_PATH and set as active profile."
#!/bin/sh
# Copyright (C) 2020 Adrián García
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Directory where Firebase config is stored
FIREBASE_TOOLS_DIRECTORY_PATH="$HOME/.config/configstore"
# Simple file that contains the active profile name
PROFILE_CONFIG_FILE_PATH="$FIREBASE_TOOLS_DIRECTORY_PATH/.profile_config"
# File where all defined profiles are saved
PROFILE_LIST_FILE_PATH="$FIREBASE_TOOLS_DIRECTORY_PATH/.profile_list"
# Get active profile
ACTIVE_PROFILE=$(cat "$PROFILE_CONFIG_FILE_PATH")
if [ "$ACTIVE_PROFILE" != "" ]; then
echo "Active profile: $ACTIVE_PROFILE"
else
echo "Active profile: No active profile"
fi
if [ -f "$PROFILE_LIST_FILE_PATH" ]; then
PROFILE_LIST=$(cat "$PROFILE_LIST_FILE_PATH")
printf 'Available profiles: \n%s\n' "$PROFILE_LIST"
else
echo "No available profiles"
fi
#!/bin/sh
# Copyright (C) 2020 Adrián García
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Directory where Firebase config is stored
FIREBASE_TOOLS_DIRECTORY_PATH="$HOME/.config/configstore"
# Main Firebase file
FIREBASE_TOOLS_FILE_PATH="$FIREBASE_TOOLS_DIRECTORY_PATH/firebase-tools.json"
# Simple file that contains the active profile name
PROFILE_CONFIG_FILE_PATH="$FIREBASE_TOOLS_DIRECTORY_PATH/.profile_config"
if [ $# -ne 1 ]; then
echo "You need to provide a profile name to change the current profile"
exit 1
fi
# Profile name to use and path of the profile file
NEW_PROFILE=$1
NEW_PROFILE_FILE_PATH="$FIREBASE_TOOLS_DIRECTORY_PATH"/firebase-tools-$NEW_PROFILE.json
# Check first if you have the profile to set profile
if [ -f "$NEW_PROFILE_FILE_PATH" ]; then
# Check if you have an active profile and save changes to its proper file
ACTIVE_PROFILE=$(cat "$PROFILE_CONFIG_FILE_PATH")
if [ "$ACTIVE_PROFILE" != "" ]; then
echo "Saving current profile $ACTIVE_PROFILE..."
ACTIVE_PROFILE_FILE_PATH="$FIREBASE_TOOLS_DIRECTORY_PATH/firebase-tools-$ACTIVE_PROFILE.json"
cp "$FIREBASE_TOOLS_FILE_PATH" "$ACTIVE_PROFILE_FILE_PATH"
fi
# Copy profile to main Firebase tools file
echo "Activating profile $NEW_PROFILE"
# shellcheck disable=SC2216
yes | cp -f "$NEW_PROFILE_FILE_PATH" "$FIREBASE_TOOLS_FILE_PATH"
echo "$NEW_PROFILE" >"$PROFILE_CONFIG_FILE_PATH"
echo "New profile: $NEW_PROFILE"
else
printf 'No profile file for profile %s found.\nRun firebase-copy-profile with the active profile.\n' "$NEW_PROFILE"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment