GithubHelp home page GithubHelp logo

whisperity / envprobe Goto Github PK

View Code? Open in Web Editor NEW
9.0 3.0 2.0 447 KB

Overlaying pluggable environment setter: modules reimagined + toggleable direnv on the shell, rather than the directory level.

Home Page: http://pypi.org/project/envprobe

License: GNU General Public License v3.0

Python 98.29% Shell 0.91% Makefile 0.79%
shell environment environment-variables module-loader configuration-management configuration-by-environment virtualenv settings-storage shell-environment bash-profile

envprobe's Introduction

Envprobe

Envprobe (envprobe) is a shell hook tool that helps you manage your environment variables on a per-shell basis easily. There is no need for clunky export sequences or manual source-ing of who-knows-where hand-written script files.

Envprobe was originally conceived as the tool between shell modules and direnv, but with added features such as saving your configuration on the fly.


Installation

Dependencies

Envprobe is supplied as a no-tools-needed Python package. The only things you need to have installed on the system are one of our supported POSIX-compliant shells and Python 3.

Supported shells are reasonably modern versions of Bash and Zsh.

Officially supported configurations

These are the configurations that the CI is running tests on:

Operating system Required dependencies Shells supported
Ubuntu 18.04 LTS (Bionic Beaver) Python ≥ 3.6 Bash (≥ 4.4), Zsh (≥ 5.4)
Ubuntu 20.04 LTS (Focal Fossa) Python ≥ 3.8 Bash (≥ 5.0), Zsh (≥ 5.8)

Download

You can download Envprobe's official releases from PyPI.

pip install envprobe

From GitHub

You can also download Envprobe from the GitHub repository, either via git or in a tarball form. Download the project anywhere you please, and extract the archive. In this example, we'll use ~/envprobe as the location.

git clone http://github.com/whisperity/envprobe.git ~/envprobe \
    --origin upstream --single-branch --branch master --depth 1

or

mkdir ~/envprobe
wget http://github.com/whisperity/envprobe/tarball/master -O envprobe.tar.gz
tar xzf envprobe.tar.gz --strip-components=1 -C ~/envprobe/

Hook

Envprobe applies the changes to your environment variable every time a prompt is generated in your shell. For this to work, a hook must be set up in each shell you're using.

The easiest way to do this is to add the hook script at the end of the configuration file for your respective shell.

⚠️ Note! If you are using other custom shell extensions, it is well-advised for the best experience to load Envprobe last.

Shell Configuration file (usually) Code to add
Bash ~/.bashrc eval "$(envprobe config hook bash $$)";
Zsh (stock) ~/.zshrc eval "$(envprobe config hook zsh $$)";
Zsh (Oh My Zsh) ~/.oh-my-zsh/custom/zzzzzz_envprobe.zsh (new file!) eval "$(envprobe config hook zsh $$)";

Quick user guide

If Envprobe is successfully installed and hooked, the ep/envprobe and epc/envprobe-config convenience functions will be defined in the current shell. If you see something like below, the tool is good to go.

$ ep
usage: envprobe [-h] ...

The following guide will show the usage of Envprobe through a few practical examples.

For a complete overview on the commands available and their usage, you can always call ep -h (or ep get -h, etc. for each subcommand) to get a quick help. The complete documentation for the user-facing commands is available behind the link.

Managing environment variables

For easy access, the environment variable managing commands are also offered as shortcuts.

Command Shortcut Usage
get VARIABLE ?VARIABLE, or simply VARIABLE Print the value of VARIABLE.
set VARIABLE VALUE !VARIABLE, VARIABLE=VALUE Sets VARIABLE to VALUE.
undefine VARIABLE ^VARIABLE Undefine VARIABLE.
add VARIABLE VALUE +VARIABLE VALUE (front), VARIABLE+ VALUE (back) Add a VALUE to an array.
remove VARIABLE VALUE -VARIABLE VALUE Remove VALUE from an array.
$ ep get USER
USER=root

$ ep USER
USER=root

$ ep PATH
PATH=/usr/local/bin:/usr/bin:/sbin:/bin

