Dahistory 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +15 -9
- data/lib/Dahistory/version.rb +1 -1
- data/lib/Dahistory.rb +70 -76
- data/spec/tests/Dashistory.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -10,30 +10,36 @@ It is mainly to help you keep backups of files you overwrite on servers you admi
|
|
10
10
|
**Example:** You use Chef or Puppet to write a config file, but you want to be sure you
|
11
11
|
are overwriting a file you have encountered before.
|
12
12
|
|
13
|
+
Installation
|
14
|
+
------------
|
15
|
+
|
16
|
+
gem 'Dahistory'
|
17
|
+
|
13
18
|
Useage
|
14
19
|
------
|
15
20
|
|
16
21
|
require "Dahistory"
|
17
22
|
|
18
23
|
path = "some/file.txt"
|
19
|
-
|
20
|
-
|
24
|
+
|
25
|
+
Dahistory path
|
26
|
+
|
27
|
+
# Checks your directory (default "./history").
|
21
28
|
# If not found there, saves copy of file in ./pending dir and
|
22
29
|
# raises Dahistory::Pending_File, "pending/HOSTNAME,path,some,file.txt.TIMESTAMP"
|
23
30
|
|
24
31
|
# You review the file,
|
25
|
-
# move the file from the pending directory, and
|
32
|
+
# move the file from the pending directory to your source/history dir, and
|
26
33
|
# re-do your last command (Capistrano, Chef, Puppet, etc.)
|
27
34
|
|
28
35
|
Override the default settings:
|
29
36
|
|
30
|
-
Dahistory
|
37
|
+
Dahistory { |o|
|
31
38
|
|
32
|
-
o.file "file/
|
33
|
-
o.dirs
|
34
|
-
o.
|
35
|
-
o.
|
36
|
-
o.backup_file "#{`hostname`}.backup.path.txt"
|
39
|
+
o.file "file/name.txt"
|
40
|
+
o.dirs "dir1", "dir2" # defaults to "./history"
|
41
|
+
o.pending_dir "./pending"
|
42
|
+
o.backup_file "#{`hostname`}.name.txt.#{Time.now.to_i}"
|
37
43
|
|
38
44
|
}
|
39
45
|
|
data/lib/Dahistory/version.rb
CHANGED
data/lib/Dahistory.rb
CHANGED
@@ -1,108 +1,102 @@
|
|
1
1
|
require 'Dahistory/version'
|
2
2
|
|
3
|
+
def Dahistory file = nil
|
4
|
+
da = Dahistory.new { |o|
|
5
|
+
o.file(file) if file
|
6
|
+
yield(o) if block_given?
|
7
|
+
}
|
8
|
+
|
9
|
+
da.save
|
10
|
+
end # === def
|
3
11
|
|
4
12
|
class Dahistory
|
5
13
|
|
6
14
|
Pending = Class.new(RuntimeError)
|
7
15
|
|
8
|
-
module
|
9
|
-
|
10
|
-
def
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
da.save
|
16
|
+
module Base
|
17
|
+
|
18
|
+
def initialize file_path = nil
|
19
|
+
@file = nil
|
20
|
+
file!(file_path) if file_path
|
21
|
+
dirs './history'
|
22
|
+
pending_dir './pending'
|
23
|
+
yield(self) if block_given?
|
17
24
|
end
|
18
25
|
|
19
|
-
|
26
|
+
old_meths = public_instance_methods
|
20
27
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
@file = nil
|
25
|
-
file!(file_path) if file_path
|
26
|
-
dirs ['.']
|
27
|
-
pending_dir "./pending"
|
28
|
-
@backup_dir = nil
|
29
|
-
yield(self) if block_given?
|
30
|
-
end
|
31
|
-
|
32
|
-
old_meths = public_instance_methods
|
33
|
-
|
34
|
-
def file path
|
35
|
-
@file = File.expand_path(path)
|
36
|
-
end
|
28
|
+
def file path
|
29
|
+
@file = File.expand_path(path)
|
30
|
+
end
|
37
31
|
|
38
|
-
|
39
|
-
|
40
|
-
|
32
|
+
def dirs *args
|
33
|
+
@dirs = args.flatten.map { |dir| File.expand_path dir }
|
34
|
+
end
|
41
35
|
|
42
|
-
|
43
|
-
|
44
|
-
|
36
|
+
def pending_dir dir = :RETURN
|
37
|
+
@pending_dir = File.expand_path(dir)
|
38
|
+
end
|
45
39
|
|
46
|
-
|
47
|
-
@pending_dir = File.expand_path(dir)
|
48
|
-
end
|
40
|
+
(public_instance_methods - old_meths ).each { |name|
|
49
41
|
|
50
|
-
|
51
|
-
|
52
|
-
alias_method :"#{name}_set", name
|
42
|
+
alias_method :"#{name}_set", name
|
53
43
|
|
54
|
-
|
44
|
+
eval %~
|
55
45
|
def #{name} *args, &blok
|
56
|
-
|
46
|
+
|
57
47
|
if args.empty?
|
58
|
-
|
48
|
+
|
59
49
|
unless instance_variable_defined?(:@#{name})
|
60
50
|
raise ArgumentError, "Instance variable not set: @#{name}"
|
61
51
|
end
|
62
52
|
@#{name}
|
63
|
-
|
53
|
+
|
64
54
|
else
|
65
|
-
|
55
|
+
|
66
56
|
#{name}_set(*args, &blok)
|
67
|
-
|
57
|
+
|
68
58
|
end
|
69
|
-
|
70
|
-
end # === def
|
71
|
-
~
|
72
|
-
|
73
|
-
}
|
74
|
-
|
75
|
-
def backup_file str = :RETURN
|
76
|
-
if str == :RETURN
|
77
|
-
@backup_file ||= "#{`hostname`.strip}-#{file.gsub('/',',')}.#{Time.now.utc.strftime "%Y.%m.%d.%H.%M.%S"}"
|
78
|
-
return @backup_file
|
79
|
-
end
|
80
|
-
|
81
|
-
@backup_file = str
|
82
|
-
end
|
83
|
-
|
84
|
-
def source_files
|
85
|
-
dirs.map { |path|
|
86
|
-
full = File.join( File.expand_path(path), "/*")
|
87
|
-
files = Dir.glob( full, File::FNM_DOTMATCH ).select { |unk| File.file?(unk) }
|
88
|
-
}.flatten
|
89
|
-
end
|
90
|
-
|
91
|
-
def save
|
92
59
|
|
93
|
-
|
94
|
-
|
60
|
+
end # === def
|
61
|
+
~
|
95
62
|
|
96
|
-
old = source_files.detect { |path|
|
97
|
-
raw = File.read(path)
|
98
|
-
raw.gsub("\r","") == standard
|
99
63
|
}
|
100
64
|
|
101
|
-
|
102
|
-
|
103
|
-
|
65
|
+
def backup_file str = :RETURN
|
66
|
+
if str == :RETURN
|
67
|
+
@backup_file ||= "#{`hostname`.strip}-#{file.gsub('/',',')}.#{Time.now.utc.strftime "%Y.%m.%d.%H.%M.%S"}"
|
68
|
+
return @backup_file
|
69
|
+
end
|
70
|
+
|
71
|
+
@backup_file = str
|
104
72
|
end
|
73
|
+
|
74
|
+
def source_files
|
75
|
+
dirs.map { |path|
|
76
|
+
full = File.join( File.expand_path(path), "/*")
|
77
|
+
files = Dir.glob( full, File::FNM_DOTMATCH ).select { |unk| File.file?(unk) }
|
78
|
+
}.flatten
|
79
|
+
end
|
80
|
+
|
81
|
+
def save
|
82
|
+
|
83
|
+
content = File.read(file)
|
84
|
+
standard = content.gsub("\r", '')
|
85
|
+
|
86
|
+
old = source_files.detect { |path|
|
87
|
+
raw = File.read(path)
|
88
|
+
raw.gsub("\r","") == standard
|
89
|
+
}
|
90
|
+
|
91
|
+
if !old
|
92
|
+
File.write(backup_file, content)
|
93
|
+
raise Pending, backup_file
|
94
|
+
end
|
95
|
+
|
96
|
+
end # === def
|
105
97
|
|
106
|
-
end # ===
|
98
|
+
end # === Base
|
107
99
|
|
100
|
+
include Base
|
101
|
+
|
108
102
|
end # === class Dahistory
|
data/spec/tests/Dashistory.rb
CHANGED