class Snapsync::CLI
Public Instance Methods
auto_add(name, dir)
click to toggle source
# File lib/snapsync/cli.rb, line 206 def auto_add(name, dir) uuid, relative = partition_of(Pathname.new(dir)) conf_path = Pathname.new(options[: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 ^ options[: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 == 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)
click to toggle source
# File lib/snapsync/cli.rb, line 240 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()
click to toggle source
# File lib/snapsync/cli.rb, line 293 def auto_sync handle_class_options auto = AutoSync.new(SnapperConfig.default_config_dir) auto.load_config(Pathname.new(options[:config_file])) if options[:one_shot] auto.sync else auto.run end end
cleanup(dir)
click to toggle source
# File lib/snapsync/cli.rb, line 88 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
config_from_name(name)
click to toggle source
# File lib/snapsync/cli.rb, line 9 def config_from_name(name) path = Pathname.new(name) if !path.exist? path = SnapperConfig.default_config_dir + path if !path.exist? raise ArgumentError, "cannot find any snapper configuration called #{name}" end end SnapperConfig.load(path) end
destroy(dir)
click to toggle source
# File lib/snapsync/cli.rb, line 277 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
each_target(dir = nil) { |nil, local_target(dir, create_if_needed: false)| ... }
click to toggle source
Resolves a path (or nil) into a list of snapsync targets and yields them
@param [String,nil] dir the path the user gave, or nil if all
available auto-sync paths should be processed. If the directory is a target, it is yield as-is. It can also be the root of a sync-all target (with proper snapsync target as subdirectories whose name matches the snapper configurations)
@yieldparam [LocalTarget] target
# File lib/snapsync/cli.rb, line 36 def each_target(dir = nil) return enum_for(__method__) if !block_given? if dir dir = Pathname.new(dir) begin return yield(nil, LocalTarget.new(dir, create_if_needed: false)) rescue LocalTarget::InvalidTargetPath end SyncAll.new(dir).each_target do |config, target| yield(config, target) end else autosync = AutoSync.load_default autosync.each_available_target do |config, target| yield(config, target) end end end
handle_class_options()
click to toggle source
# File lib/snapsync/cli.rb, line 20 def handle_class_options if options[:debug] Snapsync.logger.level = 'DEBUG' end end
init(*args)
click to toggle source
# File lib/snapsync/cli.rb, line 132 def init(*args) if options[:auto] && !options[:all] raise ArgumentError, "cannot use --auto without --all" end if options[: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 => policy_validation_error # Try to see if the user forgot to add the NAME option or added # the name option but should not have if (args.size > 1) && options[:auto] begin normalize_policy(args[1..-1]) raise ArgumentError, "--auto is set but it seems that you did not provide a name" rescue InvalidConfiguration end elsif args.size > 2 begin normalize_policy(args[2..-1]) raise ArgumentError, "--auto is not set but it seems that you provided a name" rescue InvalidConfiguration end end raise policy_validation_error 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 # We check that both options are set together for some added safety, # but it's checked at the top of the method if options[:auto] && options[:all] auto_add(name, dir) end end
list(dir = nil)
click to toggle source
# File lib/snapsync/cli.rb, line 305 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
normalize_policy(args)
click to toggle source
# File lib/snapsync/cli.rb, line 100 def normalize_policy(args) policy = if args.empty? ['default', Array.new] elsif args.size == 1 args + [Array.new] else [args.shift, args] end LocalTarget.parse_policy(*policy) return *policy end
partition_of(dir)
click to toggle source
# File lib/snapsync/cli.rb, line 56 def partition_of(dir) partitions = PartitionsMonitor.new PartitionsMonitor.new.partition_of(dir) end
policy(dir, type, *options)
click to toggle source
# File lib/snapsync/cli.rb, line 263 def policy(dir, type, *options) handle_class_options # Parse the policy early to avoid breaking later policy = normalize_policy([type, *options]) each_target(dir) do |_, target| target.change_policy(*policy) target.write_config end end
sync(config_name, dir)
click to toggle source
# File lib/snapsync/cli.rb, line 66 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)
click to toggle source
# File lib/snapsync/cli.rb, line 78 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