Displaying the active Amplify Environment alongside the current Git branch

Working with a lot of Git branches can get a bit tricky when they align to different Amplify environments. I usually find myself checking amplify status or amplify env list to determine the active environment. Below is an approach that is a bit more dy…


This content originally appeared on DEV Community and was authored by siegerts

Working with a lot of Git branches can get a bit tricky when they align to different Amplify environments. I usually find myself checking amplify status or amplify env list to determine the active environment. Below is an approach that is a bit more dynamic and similar to what Git and Python virtual environments show while working in the terminal.

Prerequisites

You'll need to have the below installed for the function to take effect.

  • git - used to determine the root directory of your Amplify project
  • jq - helps with parsing JSON
  • oh-my-zsh - managing terminal functions and themes

Display the active Amplify env

Add the below into your .zshrc or .bashrc. The output of the function can be adjusted depending on how you'd like to display the information. I display mine to the left of the current working directory by adjusting my zsh custom theme (below). This puts the environment name in a similar spot to where an active Python virtual environment name will show.

# .zshrc

amplify_env () {
    PROJECT_DIR=$(git rev-parse --show-toplevel 2>/dev/null) 

    ENV=$PROJECT_DIR/amplify/.config/local-env-info.json 

    if [ -f "$ENV" ]; then
        env_info=$(cat $ENV | jq -r ".envName") 
        echo "(? $env_info)"
    fi
}

The function checks for the current working environment name in the <project>/amplify/.config/local-env-info.json file. This approach requires a few extra steps but is quicker than re-computing on each terminal input using the amplify status CLI command.

After re-sourcing your .zshrc (below), the environment function can be invoked by running amplify_env in the terminal.

source ~/.zshrc

Adding to a custom theme

If you're using oh-my-zsh, then you can adjust your theme (or any theme) to add the output of the function so that it shows in the terminal prompts. The updated PROMPT below now includes the Amplify environment function output (${amplify_env}).

# ~/.oh-my-zsh/custom/themes/<my-custom-theme>.zsh-theme

- PROMPT='${ret_status} %{$fg[cyan]%}%c%{$reset_color%} '
+ PROMPT='$(amplify_env) ${ret_status} %{$fg[cyan]%}%c%{$reset_color%} '
...

Amplify env + Git branch = ?

Now the environment is displayed alongside the Git branch that is active. This is a nice way to quickly determine if I need to switch branches or environments to match.

Alt Text

Hopefully that helps keep your Amplify environments visually in sync with your current Git branch ?.


This content originally appeared on DEV Community and was authored by siegerts


Print Share Comment Cite Upload Translate Updates
APA

siegerts | Sciencx (2021-05-10T16:50:10+00:00) Displaying the active Amplify Environment alongside the current Git branch. Retrieved from https://www.scien.cx/2021/05/10/displaying-the-active-amplify-environment-alongside-the-current-git-branch/

MLA
" » Displaying the active Amplify Environment alongside the current Git branch." siegerts | Sciencx - Monday May 10, 2021, https://www.scien.cx/2021/05/10/displaying-the-active-amplify-environment-alongside-the-current-git-branch/
HARVARD
siegerts | Sciencx Monday May 10, 2021 » Displaying the active Amplify Environment alongside the current Git branch., viewed ,<https://www.scien.cx/2021/05/10/displaying-the-active-amplify-environment-alongside-the-current-git-branch/>
VANCOUVER
siegerts | Sciencx - » Displaying the active Amplify Environment alongside the current Git branch. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/10/displaying-the-active-amplify-environment-alongside-the-current-git-branch/
CHICAGO
" » Displaying the active Amplify Environment alongside the current Git branch." siegerts | Sciencx - Accessed . https://www.scien.cx/2021/05/10/displaying-the-active-amplify-environment-alongside-the-current-git-branch/
IEEE
" » Displaying the active Amplify Environment alongside the current Git branch." siegerts | Sciencx [Online]. Available: https://www.scien.cx/2021/05/10/displaying-the-active-amplify-environment-alongside-the-current-git-branch/. [Accessed: ]
rf:citation
» Displaying the active Amplify Environment alongside the current Git branch | siegerts | Sciencx | https://www.scien.cx/2021/05/10/displaying-the-active-amplify-environment-alongside-the-current-git-branch/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.