/env-run

Setup environment variables for development on Linux

Primary LanguageShell

env-run

A lightweight environment management utility for running commands with environment-specific configurations.

Overview

env-run allows you to manage multiple environment configurations (dev, uat, prod) and execute commands with the appropriate environment variables loaded. It supports layered configuration with base settings and environment-specific overrides.

Features

  • Multi-environment support: Separate configurations for dev, uat, and prod
  • Layered configuration: Base settings with environment-specific overrides
  • Secret management: Integration with Linux secret-tool for secure credential storage
  • Two usage modes: Command execution and environment sourcing
  • XDG compliance: Uses $XDG_CONFIG_HOME or falls back to ~/.config

Installation

  1. Copy env-run to your ~/bin directory or anywhere in your $PATH
  2. Make it executable: chmod +x env-run
  3. Run the setup script to create initial environment files: ./setup-envs-template.sh

Usage

Command Execution

Run commands with environment-specific variables:

env-run dev npm start
env-run prod ./deploy.sh
env-run uat python manage.py migrate

Environment Sourcing

Load environment variables into your current shell:

source <(env-run dev --export)
source <(env-run prod --export)

Configuration

Environment files are stored in ~/.config/env/:

  • base.env - Variables shared across all environments
  • dev.env - Development-specific variables
  • uat.env - UAT/staging-specific variables
  • prod.env - Production-specific variables

Example Configuration

base.env:

API_BASE_URL=https://api.example.com
LOG_LEVEL=info

dev.env:

ASPNETCORE_ENVIRONMENT=Development
API_BASE_URL=https://dev-api.example.com
LOG_LEVEL=debug

Secret Management

The utility integrates with Linux's secret-tool for secure credential storage:

Store a secret:

secret-tool store --label='API Key Dev' service env-vars key API_KEY_DEV

Use in environment files:

API_KEY=$(secret-tool lookup service env-vars key API_KEY_DEV 2>/dev/null || echo "fallback-value")

List stored secrets:

secret-tool search service env-vars

Testing

Run the test suites to verify functionality:

Test the main env-run utility:

./test-env-run.sh

Test the setup script:

./test-setup-envs.sh

The test scripts verify:

  • env-run tests: Error handling, environment loading, variable overrides, export functionality, command execution
  • setup-envs tests: Directory creation, file generation, content validation, XDG compliance, secret-tool detection

Requirements

  • Bash shell
  • secret-tool (optional, for secret management)
    • Install on Ubuntu/Debian: sudo apt install libsecret-tools

License

This project is provided as-is for personal and commercial use.