central 0.2.8 → 0.2.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/bin/central +29 -20
  3. data/lib/central.rb +85 -4
  4. metadata +3 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 673d289d151554b977cdbc987fbd5ea65323e605a9e5b193d5a1908b3738ea62
4
- data.tar.gz: a4a52750319b99b127496db875ddbe5df46378a8021c5b8b77bf6ac0f9457859
3
+ metadata.gz: ecf6eb46fbc67795241445bc38629214a0d9eb2b63237f3ade4ccc5009b2eb4f
4
+ data.tar.gz: d105df04c79771d9ae767c471185ec80147fbf7db6d552affa7d01185e4fe965
5
5
  SHA512:
6
- metadata.gz: c1dafe44846a27daf9911281bf721ff68bddc004df2e14822fb7b3e4dd1fa7bb87a8a9cedc197e0ec6c2519aa95c4f73ac8b0ccc85d652685bf46ca0f763e339
7
- data.tar.gz: '016499d9cd74e3a1a6bb78a123a60f44ddff74bb25198bb205eda2d8f90834467f4b3cf274544ef4d181cde77ed99e3ebf702f68d328314162808f2761633550'
6
+ metadata.gz: b3ec6fa7e823098fc75b55c66399b25da990bc3bdb58d9f54b1c9cb7f26b00eb123699c6772800cfc54aa68826b6152b61284c76555dd248183fbb2d9b497010
7
+ data.tar.gz: 5501d4249660a4d1d288aa6421439045f1ba66da1c316c62517a09d0ed1e7fb14f99f40fbef82966ae7d12ce411c61d0411f64872e4ace77bfdd2c52087ddd11
@@ -7,29 +7,38 @@
7
7
  # -------------------------------------------------------------------------
8
8
 
9
9
  require 'central'
10
+ require 'optparse'
10
11
 
11
- unless ARGV.empty?
12
- if ARGV[0] == '-v' || ARGV[0] == '--version' || ARGV[0] == '-version'
13
- puts 'central v0.2.8'
14
- exit 0
12
+ VERSION = 'v0.2.9'
13
+
14
+ options = {:color => true}
15
+ OptionParser.new do |opts|
16
+ opts.banner = %{central #{VERSION} - created by Dmitry Geurkov (d.geurkov@gmail.com) and licensed under LGPLv3
17
+
18
+ Usage: central [options] [directory|configuration.rb, ...]
19
+
20
+ Description: if directory is specified it will use configuration.rb file inside that directory
21
+ if no [directory|configuration.rb] is specified
22
+ it will use configuration.rb in current working directory
23
+
24
+ Options:}
25
+ opts.on("-m", "--monitor", "Monitor erb files for changes and reprocess them automatically") do |_|
26
+ options[:monitor] = true
15
27
  end
16
- if ARGV[0] == '-h' || ARGV[0] == '--help' || ARGV[0] == '-help'
17
- puts 'central v0.2.8 - created by Dmitry Geurkov (d.geurkov@gmail.com)'
18
- puts ' and licensed under LGPLv3'
19
- puts ''
20
- puts 'Usage: central [monitor] [path/to/configuration.rb]'
21
- puts ''
22
- puts 'Description: [monitor] - will monitor erb files for changes'
23
- puts ' reprocess them automatically, disabled by default'
24
- puts ' if no [path/to/configuration.rb] is specified'
25
- puts ' it will use configuration.rb in current working directory'
26
- exit 0
28
+
29
+ opts.on("-n", "--no-color", "Do not colorize output") do |_|
30
+ options[:color] = false
27
31
  end
28
- end
29
- if ARGV[0] == 'monitor'
30
- ARGV.shift
31
- run_central(ARGV)
32
+
33
+ opts.on("-v", "--version", "Print version") do |_|
34
+ puts "central #{VERSION}"
35
+ exit
36
+ end
37
+ end.parse!
38
+
39
+ if options[:monitor]
40
+ run_central(ARGV,options[:color])
32
41
  run_monitors
33
42
  else
34
- run_central(ARGV)
43
+ run_central(ARGV,options[:color])
35
44
  end
@@ -9,8 +9,10 @@ require 'erb'
9
9
  require 'socket'
10
10
  require 'open3'
11
11
  require 'fileutils'
12
+ require 'digest'
12
13
 
13
14
  # cli colors
15
+ $colored = true
14
16
  COLOR_RED = 31
15
17
  COLOR_GREEN = 32
16
18
 
@@ -19,7 +21,11 @@ $monitors = {}
19
21
 
20
22
  # putsc, puts with color
21
23
  def color(message, color)
22
- "\e[#{color}m#{message}\e[0m"
24
+ if $colored
25
+ "\e[#{color}m#{message}\e[0m"
26
+ else
27
+ message
28
+ end
23
29
  end
24
30
 
25
31
  # info
@@ -170,6 +176,26 @@ def file_exists?(path)
170
176
  File.file?(path) && File.readable?(path)
171
177
  end
172
178
 
179
+ # get file size
180
+ def file_size(path)
181
+ File.new(abs(path)).size
182
+ end
183
+
184
+ # get file creation time
185
+ def file_ctime(path)
186
+ File.new(abs(path)).ctime
187
+ end
188
+
189
+ # get file modification time
190
+ def file_mtime(path)
191
+ File.new(abs(path)).mtime
192
+ end
193
+
194
+ # get directory entries
195
+ def dir_entries(path)
196
+ Dir.entries(abs(path)).select { |f| f != '.' && f != '..' }
197
+ end
198
+
173
199
  # check if directory exists
