summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2015-02-04 21:03:29 -0700
committermo khan <mo@mokhan.ca>2015-02-04 21:03:29 -0700
commit91ab6ce89280d244e0aca9edd91e93979b7e14e0 (patch)
tree6d449c099b7c22297406aab7990b267c60753c0b /lib
parent5bb42397569d7f0108178009c0c130128239ab34 (diff)
create fake agent to watch filesystem for changes and publish to query api.
Diffstat (limited to 'lib')
-rw-r--r--lib/fake_agent.rb41
-rw-r--r--lib/tasks/agent.rake8
-rw-r--r--lib/tasks/scan.rake22
3 files changed, 49 insertions, 22 deletions
diff --git a/lib/fake_agent.rb b/lib/fake_agent.rb
new file mode 100644
index 0000000..19ffe25
--- /dev/null
+++ b/lib/fake_agent.rb
@@ -0,0 +1,41 @@
+class FakeAgent
+ attr_reader :id, :endpoint
+
+ def initialize(id, endpoint)
+ @id = id
+ @endpoint = endpoint
+ end
+
+ def run(directory)
+ listener = Listen.to(directory, debug: true) do |modified, added, removed|
+ publish_event(:modified, modified)
+ publish_event(:added, added)
+ publish_event(:removed, removed)
+ end
+
+ listener.start
+ sleep
+ end
+
+ private
+
+ def publish_event(event, files)
+ files.each do |file|
+ fingerprint = fingerprint_for(file)
+ url = "#{endpoint}/agents/#{id}/files/#{fingerprint}"
+ puts url
+ Typhoeus.get(url, body: {
+ payload: {
+ event: event,
+ full_path: file
+ }
+ })
+ end
+ end
+
+ def fingerprint_for(file)
+ result = `shasum -a 256 #{file}`
+ sha, * = result.split(' ')
+ sha
+ end
+end
diff --git a/lib/tasks/agent.rake b/lib/tasks/agent.rake
new file mode 100644
index 0000000..9fe156e
--- /dev/null
+++ b/lib/tasks/agent.rake
@@ -0,0 +1,8 @@
+namespace :agent do
+ desc "watch all files"
+ task watch: :environment do
+ require 'fake_agent'
+ agent = FakeAgent.new(Agent.first.id, 'http://localhost:3000')
+ agent.run(Dir.pwd)
+ end
+end
diff --git a/lib/tasks/scan.rake b/lib/tasks/scan.rake
deleted file mode 100644
index f3f60ba..0000000
--- a/lib/tasks/scan.rake
+++ /dev/null
@@ -1,22 +0,0 @@
-namespace :scan do
- desc "scan all files"
- task dir: :environment do
- require 'net/http'
-
- agent = Agent.first
- Dir['**/**/*'].each do |file|
- if File.file?(file)
- result = `shasum -a 256 #{file}`
- sha, * = result.split(' ')
- full_path = File.expand_path(file)
-
- url = "http://localhost:3000/agents/#{agent.id}/files/#{sha}"
- Typhoeus.get(url, body: {
- payload: {
- full_path: full_path
- }
- })
- end
- end
- end
-end