Skip to content

Commit

Permalink
Rename the default local env file name and use a global config for it
Browse files Browse the repository at this point in the history
  • Loading branch information
sherpalabsio committed Jul 8, 2024
1 parent ac3164a commit 8da6cbc
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
!/tests/**/.local-sherpa
!/tests/**/.sherparc
28 changes: 15 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function_1() {
```

```shell
# ~/projects/project_awesome/.local-sherpa
# ~/projects/project_awesome/.sherparc
export VAR_1="LOCAL VAR PROJECT AWESOME"

alias alias_1='echo "LOCAL ALIAS PROJECT AWESOME"'
Expand All @@ -77,7 +77,7 @@ Sherpa won't load any local env file unless you trust the directory first.
This is to prevent running malicious code when you `cd` into a directory.

``` bash
$ echo "alias rs=rspec" > ~/projects/project_awesome/.local-sherpa
$ echo "alias rs=rspec" > ~/projects/project_awesome/.sherparc
$ cd ~/projects/project_awesome
Sherpa: The local env file is not trusted. Run `sherpa trust` to mark it as trusted.
$ rs
Expand Down Expand Up @@ -123,8 +123,8 @@ Unexported variables and other data types are not supported yet.
$ git clone [email protected]:tothpeter/local_sherpa.git ~/.dotfiles/lib/local_sherpa
# Hook it into your shell
$ echo "source ~/.dotfiles/lib/local_sherpa/local_sherpa.sh" >> ~/.zshrc
# Exclude the local env Sherpa files (.local-sherpa) globally in Git
$ echo ".local-sherpa" >> $(git config --global core.excludesfile)
# Exclude the local env Sherpa files (.sherparc) globally in Git
$ echo ".sherparc" >> $(git config --global core.excludesfile)

# Optional but recommended
alias se='sherpa edit'
Expand All @@ -145,8 +145,8 @@ if you find it useful.

```shell
# Given the following directory structure with the corresponding local env files
# ~/projects/.local-sherpa
# ~/projects/project_awesome/.local-sherpa
# ~/projects/.sherparc
# ~/projects/project_awesome/.sherparc
# ~/projects/project_awesome/subdir

$ cd ~/projects/
Expand Down Expand Up @@ -183,7 +183,9 @@ Sherpa: Local env loaded. Sherpa is ready for action.

### Local env file

🚧 Comming up
```shell
export SHERPA_LOCAL_ENV_FILE='.sherparc'
```

## Cookbook

Expand All @@ -192,15 +194,15 @@ Sherpa: Local env loaded. Sherpa is ready for action.
```shell
# Run RSpec in the `project-awesome-api` Docker container

# ~/projects/project_awesome_api/.local-sherpa
# ~/projects/project_awesome_api/.sherparc
alias de='docker exec -it project-awesome-api'
alias rs='de rspec'
```

```shell
# Run RSpec on the host machine

# ~/projects/project_for_mortals/.local-sherpa
# ~/projects/project_for_mortals/.sherparc
alias rs='bin/rspec'
```

Expand All @@ -209,21 +211,21 @@ With this config `RSpec` will run depending on in which directory you `cd` into.
### Rails console in production 🤫

```shell
# ~/projects/project_with_heroku/.local-sherpa
# ~/projects/project_with_heroku/.sherparc
alias rc_prod='heroku run rails c -a APP_NAME'

# ~/projects/project_with_aws/.local-sherpa
# ~/projects/project_with_aws/.sherparc
alias rc_prod='ssh -i /path/key-pair-name.pem user@hostname "/var/app/current/bin/rails console"'
```

### Start your dev environment

```shell
# ~/projects/project_with_docker/.local-sherpa
# ~/projects/project_with_docker/.sherparc
alias up='docker-compose up --build -d'
alias down='docker-compose down'

# ~/projects/project_basic/.local-sherpa
# ~/projects/project_basic/.sherparc
alias up='bin/rails s'
```

Expand Down
1 change: 1 addition & 0 deletions lib/init.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export SHERPA_ENABLED=true
export SHERPA_LOG_LEVEL='info' # debug, info, no talking
export SHERPA_LOCAL_ENV_FILE='.sherparc'

if [ -n "$ZSH_VERSION" ]; then
SHERPA_LIB_PATH=$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )
Expand Down
6 changes: 3 additions & 3 deletions lib/local_env_file_parser.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ _fetch_variable_names() {
# Cleanup:
# export var_1 -> var_1
local variable_names
variable_names=$(grep -oE "$filter_pattern" .local-sherpa | awk '{print $2}')
variable_names=$(grep -oE "$filter_pattern" "$SHERPA_LOCAL_ENV_FILE" | awk '{print $2}')

if [ -n "$variable_names" ]; then
echo "$variable_names"
Expand All @@ -23,7 +23,7 @@ _fetch_aliase_names() {
# Cleanup:
# alias alias_1=... -> alias_1
local alias_names
alias_names=$(grep -E "$filter_pattern" .local-sherpa | awk -F'[ =]' '{print $2}')
alias_names=$(grep -E "$filter_pattern" "$SHERPA_LOCAL_ENV_FILE" | awk -F'[ =]' '{print $2}')

if [ -n "$alias_names" ]; then
echo "$alias_names"
Expand All @@ -37,7 +37,7 @@ _fetch_function_names() {
# function_1() -> function_1
# function function_2() -> function_2() -> function_2
local function_names
function_names=$(grep -oE "$filter_pattern" .local-sherpa | \
function_names=$(grep -oE "$filter_pattern" "$SHERPA_LOCAL_ENV_FILE" | \
sed 's/function //' | \
sed 's/()//')

Expand Down
8 changes: 4 additions & 4 deletions lib/sherpa.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Tell sherpa how much he should talk (works only for the current session):

edit() {
echo "hint: Waiting for your editor to close the file..."
eval "$EDITOR .local-sherpa"
eval "$EDITOR $SHERPA_LOCAL_ENV_FILE"
}

disable() {
Expand Down Expand Up @@ -107,7 +107,7 @@ load_current_env() {
log_debug "Load local env?"

# Skip if there is no local env file
[ -f .local-sherpa ] || { log_debug "No local env file"; return; }
[ -f "$SHERPA_LOCAL_ENV_FILE" ] || { log_debug "No local env file"; return; }

# Skip if the env was already loaded
was_env_loaded && { log_debug "Local env already loaded"; return; }
Expand All @@ -117,8 +117,8 @@ load_current_env() {

stash_local_env
log_debug "Load local env"
# shellcheck disable=SC1091
source .local-sherpa
# shellcheck disable=SC1090
source "$SHERPA_LOCAL_ENV_FILE"
# Append the current directory to the list. This is needed to unload the envs
# in the right order when we change directories. The root directory should be
# the last one to unload.
Expand Down
4 changes: 2 additions & 2 deletions lib/trust_verification.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

_calculate_checksum() {
sha256sum .local-sherpa | cut -d ' ' -f 1
sha256sum "$SHERPA_LOCAL_ENV_FILE" | cut -d ' ' -f 1
}

verify_trust() {
Expand Down Expand Up @@ -31,7 +31,7 @@ verify_trust() {
}

trust_current_env() {
if [[ ! -f .local-sherpa ]]; then
if [[ ! -f "$SHERPA_LOCAL_ENV_FILE" ]]; then
log_info "Nothing to trust. The current directory has no local env file. Run \`sherpa edit\` to create one."
return 1
fi
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/lib/local_env_file_parser_test.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source tests/support/init.sh

# Setup
cd fixtures/parsing
export SHERPA_LOCAL_ENV_FILE="fixtures/parsing/$SHERPA_LOCAL_ENV_FILE"


expected_list="var_1
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 8da6cbc

Please sign in to comment.