GithubHelp home page GithubHelp logo

decryptus / json-dotenv Goto Github PK

View Code? Open in Web Editor NEW
8.0 2.0 3.0 62 KB

Manipulate and extract envfiles in json format.

License: GNU General Public License v3.0

Python 82.15% Makefile 13.49% Jinja 4.37%
json dotenv python cli envfile environment-variables

json-dotenv's Introduction

json-dotenv project

PyPI pyversions PyPI version shields.io Documentation Status

json-dotenv is a free and open-source, we develop it to manipulate and extract envfiles in json format.

Table of contents

  1. Installation
  2. Usage
  3. Commands

Installation

pip install json-dotenv

Usage

usage: json-dotenv [-h] [--allow-envvar] [-c {list,keys,get,set,unset}]
                   [-k KEY] [-v VALUE] [-f FILE] [--force]
                   [-l {critical,error,warning,info,debug}]
                   [--logfile LOGFILE] [-o OUTPUT] [-q {always,never,auto}]
                   [--format {env,json}]
                   [{list,keys,get,set,unset}]

positional arguments:
  {list,keys,get,set,unset}
                        Commands: list, keys, get, set, unset

optional arguments:
  -h, --help            show this help message and exit
  --allow-envvar        Allow environment variables expansion, instead of
                        False
  -c {list,keys,get,set,unset}
                        Commands: list, keys, get, set, unset, instead of list
                        (deprecated)
  -k KEY, --key KEY     variable name to set or unset
  -v VALUE, --value VALUE
                        variable value to set
  -f FILE               Location of the environment file or from stdin (-),
                        instead of .env
  --force               Force the output even if there is an error
  -l {critical,error,warning,info,debug}, --loglevel {critical,error,warning,info,debug}
                        Emit traces with LOGLEVEL details, must be one of:
                        critical, error, warning, info, debug
  --logfile LOGFILE     Use log file <logfile> instead of /var/log/json-dotenv
                        /json-dotenv.log
  -o OUTPUT             Output result in file or to stdout
  -q {always,never,auto}
                        Whether to quote or not the variable values, instead
                        of always. This does not affect parsing
  --format {env,json}   Output format env or json, instead of json

Commands

List all environment variables in file foo.env:

json-dotenv list -f foo.env

{
  "LANG": "en_US.utf8",
  "PATH": "/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games",
  "MONIT_DOCKER_CONFIG": "vars:\n  base_url_unix: unix:///var/run/docker.sock\n  base_url_https: https://127.0.0.1:2376/\n  tls_verify: true\nclients:\n  '@import_client':\n    - clients.yml.example\n  local_https:\n    config:\n      base_url: ${vars['base_url_https']}\n      tls:\n        verify: ${vars['tls_verify']}\n  foo_https:\n    '@import_vars': foo_https.vars.yml.example\n    config:\n      base_url: ${vars['base_url_https']}\nctn-groups:\n  php:\n    match:\n      - 'name:foo-php*'\n      - 'image:*/php-fpm/*'\n      - 'label:*php-fpm*'\n  nodejs:\n    match:\n      - 'id:4c01db0b339c'\n      - 'name:node*'\nconditions:\n  mem_gt_10pct_and_cpu_gt_60pct:\n    expr:\n      - mem_percent > 10\n      - cpu_percent > 60\n  mem_usage_100MiB:\n    expr:\n      - mem_usage > 100 MiB\n  status_not_running:\n    expr:\n      - status not in (pause,running)\ncommands:\n  start_pause:\n    exec:\n      - start\n      - (echo 'foo' > /tmp/bar)\n      - pause\n  pause_restart:\n    exec:\n      - pause\n      - restart\n  remove_force:\n    exec:\n      - remove:\n          kwargs:\n            force: true",
  "SHELL": "/bin/bash",
  "AUTON_CONFIG": "general:\n  listen_addr:   0.0.0.0\n  listen_port:   8666\n  max_workers:   5\n  max_requests:  5000\n  max_life_time: 3600\n  lock_timeout:  60\n  charset:       utf-8\n  content_type:  'application/json; charset=utf-8'\n  #auth_basic:      'Restricted'\n  #auth_basic_file: '/etc/auton/auton.passwd'\nendpoints:\n  si.corp-ansible:\n    plugin: subproc\n    config:\n      prog: ansible-playbook\n      timeout: 3600\n  si.corp-terraform:\n    plugin: subproc\n    config:\n      prog: terraform\n      timeout: 3600\n  curl:\n    plugin: subproc\n    config:\n      prog: curl\n      timeout: 3600\nmodules:\n  job:\n    routes:\n      run:\n        handler:   'job_run'\n        regexp:    '^run/(?P<endpoint>[^\\/]+)/(?P<id>[a-z0-9][a-z0-9\\-]{7,63})$'\n        safe_init: true\n        auth:      false\n        op:        'POST'\n      status:\n        handler:   'job_status'\n        regexp:    '^status/(?P<endpoint>[^\\/]+)/(?P<id>[a-z0-9][a-z0-9\\-]{7,63})$'\n        auth:      false\n        op:        'GET'"
}

List all environment variables name in file foo.env:

json-dotenv keys -f foo.env

