Class: HomeSync::CLI

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

Instance Method Summary collapse

Instance Method Details

#setupObject



17
18
19
20
# File 'lib/homesync/cli.rb', line 17

def setup
  update_homesync_config if options[:homesync_path]
  update_launch_agent
end

#sync(path) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/homesync/cli.rb', line 32

def sync(path)
  if options[:overwrite_local] and options[:overwrite_homesync]
    error "--overwrite-local and --overwrite-homesync cannot be used together"
  end

  path = Pathname.new(path).expand_path
  relative_from_home = path.relative_path_from(home_path)

  if relative_from_home.to_s =~ %r{^../}
    error "#{path} is not inside your home directory"
  end

  sync_path = homesync_path.join(relative_from_home)

  if path.symlink?
    target = path.readlink
    target = path.dirname.realpath.join(target) if target.relative?
    if target == sync_path
      error "#{path} is already syncing"
    else
      error "#{path} is a symlink pointing somewhere else than its place in #{homesync_path}"
    end
  end

  if path.exist?
    if sync_path.exist?
      if options[:overwrite_local] or (not options[:overwrite_homesync] and shell.file_collision(path))
        if path.file? then path.unlink else path.rmtree end
        path.make_symlink(sync_path.to_s)
        say "Replaced #{path} with symlink to #{sync_path}"
      elsif options[:overwrite_homesync] or shell.file_collision(sync_path)
        if sync_path.file? then sync_path.unlink else sync_path.rmtree end
        path.rename(sync_path)
        path.make_symlink(sync_path.to_s)
        say "Replaced #{sync_path} with local version and created symlink to it"
      else
        say "Kept both versions, syncing has been cancelled"
      end
    else
      sync_path.dirname.mkpath
      path.rename(sync_path)
      path.make_symlink(sync_path.to_s)
      say "Moved #{path} to HomeSync and created a symlink to it"
    end
  else
    if sync_path.exist?
      path.make_symlink(sync_path.to_s)
      say "Created link to #{sync_path}"
    else
      error "#{path} doesn't exist"
    end
  end
end