class Vman::Client

Constants

ATTRS
DEFAULT_CONFIG_PATH

Public Class Methods

new(args = ARGV) click to toggle source
# File lib/vman.rb, line 14
def initialize(args = ARGV)
  @prompt     = TTY::Prompt.new
  @config     = validate_config
  @bucket_uri = @config['bucket_uri']
  @local_dir  = @config['local_dir'] || Dir.pwd
  parse_args(args)
end

Public Instance Methods

diff(args) click to toggle source
# File lib/vman.rb, line 31
def diff(args)
  from, to = @local_dir, @bucket_uri
  action   = 'push'

  if args.shift == '-r'
    to, from, action = from, to, 'pull'
  end

  cmd = "aws s3 sync #{from} #{to} --dryrun"

  @prompt.warn("Showing file diff for a #{action} from \'#{from}\' to \'#{to}\'\n")
  @prompt.warn(`#{cmd}`)
end
pull(args) click to toggle source
# File lib/vman.rb, line 45
def pull(args)
  cmd     = "aws s3 sync #{@bucket_uri} #{@local_dir}"
  sub_cmd = args.shift
  cmd     += ' --dryrun' unless sub_cmd == '-f'

  @prompt.warn("Pulling changes from remote bucket \'#{@bucket_uri}\' to #{@local_dir}.")
  @prompt.warn("This will overwrite local changes!")
  sub_cmd == '-f' ? @prompt.ok(`#{cmd}`) : @prompt.warn(`#{cmd}`)
end
push(args) click to toggle source
# File lib/vman.rb, line 22
def push(args)
  cmd     = "aws s3 sync #{@local_dir} #{@bucket_uri}"
  sub_cmd = args.shift
  cmd     += ' --dryrun' unless sub_cmd == '-f'

  @prompt.warn("Pushing changes from #{@local_dir} to remote bucket \'#{@bucket_uri}\'")
  sub_cmd == '-f' ? @prompt.ok(`#{cmd}`) : @prompt.warn(`#{cmd}`)
end

Private Instance Methods

delegate!(args) click to toggle source
# File lib/vman.rb, line 59
def delegate!(args)
  available_methods = (self.public_methods(false) - ATTRS)
  cmd = args.shift.to_sym

  if available_methods.include?(cmd)
    send(cmd, args)
  else
    raise ArgumentError, "No matching method for #{cmd}"
  end
end
parse_args(args) click to toggle source
# File lib/vman.rb, line 70
def parse_args(args)
  arg = ( args[0] ? args[0].downcase : nil)

  case arg
  when '-v', '--version'
    @prompt.say(Vman::VERSION)
  when '-h', '--help', NilClass
    @prompt.say(Vman::Help.docs)
  when '-i', '--interactive'
    Vman::Menu.new(@config).show
  else
    delegate!(args)
  end
end
validate_config(args = nil) click to toggle source
# File lib/vman.rb, line 85
def validate_config(args = nil)
  unless File.exist?(DEFAULT_CONFIG_PATH)
    @prompt.error("No configuration file found at #{DEFAULT_CONFIG_PATH}. Run 'vman configure' to generate one")
    raise ValidationError
  end

  @config = JSON.parse(File.read(DEFAULT_CONFIG_PATH))
  unless @config['bucket_uri']
    @prompt.error("No key found under 'bucket_uri' in #{DEFAULT_CONFIG_PATH}")
    raise ValidationError
  end

  unless @config['bucket_uri'] =~ /s3:\/\//
    @prompt.error("Value 'bucket_uri' must be a valid S3 URI beginning with 's3://'")
    raise ValidationError
  end
  @config
end