class Constancy::CLI

Attributes

cli_mode[RW]
command[RW]
config_file[RW]
extra_args[RW]
targets[RW]

Public Class Methods

configure(call_external_apis: true) click to toggle source
# File lib/constancy/cli.rb, line 80
def configure(call_external_apis: true)
  return if Constancy.configured?

  begin
    Constancy.configure(path: self.config_file, targets: self.targets, call_external_apis: call_external_apis)

  rescue Constancy::ConfigFileNotFound
    if self.config_file.nil?
      STDERR.puts "constancy: ERROR: No configuration file found"
    else
      STDERR.puts "constancy: ERROR: Configuration file '#{self.config_file}' was not found"
    end
    exit 1

  rescue Constancy::ConfigFileInvalid => e
    if self.config_file.nil?
      STDERR.puts "constancy: ERROR: Configuration file is invalid:"
    else
      STDERR.puts "constancy: ERROR: Configuration file '#{self.config_file}' is invalid:"
    end
    STDERR.puts "  #{e}"
    exit 1

  rescue Constancy::ConsulTokenRequired => e
    STDERR.puts "constancy: ERROR: No Consul token could be found: #{e}"
    exit 1

  rescue Constancy::VaultConfigInvalid => e
    STDERR.puts "constancy: ERROR: Vault configuration invalid: #{e}"
    exit 1

  end

  if Constancy.config.sync_targets.count < 1
    if self.targets.nil?
      STDERR.puts "constancy: WARNING: No sync targets are defined"
    else
      STDERR.puts "constancy: WARNING: No sync targets were found that matched the specified list"
    end
  end
end
parse_args(args) click to toggle source
# File lib/constancy/cli.rb, line 16
def parse_args(args)
  self.print_usage if args.count < 1
  self.command = nil
  self.config_file = nil
  self.extra_args = []
  self.cli_mode = :command

  while arg = args.shift
    case arg
    when "--help"
      self.cli_mode = :help

    when "--config"
      self.config_file = args.shift

    when "--target"
      self.targets = (args.shift||'').split(",")

    when /^-/
      # additional option, maybe for the command
      self.extra_args << arg

    else
      if self.command.nil?
        # if command is not set, this is probably the command
        self.command = arg
      else
        # otherwise, pass it thru to the child command
        self.extra_args << arg
      end
    end
  end
end
print_usage() click to toggle source
run() click to toggle source
# File lib/constancy/cli.rb, line 122
def run
  self.parse_args(ARGV)

  case self.cli_mode
  when :help
    # TODO: per-command help
    self.print_usage

  when :command
    case self.command
    when 'check'      then Constancy::CLI::CheckCommand.run(self.extra_args)
    when 'push'       then Constancy::CLI::PushCommand.run(self.extra_args)
    when 'pull'       then Constancy::CLI::PullCommand.run(self.extra_args)
    when 'config'     then Constancy::CLI::ConfigCommand.run
    when 'targets'    then Constancy::CLI::TargetsCommand.run
    when nil          then self.print_usage

    else
      STDERR.puts "constancy: ERROR: unknown command '#{self.command}'"
      STDERR.puts
      self.print_usage
    end

  else
    STDERR.puts "constancy: ERROR: unknown CLI mode '#{self.cli_mode}'"
    exit 1

  end
end