DEV Community

Kazuki Okamoto
Kazuki Okamoto

Posted on

Envar — Centrally manage environment variables to switch per directory

I've created a command-line tool called Envar.

GitHub logo kakkun61 / envar

This is a command-line tool that automatically switches values of environment variables based on the current directory path.

envar

This is a command-line tool that automatically switches values of environment variables based on the current directory path.

  1. Place the envar binary
  2. Install the shell hook
  3. Write the configuration file

Place the envar binary

Run go build and place the binary file in your PATH.

Install the shell hook

Add this line to your .bashrc:

eval "$(envar hook)"
Enter fullscreen mode Exit fullscreen mode

and add this line to your .bash_logout:

envar hook logout $$
Enter fullscreen mode Exit fullscreen mode

Write the configuration file

The configuration file uses YAML. It is located at $CONFIG_DIR/envar/vars.yaml and $CONFIG_DIR/envar/execs.yaml. $CONFIG_DIR is the value returned by os.UserConfigDir().

vars.yaml is used to define environment variable values. For example:

FOO_VAR:
  path/to/dir: foo-value-1
  other/path: foo-value-2
  other/path/never: foo-value-3
  # This is a comment line
  ~/projects: foo-value-4
  "spacial dir/path": foo-value-5
BAR_VAR:
  another/dir: bar-value-1
Enter fullscreen mode Exit fullscreen mode

The directory path/to/dir

This is a tool that allows switching environment variable values on a per-directory basis.

For example, you can configure an environment variable named FOO to return the value hoge when in the directory path/to/A, and fuga when in the directory path/to/B. This configuration is specified in a vars.yaml file like this:

FOO:
  path/to/A: hoge
  path/to/B: fuga
Enter fullscreen mode Exit fullscreen mode

You can also configure environment variables to derive their values from the output of other commands.

This example is excerpted from my own configuration files and demonstrates switching between personal and work accounts when using the gh command.

# vars.yaml
GH_TOKEN:
  /home/kazuki/Projects/Work:
    gh: kakkun61_work
  /home/kazuki/Projects:
    gh: kakkun61
Enter fullscreen mode Exit fullscreen mode
# execs.yaml
gh: gh auth token --user %s
Enter fullscreen mode Exit fullscreen mode

Frequently Asked Questions Maybe

What's the difference between this and direnv?

Direnv works by writing configuration files containing environment variables you want to set in that directory, primarily for environment variables needed by all users of a project. These are project-specific environment variables.

In contrast, envar stores configuration files in the user-specific settings directory (e.g., ~/.config/envar). It's designed for setting environment variables that serve personal needs, such as account switching in the above example.

Which shell versions are supported?

Currently, only bash is supported. If there's demand, I may add support for zsh as well.


Mainly translated by Plamo Translation.

Top comments (0)