Coder Social home page Coder Social logo

envparser's Introduction

tflori/envparser

.github/workflows/push.yml Test Coverage Maintainability Latest Stable Version Total Downloads License

A small library that is just reading an environment file in bash syntax and returns an array equal to that what you would get sourcing this file in a bash environment.

Features

Declarations of variables

# a string without spaces
STRING1=foo

# a string with spaces surrounded by quotes
STRING2="foo's bar"
STRING3='"$quote" - $author'      # string '"$quote" - $author'
STRING4=foo"' "bar                # string 'foo\' bar'

# variable replacement (assuming user=thomas)
VAR1=$user                        # string 'thomas'
VAR2="$user's profile"            # string 'thomas\'s profile'
VAR3="${APP_ENV-production}"      # string 'production'

# array handling
ARRAY1=(foo bar)                  # array ['foo', 'bar']
ARRAY2=($int 6 7)                 # array [42, 6, 7]
ARRAY3=()                         # array []
ARRAY5[0]=foo                     # array ['foo']
ARRAY5[1]=bar                     # array ['foo', 'bar']

A lot of things available in bash are not possible in this library but the important ones will be. When you find something is missing please open a feature request on github or create a merge request.

Using Environment

All variables available in $_ENV and getenv() (or a passed array) are available inside the parsed file.

Conversion of variables

In bash every variable is a string like in *nix every variable is a file and inside is a string. While it is very useful on this level we don't want that inside an application.

### null (case insensitive)
NULL1=                            # null (bash typical)
NULL2=null                        # null
NULL3="null"                      # null
STRING1='null'                    # string 'null'

### numbers (checked with is_numeric)
INT=42                            # int 42
INT2="23"                         # int 23
STRING2='42'                      # string '42'
FLOAT=23.2                        # float 23.2
FLOAT2="42.1"                     # float 42.1
STRING3='23.2'                    # string '23.2'

### true (case insensitive)
BOOL_TRUE=true                    # bool true
STRING4='true'                    # string 'true'

### ATTENTION! In php: ('false' != false)
BOOL_FALSE=false                  # bool false
STRING5='false'                   # string 'false'

### json (you should avoid json in environment files. it is just sugar; ext-json required)
OBJECT='json:{"foo":"bar"}'       # \stdobj ['foo' => 'bar']
ARRAY='jsonArray:{"foo":"bar"}'   # array ['foo' => 'bar']

### base64 (use it for unprintable characters)
APP_KEY="base64:dzN0ICJzZWNyZXQiCg=="

Concatenation

Strings are concatenated in bash by just connecting the strings together.

STRING1="hello"' $user'           # string 'hello $user'

### everything is a string when it gets concatenated
STRING2=True" Warrior"            # string 'True Warrior'

Escaping

Escaping quotes works exactly the same as in bash:

STRING1="foo's \"bar\""
STRING2='foo'"'"'s "bar"'
STRING3='foo'\''s "bar"'
STRING4=$'foo\'s "bar"'

envparser's People

Contributors

tflori avatar

Watchers

 avatar  avatar

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.