Class: Annex::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/annex/environment.rb

Overview

Represents an Annex environment. The basic annex environment contains a config/settings.yml file defining the server cluster.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = nil) ⇒ Environment

Initializes a new environment with the given options. The options is a hash where the main available key is ‘cwd`, which defines the location of the environment. If `cwd` is nil, then it defaults to the `Dir.pwd` (which is the cwd of the executing process).

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/annex/environment.rb', line 16

def initialize(opts=nil)
  opts = {
    :cwd => nil,
    :windows => false,
    :supports_colors => true,
    :no_colors => false
  }.merge(opts || {})

  # Set the default working directory
  opts[:cwd] ||= ENV["ANNEX_CWD"] if ENV.has_key?("ANNEX_CWD")
  opts[:cwd] ||= Dir.pwd
  opts[:cwd] = Pathname.new(opts[:cwd])
  raise Errors::AnnexError.new("Unknown current working directory") if !opts[:cwd].directory?

  # Set instance variables for all the configuration parameters.
  @cwd = opts[:cwd]
  @colorize = opts[:supports_colors] || !opts[:no_colors]
end

Instance Attribute Details

#cwdObject (readonly)

The ‘cwd` that this environment represents



10
11
12
# File 'lib/annex/environment.rb', line 10

def cwd
  @cwd
end

Instance Method Details

#cli(*args) ⇒ Object

Makes a call to the CLI with the given arguments as if they came from the real command line (sometimes they do!). An example:

env.cli("provision", "--role", "app", "--environment", "staging")


48
49
50
# File 'lib/annex/environment.rb', line 48

def cli(*args)
  CLI.new(args.flatten, self).execute
end

#confighash

The configuration object represented by this environment. This will trigger the environment to load if it hasn’t loaded yet.

Returns:

  • (hash)


56
57
58
# File 'lib/annex/environment.rb', line 56

def config
  @config ||= YAML::load_file(File.join(@cwd, "config", "settings.yml"))
end

#error(message) ⇒ Object

Output a message, formatted if we support colors



66
67
68
# File 'lib/annex/environment.rb', line 66

def error(message)
  $stderr.puts colorize(message, :error)
end

#info(message, level = :default) ⇒ Object

Output a message, formatted if we support colors



61
62
63
# File 'lib/annex/environment.rb', line 61

def info(message, level=:default)
  $stdout.puts colorize(message, level)
end

#inspectString

Return a human-friendly string for pretty printed or inspected instances.

Returns:

  • (String)


39
40
41
# File 'lib/annex/environment.rb', line 39

def inspect
  "#<#{self.class}: #{@cwd}>"
end