Class: Squidward::Command::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/squidward/commands/base.rb

Overview

Base command from where all the other commands inherit it contains the most used methods (stout, sterror, etc) and also handles the configuration, hence AWS credentials

Direct Known Subclasses

Help, Info, Logs, Secured, Version

Instance Method Summary collapse

Instance Method Details

#configuration(reload = false) ⇒ Object

Reads the configuration from the profile, if the reload parameter is set to true, it will read it again, if not it will just return what it’s already in-memory.



9
10
11
12
13
14
15
16
# File 'lib/squidward/commands/base.rb', line 9

def configuration(reload = false)
  # if the user asks not to reload we should send what we've got except we don't got anything
  return @configuration if (@configuration and not reload)
  path = File.expand_path File.join(CONF_PATH, SETTINGS_FILE)
  # if the configuration file isn't created yet it should create it
  return {} unless File.exists?(path)
  @configuration = YAML.load_file(path)
end

#display(msg, newline = true) ⇒ Object

Nice and clean way of echoing



27
28
29
30
31
32
33
34
# File 'lib/squidward/commands/base.rb', line 27

def display(msg, newline=true)
  if newline
    puts(msg)
  else
    print(msg)
    STDOUT.flush
  end
end

#loggerObject

Logger singleton



37
38
39
40
41
42
43
# File 'lib/squidward/commands/base.rb', line 37

def logger
  return @logger if @logger
  @logger = Logger.new(File.expand_path(File.join(CONF_PATH, LOG_FILE)))
  @logger.level = Logger::INFO
  @logger.formatter = Proc.new {|s, t, n, msg| "[#{t}] [#{s}] #{msg}\n"}
  @logger
end

#store_configuration(new_configuration) ⇒ Object

Dumps configuration file contents to the user profile



19
20
21
22
23
24
# File 'lib/squidward/commands/base.rb', line 19

def store_configuration(new_configuration)
  FileUtils.mkpath(File.expand_path(CONF_PATH))
  File.open(File.expand_path(File.join(CONF_PATH, SETTINGS_FILE)), "w+") do |io|
    YAML::dump(new_configuration, io)
  end
end