class Evertils::Controller::Config

Public Instance Methods

pre_exec() click to toggle source
# File lib/evertils/controllers/config.rb, line 9
def pre_exec
  conf = contents_of('~/.evertils/config.yml')
  @token = conf['gist_token'] unless conf['gist_token'].nil?
  @updating = Gist.gist_exists?(@token)
  # if the requested gist doesn't exist, ignore it and generate a new one
  @token = nil unless @updating

  Gist.login! unless has_auth_already?
end
pull() click to toggle source
# File lib/evertils/controllers/config.rb, line 32
def pull
  # TODO: refactor this crap
  files = Gist.download(@token)

  FileUtils.mv(File.expand_path('~/.evertils'), File.expand_path('~/.evertils.old'))
  FileUtils.mkdir_p(File.expand_path('~/.evertils/templates/type'))

  dir = {}
  t_pfx = '~/.evertils/templates/type'
  c_pfx = '~/.evertils'
  root_files = ['config.yml']

  files.each_pair do |_, file|
    dir["#{t_pfx}/#{file['filename']}"] = file['content'] unless file['filename'] == 'config.yml'
    dir["#{c_pfx}/#{file['filename']}"] = file['content'] if root_files.include?(file['filename'])
  end

  dir.each_pair do |path, contents|
    File.open(File.expand_path(path), 'w') { |f| f.write(contents) }
  end
end
push() click to toggle source
# File lib/evertils/controllers/config.rb, line 19
def push
  options = {
    public: false
  }

  options.merge!(update: @token) unless @token.nil?

  resp = Gist.multi_gist(payload, options)
  @token = resp['id'] if resp.key?('id')

  Notify.success(message(resp['html_url'])) if store_token?
end

Private Instance Methods

contents_of(file) click to toggle source
# File lib/evertils/controllers/config.rb, line 68
def contents_of(file)
  YAML.load_file(File.expand_path(file))
end
gist_authenticate() click to toggle source
# File lib/evertils/controllers/config.rb, line 82
def gist_authenticate
  Gist.login!
end
has_auth_already?() click to toggle source
# File lib/evertils/controllers/config.rb, line 86
def has_auth_already?
  File.exist?(File.expand_path('~/.gist'))
end
message(url) click to toggle source
# File lib/evertils/controllers/config.rb, line 56
def message(url)
  return "Gist updated - #{url}" if @updating

  "Gist created - #{url}"
end
payload() click to toggle source
# File lib/evertils/controllers/config.rb, line 62
def payload
  {
    'config.yml' => contents_of('~/.evertils/config.yml').to_yaml,
  }.merge(types)
end
store_token?() click to toggle source
# File lib/evertils/controllers/config.rb, line 90
def store_token?
  store = YAML::Store.new(File.expand_path('~/.evertils/config.yml'))
  yaml = contents_of('~/.evertils/config.yml')

  store.transaction do
    yaml.each_pair { |key, value| store[key] = value }
    store['gist_token'] = @token
  end

  true
end
types() click to toggle source
# File lib/evertils/controllers/config.rb, line 72
def types
  types = {}

  Dir[File.expand_path('~/.evertils/templates/type/*.yml')].each do |dir|
    types["templates/type/#{dir.split('/').last}"] = contents_of(dir).to_yaml
  end

  types
end