Class: Envfile

Inherits:
Hash
  • Object
show all
Defined in:
lib/envfile.rb

Overview

Envfile understands various input file formats and executes commands according to that.

It should be noted that Envfile is (unlike the ENV object itself) a kind of Hash. So if you want to modify some environment variables, feel free to do so as you normally do with Hashes. It is of course up to you to properly set them up.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run_with_envfile(envfile, cmd, *argv) ⇒ Object

Utility method to setup, fire, then forget.

Parameters:

  • envfile (String)

    an envfile path

  • cmd (String)

    program file path

  • argv (String)

    passed to cmd

See Also:



43
44
45
# File 'lib/envfile.rb', line 43

def self.run_with_envfile envfile, cmd, *argv
   new.parse(envfile).exec!(cmd, argv)
end

Instance Method Details

#exec!(cmd, argv) ⇒ Object

Note:

This method does not always return. But when it does it always results into an exception.

Executes the given cmd under self’s setup.

Parameters:

  • cmd (String)

    program file path

  • argv (String)

    passed to cmd

Raises:

  • (Errno::ENOENT)

    envfile does not exist

  • (Errno::ENOEXEC)

    cmd not runnable

  • (Errno::EACCESS)

    permission denied to exec

  • (Errno::ELOOP)

    symlink is too deep

  • (Errno::ENAMETOOLONG)

    file path too long



75
76
77
# File 'lib/envfile.rb', line 75

def exec! cmd, argv
   exec self, [cmd, cmd], *argv, close_others: true
end

#parse(envfile) ⇒ Object

Note:

When called multiple times with different envfile per instance, and if any of the env key collides, the result is undefined. I would like to advise you no to do such thing.

Load up the contents of the given file.

Parameters:

  • envfile (String)

    path to a file



53
54
55
56
57
58
59
60
61
# File 'lib/envfile.rb', line 53

def parse envfile
   path = Pathname.new envfile
   update case path.extname
          when '.pl',  '.perl' then parse_perl    path
          when '.js',  '.json' then parse_json    path
          when '.yml', '.yaml' then parse_yaml    path
          else                      parse_xaicron path
          end
end