home-dotenv 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/home-dotenv.rb +31 -0
- metadata +86 -0
    
        checksums.yaml
    ADDED
    
    | @@ -0,0 +1,7 @@ | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            SHA1:
         | 
| 3 | 
            +
              metadata.gz: 310537c2f78ebc086bc9a2a33d0ddf96ebdd354a
         | 
| 4 | 
            +
              data.tar.gz: fbe0ad63c194157227fc1edcacf73ddb7db15c16
         | 
| 5 | 
            +
            SHA512:
         | 
| 6 | 
            +
              metadata.gz: f3a278a4bb9904d5398cec836ad421dab21d3af5cab58a909d82328c78b561d08721adfec06f1e712ca15921be24b3ccc865552f4d89bcc3bec82cba8728333b
         | 
| 7 | 
            +
              data.tar.gz: b7d716ac81545153088bbb195514b49fab8a722cbeed576e5b904cb103527262b4a7164a6d5fcb0464a7d098916fab1e22d87a7c98e603e13e0f65a5136ba761
         | 
    
        data/lib/home-dotenv.rb
    ADDED
    
    | @@ -0,0 +1,31 @@ | |
| 1 | 
            +
            # USAGE: HomeDotenv.parse
         | 
| 2 | 
            +
            class HomeDotenv
         | 
| 3 | 
            +
              # With a ~/.env
         | 
| 4 | 
            +
              #
         | 
| 5 | 
            +
              #   SECRET_KEY=so-secret-really
         | 
| 6 | 
            +
              #
         | 
| 7 | 
            +
              # HOME_ENV = HomeDotenv.parse
         | 
| 8 | 
            +
              # puts HOME_ENV[:SECRET_KEY]
         | 
| 9 | 
            +
              def self.parse(filename = nil)
         | 
| 10 | 
            +
                Hash[*read_lines(filename).map { |l| parse_line(l) }.flatten]
         | 
| 11 | 
            +
              end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
              class << self
         | 
| 14 | 
            +
                private
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                def read_lines(filename = nil)
         | 
| 17 | 
            +
                  filename ||= Dir.home + '/.env'
         | 
| 18 | 
            +
                  lines = File.read(filename).split("\n")
         | 
| 19 | 
            +
                  lines.reject!(&:empty?)
         | 
| 20 | 
            +
                  lines.reject! { |l| l[0] == '#' }
         | 
| 21 | 
            +
                  lines.reject! { |l| !l.include?('=') || l.split('=').first.to_s == '' }
         | 
| 22 | 
            +
                  lines
         | 
| 23 | 
            +
                end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                def parse_line(line)
         | 
| 26 | 
            +
                  key = line.split('=').first.to_sym
         | 
| 27 | 
            +
                  value = line.split('=')[1..-1].join('=')
         | 
| 28 | 
            +
                  [key, value]
         | 
| 29 | 
            +
                end
         | 
| 30 | 
            +
              end
         | 
| 31 | 
            +
            end
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,86 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: home-dotenv
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.0.1
         | 
| 5 | 
            +
            platform: ruby
         | 
| 6 | 
            +
            authors:
         | 
| 7 | 
            +
            - Dorian Marié
         | 
| 8 | 
            +
            autorequire: 
         | 
| 9 | 
            +
            bindir: bin
         | 
| 10 | 
            +
            cert_chain: []
         | 
| 11 | 
            +
            date: 2017-02-25 00:00:00.000000000 Z
         | 
| 12 | 
            +
            dependencies: []
         | 
| 13 | 
            +
            description: |
         | 
| 14 | 
            +
              # `home-dotenv`
         | 
| 15 | 
            +
             | 
| 16 | 
            +
              ## Instalation
         | 
| 17 | 
            +
             | 
| 18 | 
            +
              ``ruby
         | 
| 19 | 
            +
              gem install home-dotenv
         | 
| 20 | 
            +
             | 
| 21 | 
            +
              # or in a Gemfile
         | 
