"""Launch a command with consistent proxy variables, without printing their values."""
import os
import sys
from qg_client import connection, ConfigurationError

def environment():
    env = dict(os.environ)
    proxy = connection()
    # A deliberate bypass list. Protected destinations must not match it.
    bypass = env.get('QG_NO_PROXY', 'localhost,127.0.0.1,::1')
    for name in ('HTTP_PROXY', 'HTTPS_PROXY', 'ALL_PROXY',
                 'http_proxy', 'https_proxy', 'all_proxy'):
        env[name] = proxy
    env['NO_PROXY'] = env['no_proxy'] = bypass
    return env

if __name__ == '__main__':
    if len(sys.argv) < 2:
        raise SystemExit('Usage: python launch-hermes.py COMMAND [ARG ...]')
    try:
        env = environment()
    except ConfigurationError:
        raise SystemExit('Proxy configuration missing or invalid') from None
    os.execvpe(sys.argv[1], sys.argv[1:], env)
