avm-tools 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 25b95f597939c9cbdc8e67d9a3d68f4edcb184197607c7ffdbe66a6713e5f2d2
4
- data.tar.gz: 29a1f0c7bfe1f945db68bd2fba973bacd58d12b82551bc85e6922fad5206c80c
3
+ metadata.gz: ec26f5acffe872504b8bb38a3ac9576c5e8a0371ed59bf47291bbaa7ee409e86
4
+ data.tar.gz: abff823fcf4d67c8585234b44bc02c09c71f5abfc4c07cb81879c12e6d207f30
5
5
  SHA512:
6
- metadata.gz: 4c86b25b35d775fde5b9c82ee4d963f9d185e27d846f5e7e81e4f016774eadbe0fcf3ddf6628dd8db76276be8809938355968b15609ffd25e3778011ce3c7b42
7
- data.tar.gz: a1bc826968c160a28c8d63424139fd2e3e326a99a3568c35d765b6c0d0ac6f7ffd661f3db1a3a9383863ec2cc10765478624981502f2f7a4ea4ee43699508b56
6
+ metadata.gz: 21c93ff405233684dc17a31d1fcecaab294f04de974984745e48539099d88e09f3e1006f9e576a2b34205ebae75668c58a5286de1d7f72ec917ab4c43416e54c
7
+ data.tar.gz: 99e36aebae62dfd8d654177019156d9c7ba2dc187b778aad3b206da3e637657eb3f68bc968bb1293d717379d73447d19148d45114f41d738aeb17e48bfef2ddd
@@ -8,10 +8,11 @@ module Avm
8
8
  class Rotate
9
9
  include ::EacRubyUtils::SimpleCache
10
10
 
11
- attr_reader :source_path
11
+ attr_reader :options, :source_path
12
12
 
13
- def initialize(source_path)
13
+ def initialize(source_path, options = {})
14
14
  @source_path = source_path
15
+ @options = options
15
16
  end
16
17
 
17
18
  def run
@@ -19,14 +20,47 @@ module Avm
19
20
  return validate_msg if validate_msg.present?
20
21
 
21
22
  ::FileUtils.mv(source_path, target_path)
23
+ check_space_limit
22
24
  nil
23
25
  end
24
26
 
27
+ def space_limit
28
+ r = options[:space_limit].try(:to_i)
29
+ return r if r.present? && r.positive?
30
+
31
+ r
32
+ end
33
+
34
+ def space_used
35
+ rotated_files.inject(0) { |a, e| a + ::File.size(e) }
36
+ end
37
+
38
+ def rotated_files
39
+ ::Dir["#{dirname}/#{source_basename_without_extension}*#{source_extension}"]
40
+ end
41
+
42
+ def oldest_rotated_file
43
+ rotated_files.min_by { |file| [::File.mtime(file)] }
44
+ end
45
+
25
46
  private
26
47
 
48
+ def check_space_limit
49
+ return unless space_limit
50
+
51
+ while space_used > space_limit
52
+ file = oldest_rotated_file
53
+ unless file
54
+ raise 'oldest_rotated_file returned nil ' \
55
+ "(Limit: #{space_limit}, used: #{space_used})"
56
+ end
57
+ ::File.delete(file)
58
+ end
59
+ end
60
+
27
61
  def validate
28
- return 'Source not exist' unless ::File.exist?(source_path)
29
- return 'Already rotated' if source_rotated?
62
+ return "Source file \"#{source_path}\" does not exist" unless ::File.exist?(source_path)
63
+ return "File \"#{source_path}\" is already rotated" if source_rotated?
30
64
 
31
65
  nil
32
66
  end
@@ -44,7 +78,11 @@ module Avm
44
78
  end
45
79
 
46
80
  def target_path_uncached
47
- ::File.join(::File.dirname(source_path), target_basename)
81
+ ::File.join(dirname, target_basename)
82
+ end
83
+
84
+ def dirname
85
+ ::File.dirname(source_path)
48
86
  end
49
87
 
50
88
  def target_basename
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'eac_ruby_utils/console/docopt_runner'
4
+ require 'eac_ruby_utils/console/speaker'
4
5
  require 'avm/files/rotate'
5
6
 
6
7
  module Avm
@@ -8,19 +9,30 @@ module Avm
8
9
  class Runner < ::EacRubyUtils::Console::DocoptRunner
9
10
  class Files < ::EacRubyUtils::Console::DocoptRunner
10
11
  class Rotate < ::EacRubyUtils::Console::DocoptRunner
12
+ include ::EacRubyUtils::Console::Speaker
13
+
11
14
  DOC = <<~DOCOPT
12
15
  Rotates a file (Like a backup).
13
16
 
14
17
  Usage:
15
- __PROGRAM__ <path>
18
+ __PROGRAM__ [options] <path>
16
19
  __PROGRAM__ -h | --help
17
20
 
18
21
  Options:
19
- -h --help Show this screen.
22
+ -h --help Show this screen.
23
+ -L --space-limit=<space> Limit by <space> the space used by rotated files.
20
24
  DOCOPT
21
25
 
22
26
  def run
23
- ::Avm::Files::Rotate.new(options.fetch('<path>')).run
27
+ error_message = rotate.run
28
+ fatal_error(error_message) if error_message
29
+ end
30
+
31
+ def rotate
32
+ @rotate ||= ::Avm::Files::Rotate.new(
33
+ options.fetch('<path>'),
34
+ space_limit: options.fetch('--space-limit')
35
+ )
24
36
  end
25
37
  end
26
38
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Avm
4
4
  module Tools
5
- VERSION = '0.7.0'
5
+ VERSION = '0.8.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: avm-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Esquilo Azul Company
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-08-16 00:00:00.000000000 Z
11
+ date: 2019-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: eac_launcher