Class: Snapsync::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/snapsync/cli.rb

Instance Method Summary collapse

Instance Method Details

#auto_add(name, dir) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/snapsync/cli.rb', line 149

def auto_add(name, dir)
    partitions = PartitionsMonitor.new
    uuid, relative = partitions.partition_of(Pathname.new(dir))

    conf_path = Pathname.new('/etc/snapsync.conf')

    autosync = AutoSync.new
    autosync.load_config(conf_path)
    exists = autosync.each_autosync_target.find do |t|
        t.partition_uuid == uuid && t.path.cleanpath == relative.cleanpath
    end
    if exists
        if exists.automount == options[:automount]
            Snapsync.info "already exists under the name #{exists.name}"
        else
            Snapsync.info "already exists under the name #{exists.name} but with a different automount flag, changing"
            exists.automount = options[:automount]
        end
        exists.name ||= name
    else
        autosync.add AutoSync::AutoSyncTarget.new(uuid, relative, options[:automount], name)
    end
    autosync.write_config(conf_path)
end

#auto_remove(name) ⇒ Object



175
176
177
178
179
180
181
# File 'lib/snapsync/cli.rb', line 175

def auto_remove(name)
    conf_path = Pathname.new('/etc/snapsync.conf')
    autosync = AutoSync.new
    autosync.load_config(conf_path)
    autosync.remove(name: name)
    autosync.write_config(conf_path)
end

#auto_syncObject



224
225
226
227
228
229
# File 'lib/snapsync/cli.rb', line 224

def auto_sync
    handle_class_options
    auto = AutoSync.new(SnapperConfig.default_config_dir)
    auto.load_config(Pathname.new(options[:config_file]))
    auto.run
end

#cleanup(dir) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/snapsync/cli.rb', line 83

def cleanup(dir)
    handle_class_options

    target = LocalTarget.new(Pathname.new(dir))
    if target.cleanup
        target.cleanup.cleanup(target, dry_run: options[:dry_run])
    else
        Snapsync.info "#{target.sync_policy.class.name} policy set, nothing to do"
    end
end

#destroy(dir) ⇒ Object



210
211
212
213
214
215
216
217
218
219
# File 'lib/snapsync/cli.rb', line 210

def destroy(dir)
    handle_class_options
    target_dir = Pathname.new(dir)
    target = LocalTarget.new(target_dir, create_if_needed: false)
    snapshots = target.each_snapshot.to_a
    snapshots.sort_by(&:num).each do |s|
        target.delete(s)
    end
    target_dir.rmtree
end

#init(name, dir, *policy) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/snapsync/cli.rb', line 106

def init(name, dir, *policy)
    dir = Pathname.new(dir)

    if policy.empty?
        policy = ['default', Array.new]
    elsif policy.size == 1
        policy << Array.new
    end

    dirs = Array.new
    if options[:all]
        SnapperConfig.each_in_dir do |config|
            dirs << dir + config.name
        end
    else
        dirs << dir
    end

    dirs.each do |path|
        begin
            LocalTarget.new(path, create_if_needed: false)
            Snapsync.info "#{path} was already initialized"
        rescue ArgumentError, LocalTarget::NoUUIDError
            path.mkpath
            target = LocalTarget.new(path)
            target.change_policy(*policy)
            target.write_config
            Snapsync.info "initialized #{path} as a snapsync target"
        end
    end

    if options[:auto]
        if !options[:all]
            Snapsync.warn "cannot use --auto without --all"
        else
            auto_add(name, dir)
        end
    end
end

#list(dir = nil) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/snapsync/cli.rb', line 232

def list(dir = nil)
    handle_class_options
    each_target(dir) do |_, target|
        puts "== #{target.dir}"
        puts "UUID: #{target.uuid}"
        puts "Enabled: #{target.enabled?}"
        puts "Autoclean: #{target.autoclean?}"
        print "Policy: "
        pp target.sync_policy

        puts "Snapshots:"
        target.each_snapshot do |s|
            puts "  #{s.num} #{s.to_time}"
        end
    end
end

#policy(dir, type, *options) ⇒ Object



198
199
200
201
202
203
204
# File 'lib/snapsync/cli.rb', line 198

def policy(dir, type, *options)
    handle_class_options
    each_target(dir) do |_, target|
        target.change_policy(type, options)
        target.write_config
    end
end

#sync(config_name, dir) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/snapsync/cli.rb', line 61

def sync(config_name, dir)
    handle_class_options

    config = config_from_name(config_name)
    target = LocalTarget.new(Pathname.new(dir))
    Sync.new(config, target, autoclean: options[:autoclean]).run
end

#sync_all(dir) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/snapsync/cli.rb', line 73

def sync_all(dir)
    handle_class_options

    dir = Pathname.new(dir)
    op = SyncAll.new(dir, config_dir: SnapperConfig.default_config_dir, autoclean: options[:autoclean])
    op.run
end