Class: Snapsync::CLI
- Inherits:
-
Thor
- Object
- Thor
- Snapsync::CLI
- Defined in:
- lib/snapsync/cli.rb
Instance Method Summary collapse
- #auto_add(name, dir) ⇒ Object
- #auto_remove(name) ⇒ Object
- #auto_sync ⇒ Object
- #cleanup(dir) ⇒ Object
- #destroy(dir) ⇒ Object
- #init(*args) ⇒ Object
- #list(dir = nil) ⇒ Object
- #policy(dir, type, *options) ⇒ Object
- #sync(config_name, dir) ⇒ Object
- #sync_all(dir) ⇒ Object
Instance Method Details
#auto_add(name, dir) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/snapsync/cli.rb', line 212 def auto_add(name, dir) uuid, relative = partition_of(Pathname.new(dir)) conf_path = Pathname.new([:config_file]) autosync = AutoSync.new if conf_path.exist? autosync.load_config(conf_path) end exists = autosync.each_autosync_target.find do |t| t.partition_uuid == uuid && t.path.cleanpath == relative.cleanpath end if exists if !exists.name if (exists.automount ^ [:automount]) && name Snapsync.info "already exists without a name, setting the name to #{name}" elsif name Snapsync.info "already exists without a name and a different automount flag, setting the name to #{name} and updating the automount flag" else Snapsync.info "already exists with different automount flag, updating" end elsif exists.automount == [: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 = [:automount] end exists.name ||= name else autosync.add AutoSync::AutoSyncTarget.new(uuid, relative, [:automount], name) end autosync.write_config(conf_path) end |
#auto_remove(name) ⇒ Object
246 247 248 249 250 251 252 |
# File 'lib/snapsync/cli.rb', line 246 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_sync ⇒ Object
297 298 299 300 301 302 |
# File 'lib/snapsync/cli.rb', line 297 def auto_sync auto = AutoSync.new(SnapperConfig.default_config_dir) auto.load_config(Pathname.new([:config_file])) auto.run end |
#cleanup(dir) ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/snapsync/cli.rb', line 88 def cleanup(dir) target = LocalTarget.new(Pathname.new(dir)) if target.cleanup target.cleanup.cleanup(target, dry_run: [:dry_run]) else Snapsync.info "#{target.sync_policy.class.name} policy set, nothing to do" end end |
#destroy(dir) ⇒ Object
283 284 285 286 287 288 289 290 291 292 |
# File 'lib/snapsync/cli.rb', line 283 def destroy(dir) 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(*args) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/snapsync/cli.rb', line 132 def init(*args) if [:auto] && ![:all] raise ArgumentError, "cannot use --auto without --all" end if [:auto] if args.size < 2 self.class.handle_argument_error(self.class.all_commands['init'], nil, args, 2) end name, dir, *policy = *args else if args.size < 1 self.class.handle_argument_error(self.class.all_commands['init'], nil, args, 1) end dir, *policy = *args end dir = Pathname.new(dir) # Parse the policy option early to avoid breaking later begin policy = normalize_policy(policy) rescue Exception # Try to see if the user forgot to add the NAME option or added # the name option but should not have if [:auto] valid_policy = begin normalize_policy(args[1..-1]) true rescue InvalidConfiguration false end if valid_policy raise ArgumentError, "--auto is set but it seems that you did not provide a name" end else valid_policy = begin normalize_policy(args[2..-1]) true rescue InvalidConfiguration false end if valid_policy raise ArgumentError, "--auto is not set but it seems that you provided a name" end end raise ArgumentError, "invalid policy #{policy}" end dirs = Array.new if [: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 # We check that both options are set together for some added safety, # but it's checked at the top of the method if [:auto] && [:all] auto_add(name, dir) end end |
#list(dir = nil) ⇒ Object
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/snapsync/cli.rb', line 305 def list(dir = nil) 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
269 270 271 272 273 274 275 276 277 |
# File 'lib/snapsync/cli.rb', line 269 def policy(dir, type, *) # Parse the policy early to avoid breaking later LocalTarget.parse_policy(*policy) each_target(dir) do |_, target| target.change_policy(type, ) target.write_config end end |
#sync(config_name, dir) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/snapsync/cli.rb', line 66 def sync(config_name, dir) config = config_from_name(config_name) target = LocalTarget.new(Pathname.new(dir)) Sync.new(config, target, autoclean: [:autoclean]).run end |
#sync_all(dir) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/snapsync/cli.rb', line 78 def sync_all(dir) dir = Pathname.new(dir) op = SyncAll.new(dir, config_dir: SnapperConfig.default_config_dir, autoclean: [:autoclean]) op.run end |