[
  "LANG",
  "PATH",
  "MONIT_DOCKER_CONFIG",
  "SHELL",
  "AUTON_CONFIG"
]

Get foo.env contents from stdin and set variables AUTON_CONFIG=bar and toto=titi:

cat foo.env | json-dotenv set -f - -k AUTON_CONFIG -v bar -k toto -v titi

{
  "LANG": "en_US.utf8",
  "PATH": "/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games",
  "MONIT_DOCKER_CONFIG": "vars:\n  base_url_unix: unix:///var/run/docker.sock\n  base_url_https: https://127.0.0.1:2376/\n  tls_verify: true\nclients:\n  '@import_client':\n    - clients.yml.example\n  local_https:\n    config:\n      base_url: ${vars['base_url_https']}\n      tls:\n        verify: ${vars['tls_verify']}\n  foo_https:\n    '@import_vars': foo_https.vars.yml.example\n    config:\n      base_url: ${vars['base_url_https']}\nctn-groups:\n  php:\n    match:\n      - 'name:foo-php*'\n      - 'image:*/php-fpm/*'\n      - 'label:*php-fpm*'\n  nodejs:\n    match:\n      - 'id:4c01db0b339c'\n      - 'name:node*'\nconditions:\n  mem_gt_10pct_and_cpu_gt_60pct:\n    expr:\n      - mem_percent > 10\n      - cpu_percent > 60\n  mem_usage_100MiB:\n    expr:\n      - mem_usage > 100 MiB\n  status_not_running:\n    expr:\n      - status not in (pause,running)\ncommands:\n  start_pause:\n    exec:\n      - start\n      - (echo 'foo' > /tmp/bar)\n      - pause\n  pause_restart:\n    exec:\n      - pause\n      - restart\n  remove_force:\n    exec:\n      - remove:\n          kwargs:\n            force: true",
  "SHELL": "/bin/bash",
  "AUTON_CONFIG": "bar",
  "toto": "titi"
}

Get variables LANG and PATH from foo.env:

json-dotenv get -f foo.env -k LANG -k PATH

{
  "LANG": "en_US.utf8",
  "PATH": "/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
}

Unset variables MONIT_DOCKER_CONFIG and AUTON_CONFIG from file foo.env (file not modified):

json-dotenv unset -f foo.env -k MONIT_DOCKER_CONFIG -k AUTON_CONFIG

{
  "LANG": "en_US.utf8",
  "PATH": "/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games",
  "SHELL": "/bin/bash"
}

Set variables TOTO and BAR and output result in file bar.json:

json-dotenv set -f '' -k TOTO -v tutu -k BAR -v foo -o bar.json

Set variables TOTO and BAR and output result in file bar.env (environment variables format):

json-dotenv set -f '' -k TOTO -v tutu -k BAR -v foo --format env -o bar.env

json-dotenv's People

Contributors

decryptus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

json-dotenv's Issues

Get error path should be string not StringIO

I use on Windows and get the error when run it

Command & error:

json-dotenv list -f private/.env

ERROR:2023-04-21 20:45:04: stat: path should be string, bytes, os.PathLike or integer, not StringIO
Traceback (most recent call last):
  File "venv\Scripts\json-dotenv.py", line 462, in main
    getattr(JsonDotEnv(options), "do_%s" % options.command)()
  File "venv\Scripts\json-dotenv.py", line 337, in do_list
    dotenv.dotenv_values(self.fcontent)))
  File "venv\lib\site-packages\dotenv\main.py", line 375, in dotenv_values
    return DotEnv(
  File "venv\lib\site-packages\dotenv\main.py", line 75, in dict
    self._dict = OrderedDict(resolve_variables(raw_values, override=self.override))
  File "venv\lib\site-packages\dotenv\main.py", line 233, in resolve_variables
    for (name, value) in values:
  File "venv\lib\site-packages\dotenv\main.py", line 82, in parse
    with self._get_stream() as stream:
  File "C:\Program Files\Python39\lib\contextlib.py", line 117, in __enter__
    return next(self.gen)
  File "venv\lib\site-packages\dotenv\main.py", line 54, in _get_stream
    if self.dotenv_path and os.path.isfile(self.dotenv_path):
  File "C:\Program Files\Python39\lib\genericpath.py", line 30, in isfile
    st = os.stat(path)
TypeError: stat: path should be string, bytes, os.PathLike or integer, not StringIO

I could fix the problem by adding stream argument to dotenv.dotenv_values execution like I read in a doc:

load_dotenv and dotenv_values accept [streams](https://docs.python.org/3/library/io.html) via their stream argument. It is thus possible to load the variables from sources other than the filesystem (e.g. the network).

from io import StringIO

from dotenv import load_dotenv

config = StringIO("USER=foo\[email protected]")
load_dotenv(stream=config)

Windows 10
Python 3.9.2
json-dotenv 0.0.28
python-dotenv 1.0.0

Can't install with pip 19.0.3

$ pip install json-dotenv
Collecting json-dotenv
  Could not find a version that satisfies the requirement json-dotenv (from versions: )
No matching distribution found for json-dotenv

$ pip --version 
pip 19.0.3 from /usr/lib/python3.7/site-packages/pip (python 3.7)

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.