$ echo $SOME_VARIABLE
# No result, variable is not defined.
$ ep SOME_VARIABLE=MyValue
$ echo $SOME_VARIABLE
MyValue

$ ep ^SOME_VARIABLE
$ echo $SOME_VARIABLE
# No result.



$ fancy
fancy: command not found!

$ ep +PATH /opt/fancy/bin
$ fancy
Fancy tool works!

$ ep PATH
PATH=/opt/fancy/bin:/usr/local/bin:/usr/bin:/sbin:/bin

$ pwd
/root

$ ep -PATH /opt/fancy/bin
$ ep PATH+ .
$ ep PATH
PATH=/usr/local/bin:/usr/bin:/sbin:/bin:/root

$ ep +PATH ..
PATH=/:/usr/local/bin:/usr/bin:/sbin:/bin:/root

Saved snapshots

For easy access, some of the snapshot management commands are also offered as shortcuts.

Command Shortcut Usage
diff [var1 var2...] % [var1 var2...] Show the difference between the current environment and the last saved one (optionally only for the variables named).
save [--patch] SNAPSHOT [var1 var2...] }SNAPSHOT [-p] [var1 var2...] Save the changes (optionally of only the variables named) to the snapshot named SNAPSHOT. If -p is given, ask for confirmation of each individual change.
load [--patch] SNAPSHOT [var1 var2...] {SNAPSHOT [-p] [var1 var2...] Load the changes (optionally to only the variables names) from the snapshot named SNAPSHOT. If -p is given, ask for confirmation of each individual change.
list List the saved snapshots' names.
delete SNAPSHOT Delete the snapshot named SNAPSHOT.
$ ep %
# (No output, initially the environment hasn't been changed yet.)

$ ep PATH
PATH=/usr/local/bin:/usr/bin/:/sbin:/bin

$ ep SOME_VARIABLE=foo
$ ep +PATH /tmp
$ ep -PATH /sbin
$ ep PATH+ /home/user/bin

$ ep %
(+) Added:       SOME_VARIABLE
        defined value: foo

(!) Changed:     PATH
        added:         /tmp
        added:         /home/user/bin
        removed:       /sbin

$ ep } mypath PATH
For variable 'PATH' the element '/tmp' was added.
For variable 'PATH' the element '/home/user/bin' was added.
For variable 'PATH' the element '/sbin' was removed.

$ ep } other_vars -p
New variable 'SOME_VARIABLE' with value 'foo'.
Save this change? (y/N) _



$ ep list
mypath
other_vars

$ ep delete mypath
$ ep list
other_var



$ ep load custompaths
For variable 'PATH' the element '/srv/custom/bin' will be added.

$ ep PATH
PATH=/srv/custom/bin:/tmp:/home/user/bin