174
200
  def dir_exists?(path)
175
201
  path = abs(path)
@@ -192,6 +218,11 @@ def symlink_path(symlink)
192
218
  out.strip
193
219
  end
194
220
 
221
+ # calculate SHA2 digest for a file
222
+ def sha2(file)
223
+ Digest::SHA256.hexdigest read(file)
224
+ end
225
+
195
226
  # make directory including intermediate directories
196
227
  def mkdir(path)
197
228
  path = abs(path)
@@ -307,8 +338,12 @@ def git(url, path, branch: nil, silent: true, depth: nil)
307
338
  end
308
339
 
309
340
  # download url into a path using curl
310
- def curl(url, path, verbose: false)
341
+ def curl(url, path, content_length_check: false, verbose: false)
311
342
  path = abs(path)
343
+ if content_length_check and file_exists?(path)
344
+ content_length = curl_headers(url, verbose: verbose)['content-length'].to_i
345
+ return if file_size(path) == content_length
346
+ end
312
347
  info 'Downloading', "#{url} → #{path}"
313
348
  exit_code, output, = shell("curl -s -S \"#{url}\"",
314
349
  verbose: verbose, silent: true)
@@ -320,6 +355,21 @@ def curl(url, path, verbose: false)
320
355
  info 'Downloaded', "#{url} → #{path}"
321
356
  end
322
357
 
358
+ # get url response headers as Hash using curl
359
+ def curl_headers(url, method: 'HEAD', verbose: false)
360
+ exit_code, output, = shell("curl -I -X #{method} -s -S \"#{url}\"",
361
+ verbose: verbose, silent: true)
362
+ unless exit_code.success?
363
+ error output
364
+ fail "Couldn't get headers from", url
365
+ end
366
+ headers = {}
367
+ output.scan(/^(?!HTTP)([^:]+):(.*)$/).each do |m|
368
+ headers[m[0].strip.downcase] = m[1].sub("\r","").strip
369
+ end
370
+ headers
371
+ end
372
+
323
373
  # read content of a file
324
374
  def read(file)
325
375
  file = abs(file)
@@ -364,8 +414,17 @@ def ls(path, dotfiles: false, grep: '', dir: true, file: true)
364
414
  ls
365
415
  end
366
416
 
417
+ # compare_file
418
+ def compare_file(from, to)
419
+ from = abs(from)
420
+ to = abs(to)
421
+ FileUtils.compare_file(from, to)
422
+ end
423
+
367
424
  # copy_file
368
425
  def copy_file(from, to)
426
+ from = abs(from)
427
+ to = abs(to)
369
428
  fail "Couldn't access file", from unless file_exists?(from)
370
429
 
371
430
  return if file_exists?(to) && FileUtils.compare_file(from, to)
@@ -379,7 +438,27 @@ def copy(from, to)
379
438
  from = abs(from)
380
439
  to = abs(to)
381
440
  if dir_exists?(from)
382
- (Dir.entries(from).select { |f| f != '.' && f != '..' }).each do |f|
441
+ dir_entries(from).each do |f|
442
+ FileUtils.mkdir_p(to)
443
+ copy("#{from}/#{f}", "#{to}/#{f}")
444
+ end
445
+ else
446
+ copy_file(from, to)
447
+ end
448
+ end
449
+
450
+ # mirror
451
+ def mirror(from, to)
452
+ from = abs(from)
453
+ to = abs(to)
454
+ if dir_exists?(from)
455
+ from_entries = dir_entries(from)
456
+ if dir_exists?(to)
457
+ dir_entries(to).each do |f|
458
+ rm("#{to}/#{f}", recursive: true) unless from_entries.include?(f)
459
+ end
460
+ end
461
+ from_entries.each do |f|
383
462
  FileUtils.mkdir_p(to)
384
463
  copy("#{from}/#{f}", "#{to}/#{f}")
385
464
  end
@@ -418,6 +497,7 @@ end
418
497
  def run(file)
419
498
  cwd = pwd
420
499
  file = abs(file)
500
+ file = File.join(file,'configuration.rb') if not file_exists?(file) and dir_exists?(file)
421
501
  fail 'No configuration file found', file unless file_exists?(file)
422
502
 
423
503
  info 'Running configuration', file
@@ -433,7 +513,8 @@ def run_if_exists(file)
433
513
  end
434
514
 
435
515
  # run central configuration
436
- def run_central(configurations)
516
+ def run_central(configurations, colored = true)
517
+ $colored = colored
437
518
  if configurations.instance_of?(Array) && !configurations.empty?
438
519
  configurations.each { |configuration| run configuration }
439
520
  elsif configurations.instance_of?(String)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: central
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.8
4
+ version: 0.2.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Geurkov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-22 00:00:00.000000000 Z
11
+ date: 2020-12-31 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: central dotfile management system
14
14
  email: d.geurkov@gmail.com
@@ -38,8 +38,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
38
38
  - !ruby/object:Gem::Version
39
39
  version: '0'
40
40
  requirements: []
41
- rubyforge_project:
42
- rubygems_version: 2.7.6
41
+ rubygems_version: 3.1.4
43
42
  signing_key:
44
43
  specification_version: 4
45
44
  summary: central dotfile management