Class: DotFiles::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/dot_files/command.rb

Instance Method Summary collapse

Constructor Details

#initialize(block) ⇒ Command

Returns a new instance of Command.



4
5
6
# File 'lib/dot_files/command.rb', line 4

def initialize(block)
  (class << self;self end).send :define_method, :command, &block
end

Instance Method Details

#call(*args) ⇒ Object



8
9
10
11
12
# File 'lib/dot_files/command.rb', line 8

def call(*args)
  arity = method(:command).arity
  args << nil while args.size < arity
  send :command, *args
end

#error(message, exit_code = 1) ⇒ Object



14
15
16
17
# File 'lib/dot_files/command.rb', line 14

def error(message, exit_code = 1)
  puts "\e[31m#{message}\e[0m"
  Process.exit(exit_code)
end

#puts(text = '') ⇒ Object



101
102
103
104
105
# File 'lib/dot_files/command.rb', line 101

def puts(text = '')
  text = text.to_s
  text.gsub!(/\{\{(.*)\}\}/) { "\e[33m#{$1}\e[0m"}
  super
end

#remote_file_contents(path) ⇒ Object



107
108
109
110
# File 'lib/dot_files/command.rb', line 107

def remote_file_contents(path)
  req = request("#{DotFiles.config.username}/#{path}")
  req ? JSON.parse(req)['file'] : nil
end

#remote_filesObject

 Return an array of all files which need to be synced



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/dot_files/command.rb', line 66

def remote_files
  if files = get_remote_files
    array = []
    DotFiles.config.shas = Hash.new unless DotFiles.config.shas.is_a?(Hash)
    for filename, remote_sha in files
      hash = {:filename => filename, :action => nil, :local_sha => nil, :local_cached_sha => nil, :remote_sha => remote_sha, :local_path => File.join(ENV['HOME'], filename)}
      
      if File.exist?(hash[:local_path])
        hash[:local_sha] = Digest::SHA1.hexdigest(File.read(hash[:local_path]))
        hash[:local_cached_sha] = DotFiles.config.shas[hash[:filename]]
        
        local_file_changed = (hash[:local_sha] != hash[:local_cached_sha])
        remote_file_changed = (hash[:local_cached_sha] != hash[:remote_sha])
        
        if local_file_changed && remote_file_changed
          hash[:action] = :conflict
        elsif !local_file_changed && remote_file_changed
          hash[:action] = :update_local
        elsif local_file_changed && !remote_file_changed
          hash[:action] = :update_remote
        else
          hash[:action] = :none
        end
      else
        hash[:action] = :create_local
      end
      
      array << hash
    end
    array
  else
    error "We couldn't get a list of files from the remote service. Please try later."
  end
end

#request(path, options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dot_files/command.rb', line 19

def request(path, options = {})
  uri = URI.parse(DotFiles.site + "/" + path)
  
  if options[:data]
    req = Net::HTTP::Post.new(uri.path)
  else
    req = Net::HTTP::Get.new(uri.path)
  end
  
  
  req.basic_auth(options[:username] || DotFiles.config.username, options[:api_key] || DotFiles.config.api_key)
  
  res = Net::HTTP.new(uri.host, uri.port)
  
  req.add_field("Accept", "application/json")
  req.add_field("Content-type", "application/json")
  
  if options[:data]
    options[:data] = options[:data].to_json
  end
  
  #res.use_ssl = true
  #res.verify_mode = OpenSSL::SSL::VERIFY_NONE
  begin
    Timeout.timeout(10) do
      res = res.request(req, options[:data])
      case res
      when Net::HTTPSuccess
        return res.body.strip
      else
        false
      end
    end
  rescue Timeout::Error
    puts "Sorry, the request timed out. Please try again later."
    Process.exit(1)
  end
end

#require_setupObject



58
59
60
61
62
# File 'lib/dot_files/command.rb', line 58

def require_setup
  unless DotFiles.config.username && DotFiles.config.api_key
    error "You haven't configured this computer yet. Run 'dotfiles setup' to authorise this computer."
  end
end

#save_local_file(filename, contents) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/dot_files/command.rb', line 117

def save_local_file(filename, contents)
  local_path = File.join(ENV['HOME'], filename)
  FileUtils.mkdir_p(File.dirname(local_path))
  File.open(local_path, 'w') { |f| f.write(contents) }
  DotFiles.config.shas[filename] = Digest::SHA1.hexdigest(contents)
  DotFiles.config.save
end

#save_remote_file(path, contents) ⇒ Object



112
113
114
115
# File 'lib/dot_files/command.rb', line 112

def save_remote_file(path, contents)
  req = request("save", :data => {:dot_file => {:path => path, :file => contents}})
  req ? true : false
end