-
Notifications
You must be signed in to change notification settings - Fork 2
/
publish
executable file
·73 lines (58 loc) · 1.25 KB
/
publish
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env ruby
require 'aws-sdk-v1'
require 'digest/md5'
bucket = ENV['AWS_BUCKET'] || File.basename(Dir.pwd)
SITE = AWS::S3.new(region: ENV['AWS_REGION'] || 'eu-west-1')
.buckets[bucket]
USAGE = <<EOF
usage: #{$0} file...
the following environment vars will be used:
AWS_REGION
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
EOF
def upload(files=["."])
files = files.map do |f|
File.directory?(f) ? traverse(Dir.new(f)) : f
end.flatten
files.each do |f|
obj = SITE.objects[f]
local_md5 = md5sum(f).chomp
remote_md5 = obj.etag.tr('"', '') if obj.exists?
print "#{f}.. "
if local_md5.to_s != remote_md5.to_s
obj.write(Pathname.new(f))
obj.acl = :public_read
puts "ok"
else
puts "skipping"
end
end
end
def md5sum(file_name)
Digest::MD5.hexdigest(File.read(file_name))
end
def traverse(dir)
list = []
dir.each do |entry|
unless [".", "..", ".git"].include?(entry)
path = File.join(dir.path, entry)
if File.directory?(path)
list += traverse(Dir.new(path))
else
list << path
end
end
end
list
end
if ARGV[0] == "-h"
puts USAGE
exit 0
end
begin
upload(ARGV)
rescue Exception => e
$stderr.puts "error: #{e.message}"
exit 1
end