$ ep { foobar -n
New variable 'FOO' will be created with value 'bar'.

$ ep FOO
FOO is not defined

$ ep { foobar -p
New variable 'FOO' will be created with value 'bar'.
Load and apply this change? (y/N) _

Saved snapshots

Envprobe offers type safety when accessing environment variables, such as prohibiting assigning a textual value to a _PID variable. The type of a variable is decided based on several heuristics by default, and can be configured by the user.

$ echo $SSH_AGENT_PID
12345

$ export SSH_AGENT_PID="invalid-value"
# The above example works, even though a "_PID" variable should only
# contain numbers.

$ ep SSH_AGENT_PID=98765
$ ep SSH_AGENT_PID="foo"
[ERROR] Failed to execute: could not convert string to number.

$ ep SSH_AGENT_PID
SSH_AGENT_PID=98765

Configuring variable handling

The envprobe-config (or epc) command contains various subcommands that can be used to configure Envprobe's behaviour.

For a complete overview on the commands available and their usage, you can always call epc -h (or epc track -h, etc. for each subcommand) to get a quick help. The complete documentation for the user-facing commands is available behind the link.

Command Usage
hook ... Generate the Shell hook that allows Envprobe to interface with the environment.
set VARIABLE ... Set additional information, such as a description, for a variable.
track VARIABLE Configure whether changes to a VARIABLE should be managed in saved snapshots.
descriptions update Download the variable descriptions to use locally.

Obtaining variable type descriptions

Envprobe is managed with a sister project, the Envprobe Variable Descriptions Knowledge Base which contains a curated list of environment variable descriptions and types. This database is used together with other heuristics to provide type-safe access to variables. The description of a variable, when queried, may come from a local copy of this database too. Read more about this feature.

To initially download, or subsequently update, the description database, execute:

epc descriptions update

envprobe's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

envprobe's Issues

The "ignored" variable type

The original version had the ability for users to epc set-type VAR ignored which made Envprobe throw a fatal exception if the user wanted to access the variable in any way.

Related documentation code from the previous version:

    epilogue += "\nSetting the type to 'ignored' will make Envprobe "       \
                "prohibit access of the variable. (Note: this is not the "  \
                "same as \"track ignoring\" the variable. The latter only " \
                "disables the variable in the save/load command, the "      \
                "'ignored' type disables the get/set too!)"

[bash] Setting string variable separated by spaces results in error

$ set -x
$ ep DISTCC_PORTS="15166/12 15167/20"
[...]
+++ cat /tmp/.envprobe.00000-aaaaaaaa/control.sh
++ eval export DISTCC_PORTS=15166/12 '15167/20;'
+++ export DISTCC_PORTS=15166/12 15167/20
+++ DISTCC_PORTS=15166/12
bash: export: `15167/20': not a valid identifier

Allow unloading a loaded save

This feature will require generating and storing the inverse action of a load to produce an unload operetion which unsets defined variables, rolls back their original state (only if not subsequently modified by the user?), removes added Pathlike elements, etc.

[checklist] Feature migration to V1

The full list of Envprobe's features that exist and should either be migrated to the new version or cut out. All migrated features should be tested #8!

  • Shell hooking, control generation
    • Bash implementation
    • Zsh implementation
    • (epc) shell command
    • Cleanup of files for exiting shells (#11)
  • vartypes - environment variable mapping to Pythonic types
    • Built-in heuristics for mapping a variable to such a type
  • Understanding of environment and difference
    • Per-shell persistency
    • Environment loading
    • Difference between current and persisted
  • Environment management commands:
    • get
    • set
    • add
    • remove
    • undefine
    • Proper shortcut handling for these
  • Configuration file handling
  • Variable tracking
    • Tracking configuration, two-level tracking overlay
    • (epc) track, (epc) default-tracking commands to control this configuration
  • Saved configurations
    • Difference persistency (properly? #2)
    • Respecting the tracking settings
    • diff
    • save
    • load
    • list, delete
  • User-configured vartypes heuristics settings: (epc) set-type
  • Community description features
    • Setting description locally: (epc) set-description
    • (epc) update-community
    • Respecting the downloaded community descriptions when resolving variable type.

Create tests

It looks stupid that there are literally zero tests...

Support marking variables as read-only

  • get should be supported, but modifying operations should not
  • They should not show up in "diff", "save", "load", etc.
  • (Type and description setting should be allowed!)

Variables such as PWD, OLDPWD are like this.

Support Zsh

Zsh is a popular shell which is fairly Bash-like. The support for Zsh seems as straightforward as implementing the specific hook.
For initial implementation see #6.

PATH-like variable modifications overwrite instead of union in saves

Reproduce:

  1. ep +PATH foo
  2. ep save foo
  3. ep +PATH bar
  4. ep save foo
  5. cat ~/.local/share/envprobe/saves/foo.json

Results in the following save:

{
  "unset": [],
  "variables": {
    "PATH": {
      "add": [
        "/tmp/bar"
      ],
      "remove": []
    }
  }

The addition of directory foo to the variable is missing.

Automatically generate submissions for the community descriptions project

If someone wants to submit their personal variable type and description to the description project, make it easily doable without having to edit CSV files.

  1. Ask which types and descriptions to submit. Save the information about what has been "packaged" and what was "ignored".
  2. Create the patch.
  3. Suggest methods to submitting the patch, guide the user through forking, uploading the new version, and submitting a PR.
  4. Do the previous point automatically through API calls and whatnot.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.