class Sgfa::Cli::Binder

Command line interface for {Sgfa::Binder}. Currently it just supports {Sgfa::BinderFs}.

@todo Needs to be fully implemented. Currently just a shell.

Public Instance Methods

_get_aws() click to toggle source

Get AWS creds

# File lib/sgfa/cli/binder.rb, line 317
def _get_aws
  json = File.read(options[:key])
  creds = JSON.parse(json)
  opts = {
    region: creds['aws_region'],
    access_key_id: creds['aws_id'],
    secret_access_key: creds['aws_key'],
  }
  return opts

rescue Errno::ENOENT
  puts 'AWS keys file not found'
  return false

rescue Errno::EACCESS
  puts 'AWS key file permission denied'
  return false

rescue JSON::JSONError
  puts 'AWS key file JSON parse error'
  return false
end
_get_last() click to toggle source

Get last option

# File lib/sgfa/cli/binder.rb, line 342
def _get_last()
  return {} if !options[:last]
  json = File.read(options[:last])
  last = JSON.parse(json)
  return last
rescue Errno::ENOENT
  puts "Last backup state file not found"
  exit
rescue Errno::EACCESS
  puts "Access denied to last backup state file"
  exit
rescue JSON::JSONError
  puts "Last backup state file parse failed"
  exit
end
_get_log() click to toggle source

get the log

# File lib/sgfa/cli/binder.rb, line 300
def _get_log
  log = Logger.new(STDOUT)
  case options[:level]
    when 'debug'
      log.level = Logger::DEBUG
    when 'info'
      log.level = Logger::INFO
    when 'warn'
      log.level = Logger::WARN
    else
      log.level = Logger::ERROR
  end
  return log
end
_open_binder() click to toggle source

Open the binder

# File lib/sgfa/cli/binder.rb, line 282
def _open_binder()
  # open binder
  if !options[:fs_path]
    puts 'Binder type and location required.'
    return false
  end
  bnd = ::Sgfa::BinderFs.new
  begin
    bnd.open(options[:fs_path])
  rescue ::Sgfa::Error::Limits, ::Sgfa::Error::NonExistent => exp
    puts exp.message
    return false
  end
  return bnd
end
_put_last(last) click to toggle source

Save last

# File lib/sgfa/cli/binder.rb, line 361
def _put_last(last)
  return if !options[:save]
  json = JSON.pretty_generate(last) + "\n"
  File.open(options[:save], 'w', :encoding => 'utf-8'){|fi| fi.write json}
rescue Errno::EACCESS
  puts "Access denied to backup state file"
  exit
end
backup_fs(dest) click to toggle source
# File lib/sgfa/cli/binder.rb, line 240
def backup_fs(dest)
  return if !(bnd = _open_binder)
  log = _get_log
  last = _get_last
  sto = ::Sgfa::StoreFs.new
  sto.open(dest)
  last = bnd.backup_push(sto, prev: last, log: log)
  bnd.close
  sto.close
  _put_last(last)
end
backup_s3() click to toggle source
# File lib/sgfa/cli/binder.rb, line 170
def backup_s3()

  return if !(aws = _get_aws)
  return if !(bnd = _open_binder)
  log = _get_log
  last = _get_last
  s3 = ::Aws::S3::Client.new(aws)
  sto = ::Sgfa::StoreS3.new
  sto.open(s3, options[:bucket])
  last = bnd.backup_push(sto, prev: last, log: log)
  bnd.close
  sto.close
  _put_last(last)

end
create(id_text, init_json) click to toggle source
# File lib/sgfa/cli/binder.rb, line 76
def create(id_text, init_json)

  if !options[:fs_path]
    puts 'Binder type and location required.'
    return
  end
  bnd = ::Sgfa::BinderFs.new
  tr = {
    user: options[:user],
    title: options[:title],
    body: options[:body],
  }
  begin
    init = JSON.parse(File.read(init_json), :symbolize_names => true)
    init[:id_text] = id_text
    bnd.create(options[:fs_path], tr, init)
  rescue Errno::ENOENT
    puts 'Binder type JSON file not found'
    return
  rescue JSON::JSONError
    puts 'Binder type JSON file did not parse'
    return
  rescue ::Sgfa::Error::NonExistent, ::Sgfa::Error::Limits => exp
    puts exp.message
    return
  end

end
info() click to toggle source
# File lib/sgfa/cli/binder.rb, line 38
def info

  # open binder
  return if !(bnd = _open_binder)

  # print info
  tr = {
    perms: ['info']
  }
  info = bnd.binder_info(tr)
  puts 'Text ID: %s' % info[:id_text]
  puts 'Hash ID: %s' % info[:id_hash]
  puts 'Values: %d' % info[:values].size
  puts 'Jackets: %d' % info[:jackets].size
  puts 'Users: %d' % info[:users].size

  bnd.close
end
restore_fs(id_text, bak) click to toggle source
# File lib/sgfa/cli/binder.rb, line 261
def restore_fs(id_text, bak)
  if !options[:fs_path]
    puts 'Binder type and location required.'
   return
  end
  sto = ::Sgfa::StoreFs.new
  sto.open(bak)
  log = _get_log
  bnd = ::Sgfa::BinderFs.new
  bnd.create_raw(options[:fs_path], id_text)
  bnd.open(options[:fs_path])
  bnd.backup_pull(sto, log: log)
  bnd.close
  sto.close
end
restore_s3(id_text) click to toggle source
# File lib/sgfa/cli/binder.rb, line 205
def restore_s3(id_text)
  if !options[:fs_path]
    puts 'Binder type and location required.'
   return
  end
  return if !(aws = _get_aws)
  log = _get_log
  s3 = ::Aws::S3::Client.new(aws)
  sto = ::Sgfa::StoreS3.new
  sto.open(s3, options[:bucket])
  bnd = ::Sgfa::BinderFs.new
  bnd.create_raw(options[:fs_path], id_text)
  bnd.open(options[:fs_path])
  bnd.backup_pull(sto, log: log)
  bnd.close
  sto.close
end
web_demo(css) click to toggle source
# File lib/sgfa/cli/binder.rb, line 124
def web_demo(css)
  begin
    css_str = File.read(css)
  rescue Errno::ENOENT
    puts 'CSS file not found'
    return
  end

  app_bnd = ::Sgfa::Demo::WebBinders.new(options[:dir], '/sgfa.css')
  app_auth = Rack::Auth::Basic.new(app_bnd, 'Sgfa Demo'){|name, pass| true}
  app_css = ::Sgfa::Demo::WebCss.new(css_str, '/sgfa.css', app_auth)

  Rack::Handler::WEBrick.run(app_css, {
    :Port => options[:port],
    :BindAddress => options[:addr],
    })

end