| 22 | 
            +
              gem 'home-dotenv'
         | 
| 23 | 
            +
              ```
         | 
| 24 | 
            +
             | 
| 25 | 
            +
              ## Explanation
         | 
| 26 | 
            +
             | 
| 27 | 
            +
              So, you have scripts run on your system and you have config variables for those
         | 
| 28 | 
            +
              scripts (keys, usernames, passwords, hostnames, etc).
         | 
| 29 | 
            +
             | 
| 30 | 
            +
              A unified way to access those globals would be nice instead of hard-coding them
         | 
| 31 | 
            +
              or passing them as parameters.
         | 
| 32 | 
            +
             | 
| 33 | 
            +
              So, now you can put all your variables in ~/.env like so:
         | 
| 34 | 
            +
             | 
| 35 | 
            +
              ```
         | 
| 36 | 
            +
              SUPER_SECRET_PASSWORD=i-definitely-don-t-want-people-to-see-this
         | 
| 37 | 
            +
              AWS_KEY=abcdefgh
         | 
| 38 | 
            +
              SECRET_HOST=https://private.secret.com
         | 
| 39 | 
            +
              ```
         | 
| 40 | 
            +
             | 
| 41 | 
            +
              Then, in your scripts:
         | 
| 42 | 
            +
             | 
| 43 | 
            +
              ```
         | 
| 44 | 
            +
              require 'home-dotenv'
         | 
| 45 | 
            +
              HOME_ENV = HomeDotenv.parse
         | 
| 46 | 
            +
             | 
| 47 | 
            +
              require 'open-uri'
         | 
| 48 | 
            +
              open(HOME_HOME[:SECRET_HOST]).read
         | 
| 49 | 
            +
              ```
         | 
| 50 | 
            +
             | 
| 51 | 
            +
              Parsing rules:
         | 
| 52 | 
            +
             | 
| 53 | 
            +
              - if the line is empty or starts by "#" (comment), ignore
         | 
| 54 | 
            +
              - otherwise, string before the first "=" is key, after that it's the value
         | 
| 55 | 
            +
              - result is a hash indexed by symbol, e.g. HOME_ENV[:AWS_KEY]
         | 
| 56 | 
            +
            email: dorian@doma.io
         | 
| 57 | 
            +
            executables: []
         | 
| 58 | 
            +
            extensions: []
         | 
| 59 | 
            +
            extra_rdoc_files: []
         | 
| 60 | 
            +
            files:
         | 
| 61 | 
            +
            - lib/home-dotenv.rb
         | 
| 62 | 
            +
            homepage: https://github.com/Dorian/home-dotenv
         | 
| 63 | 
            +
            licenses:
         | 
| 64 | 
            +
            - MIT
         | 
| 65 | 
            +
            metadata: {}
         | 
| 66 | 
            +
            post_install_message: 
         | 
| 67 | 
            +
            rdoc_options: []
         | 
| 68 | 
            +
            require_paths:
         | 
| 69 | 
            +
            - lib
         | 
| 70 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 71 | 
            +
              requirements:
         | 
| 72 | 
            +
              - - ">="
         | 
| 73 | 
            +
                - !ruby/object:Gem::Version
         | 
| 74 | 
            +
                  version: '0'
         | 
| 75 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 76 | 
            +
              requirements:
         | 
| 77 | 
            +
              - - ">="
         | 
| 78 | 
            +
                - !ruby/object:Gem::Version
         | 
| 79 | 
            +
                  version: '0'
         | 
| 80 | 
            +
            requirements: []
         | 
| 81 | 
            +
            rubyforge_project: 
         | 
| 82 | 
            +
            rubygems_version: 2.6.8
         | 
| 83 | 
            +
            signing_key: 
         | 
| 84 | 
            +
            specification_version: 4
         | 
| 85 | 
            +
            summary: Load ~/.env variables
         | 
| 86 | 
            +
            test_files: []
         |