gitdis 0.1.1.1 → 0.1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/bin/gitdis +9 -5
  4. data/lib/gitdis.rb +40 -23
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 122d1a3f1436e31c123e42fdfb6e9f146f196004
4
- data.tar.gz: 453ba3ef4a508206ef3b71e228eca18e7c534e66
3
+ metadata.gz: dff5d4e19798bf4fa0087e9a97affdc2b60cb5ff
4
+ data.tar.gz: 9d3133897f4a68c1de7298615e6d9a50ac7ed39b
5
5
  SHA512:
6
- metadata.gz: f0729411c0f2fe5519fffe64f965c2f449e9b4afc60fc3970c33215b3561f490abd1c2c992629c09b8947471c59ad90974e6eaa4e98d35cff5003d5f86285df8
7
- data.tar.gz: e077368e323bb720bd3040506853e3994a81f96b66971b76bfd0f6ef02e1aba199bddc035aada22667aef7b4e8aa8b4dffd54a42e74c316d7869909d494283cc
6
+ metadata.gz: fc9c1c81e4b3ccd76df7259f01bbd8970ed18770a0f53d8d1631a236c0d460a3adcba521a42660c19db323232581b4a89432ab4b68d1a460062f79ab8eeb6078
7
+ data.tar.gz: f48060d031e6b1865ce3f7f771e64889f8bf924a7748801a1dd50022ff6526336715a5617a86fe09ebff18b9a1932f30e77104913ab6858a176d4da7ffa588a8
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1.1
1
+ 0.1.2.1
data/bin/gitdis CHANGED
@@ -16,6 +16,7 @@ opts = Slop.parse { |o|
16
16
  o.string '-r', '--git-repo', 'path/to/repo_dir'
17
17
  o.string '-b', '--git-branch', 'e.g. master'
18
18
  o.separator ' Other options'
19
+ o.bool '--dry-run', 'Perform comparisons but do not write to Redis'
19
20
  o.bool '-D', '--dump', 'Just dump Redis contents per YAML keymap'
20
21
  o.on '-h', '--help' do
21
22
  puts o
@@ -71,6 +72,7 @@ if opts.dump?
71
72
  GitDis.dump(keymap.keys, redis_options)
72
73
  else
73
74
  gd = GitDis.new(config.fetch('git-repo'), redis_options)
75
+ gd.dry_run = true if opts[:'dry-run']
74
76
  gd.git_pull config.fetch('git-branch')
75
77
 
76
78
  keymap.each { |key, fileglob|
@@ -80,12 +82,14 @@ else
80
82
  puts "#{fileglob} not found"
81
83
  when false
82
84
  puts "#{fileglob} unchanged"
85
+ when true
86
+ puts "#{fileglob} changed but not updated (DRY RUN)"
87
+ when Array
88
+ ver, md5 = *result
89
+ puts "#{fileglob} updated:"
90
+ puts "\tVersion: #{ver} (#{md5})"
83
91
  else
84
- unless opts.dry_run?
85
- ver, md5 = *result
86
- puts "#{fileglob} updated:"
87
- puts "\tVersion: #{ver} (#{md5})"
88
- end
92
+ raise "unknown update result: #{result.inspect}"
89
93
  end
90
94
  }
91
95
  end
data/lib/gitdis.rb CHANGED
@@ -48,9 +48,45 @@ class GitDis
48
48
  }
49
49
  end
50
50
 
51
- attr_accessor :repo_dir, :redis
51
+ # concatenate file contents into a single string
52
+ # separate by newlines, including CRs if any CRs are detected anywhere
53
+ # include a filetype-specific separator if recognized
54
+ # filenames is an array, and all lengths 0-N are handled
55
+ def self.concatenate(filenames)
56
+ filetypes = filenames.map { |fname| File.extname(fname) }.uniq
57
+ case filetypes.length
58
+ when 0
59
+ return "" if filenames.length == 0
60
+ raise "filetype detection failure: #{filenames}"
61
+ when 1
62
+ sep = self.separator(filetypes.first)
63
+ else
64
+ raise "refusing to concatenate disparate filetypes: #{filetypes}"
65
+ end
66
+
67
+ payload = filenames.map { |fname|
68
+ contents = File.read(fname) || raise("could not read #{fname}")
69
+ sep << "\r" if !sep.include?("\r") and contents.include?("\r")
70
+ contents if !contents.empty?
71
+ }.compact.join("#{sep}\n")
72
+ end
73
+
74
+ # return a specific separator for known filetypes
75
+ # e.g. yaml document separator: ---
76
+ def self.separator(filetype)
77
+ filetype = filetype[1..-1] if filetype[0] == '.'
78
+ case filetype.downcase
79
+ when 'yaml', 'yml'
80
+ '---'
81
+ else
82
+ ''
83
+ end
84
+ end
85
+
86
+ attr_accessor :repo_dir, :redis, :dry_run
52
87
 
53
88
  def initialize(repo_dir, redis_options = {})
89
+ @dry_run = false
54
90
  @repo_dir = File.expand_path(repo_dir)
55
91
  raise "#{@repo_dir} does not exist!" unless Dir.exist? @repo_dir
56
92
  @redis = Redis.new(redis_options)
@@ -68,6 +104,7 @@ class GitDis
68
104
  end
69
105
 
70
106
  # quick false if calculated md5 == redis md5
107
+ # return true if dry run and update needed
71
108
  # otherwise update contents and md5; increment version
72
109
  def update_redis(base_key, file_contents)
73
110
  md5 = Digest::MD5.hexdigest(file_contents)
@@ -75,7 +112,6 @@ class GitDis
75
112
  return false if @redis.get(mkey) == md5
76
113
 
77
114
  if @dry_run
78
- puts "DRY RUN: would have updated #{base_key} with md5 #{md5}"
79
115
  true
80
116
  else
81
117
  @redis.set(fkey, file_contents)
@@ -88,6 +124,7 @@ class GitDis
88
124
  # e.g. update('foo:bar:baz', 'foo/bar/*.baz')
89
125
  # return nil # path does not exist
90
126
  # false # no update needed
127
+ # true # update was needed, but just a dry run
91
128
  # [ver, md5] # updated
92
129
  def update(base_key, relpath)
93
130
  # handle e.g. "foo/bar/*.yaml"
@@ -95,27 +132,7 @@ class GitDis
95
132
  case files.length
96
133
  when 0 then nil
97
134
  when 1 then self.update_redis(base_key, File.read(files.first))
98
- else
99
- puts "concatenating #{files.length} files"
100
- sep = "\n"
101
-
102
- payload = files.map { |fname|
103
- contents = File.read(fname)
104
- if contents and !contents.empty?
105
- # scan for carriage returns (Microsoft text format)
106
- sep = "\r\n" if sep == "\n" and contents.include?("\r")
107
- contents
108
- # debugging
109
- elsif contents
110
- puts "#{fname} is empty"
111
- nil
112
- else
113
- puts "File.read(#{fname}) returned false/nil"
114
- nil
115
- end
116
- }.compact.join("---#{sep}")
117
-
118
- self.update_redis(base_key, payload)
135
+ else self.update_redis(base_key, self.class.concatenate(files))
119
136
  end
120
137
  end
121
138
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gitdis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1.1
4
+ version: 0.1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rick Hull
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-04 00:00:00.000000000 Z
11
+ date: 2015-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: slop