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



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/snapsync/cli.rb', line 115

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_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



141
142
143
144
145
146
147
# File 'lib/snapsync/cli.rb', line 141

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



195
196
197
198
199
# File 'lib/snapsync/cli.rb', line 195

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

#cleanup(dir) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/snapsync/cli.rb', line 49

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



181
182
183
184
185
186
187
188
189
190
# File 'lib/snapsync/cli.rb', line 181

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/snapsync/cli.rb', line 72

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

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



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/snapsync/cli.rb', line 164

def policy(dir, type, *options)
    handle_class_options

    dir = Pathname.new(dir)
    if !dir.exist?
        dir.mkpath
    end

    target = LocalTarget.new(dir)
    target.change_policy(type, options)
    target.write_config
end

#sync(config_name, dir) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/snapsync/cli.rb', line 28

def sync(config_name, dir)
    handle_class_options

    config = config_from_name(config_name)
    target = LocalTarget.new(Pathname.new(dir))
    LocalSync.new(config, target).sync
end

#sync_all(dir) ⇒ Object



40
41
42
43
44
45
# File 'lib/snapsync/cli.rb', line 40

def sync_all(dir)
    handle_class_options

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