Skip to content

Commit

Permalink
Merged pull request #102 from tsigo/develop.
Browse files Browse the repository at this point in the history
Add a "--quiet" or "-q" option to perform
  • Loading branch information
Michael van Rooijen committed Apr 29, 2011
2 parents 85ebb4a + 6bc7b1c commit dcdc3bf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
17 changes: 12 additions & 5 deletions bin/backup
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ class BackupCLI < Thor
# Performs the backup process. The only required option is the --trigger [-t].
# If the other options (--config_file, --data_path, --tmp_path) aren't specified
# it'll fallback to the (good) defaults
method_option :trigger, :type => :string, :aliases => ['-t', '--triggers'], :required => true
method_option :config_file, :type => :string, :aliases => '-c'
method_option :data_path, :type => :string, :aliases => '-d'
method_option :log_path, :type => :string, :aliases => '-l'
method_option :trigger, :type => :string, :aliases => ['-t', '--triggers'], :required => true
method_option :config_file, :type => :string, :aliases => '-c'
method_option :data_path, :type => :string, :aliases => '-d'
method_option :log_path, :type => :string, :aliases => '-l'
method_option :tmp_path, :type => :string
method_option :quiet, :type => :boolean, :aliases => '-q'
desc 'perform', "Performs the backup for the specified trigger.\n" +
"You may perform multiple backups by providing multiple triggers, separated by commas.\n\n" +
"Example:\n\s\s$ backup perform --triggers backup1,backup2,backup3,backup4\n\n" +
Expand Down Expand Up @@ -76,6 +77,12 @@ class BackupCLI < Thor
FileUtils.mkdir_p(path)
end

##
# Silence Backup::Logger from printing to STDOUT, if --quiet was specified
if options[:quiet]
Backup::Logger.send(:const_set, :QUIET, options[:quiet])
end

##
# Process each trigger
options[:trigger].split(",").map(&:strip).each do |trigger|
Expand Down Expand Up @@ -257,4 +264,4 @@ end

##
# Enable the CLI for the Backup binary
BackupCLI.start
BackupCLI.start
12 changes: 8 additions & 4 deletions lib/backup/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@ class Logger
##
# Outputs a messages to the console and writes it to the backup.log
def self.message(string)
puts loggify(:message, string, :green)
puts loggify(:message, string, :green) unless quiet?
to_file loggify(:message, string)
end

##
# Outputs an error to the console and writes it to the backup.log
def self.error(string)
puts loggify(:error, string, :red)
puts loggify(:error, string, :red) unless quiet?
to_file loggify(:error, string)
end

##
# Outputs a notice to the console and writes it to the backup.log
def self.warn(string)
puts loggify(:warning, string, :yellow)
puts loggify(:warning, string, :yellow) unless quiet?
to_file loggify(:warning, string)
end

##
# Outputs the data as if it were a regular 'puts' command,
# but also logs it to the backup.log
def self.normal(string)
puts string
puts string unless quiet?
to_file string
end

Expand Down Expand Up @@ -90,5 +90,9 @@ def self.colorize(string, code)
"\e[#{code}m#{string}\e[0m"
end

def self.quiet?
const_defined?(:QUIET) && QUIET
end

end
end
12 changes: 12 additions & 0 deletions spec/logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,16 @@
Backup::Logger.silent "This has been logged."
end
end

context 'when quieted' do
it do
Backup::Logger.send(:const_set, :QUIET, true)
Backup::Logger.expects(:puts).never

Backup::Logger.message "This has been logged."
Backup::Logger.error "This has been logged."
Backup::Logger.warn "This has been logged."
Backup::Logger.normal "This has been logged."
end
end
end

0 comments on commit dcdc3bf

Please sign in to comment.