Class: Constancy::CLI::PullCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/constancy/cli/pull_command.rb

Class Method Summary collapse

Class Method Details

.pull_dir(diff) ⇒ Object



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
85
86
87
88
89
90
91
92
93
# File 'lib/constancy/cli/pull_command.rb', line 41

def pull_dir(diff)
  diff.items_to_change.each do |item|
    case item.op
    when :create
      print "CREATE".bold.green + " " + item.display_filename
      begin
        FileUtils.mkdir_p(File.dirname(item.filename))
        # attempt to write atomically-ish
        tmpfile = item.filename + ".constancy-tmp"
        File.open(tmpfile, "w") do |f|
          f.write(item.remote_content)
        end
        FileUtils.move(tmpfile, item.filename)
        puts "   OK".bold
      rescue => e
        puts "   ERROR".bold.red
        puts "  #{e}"
      end

    when :update
      print "UPDATE".bold.blue + " " + item.display_filename
      begin
        # attempt to write atomically-ish
        tmpfile = item.filename + ".constancy-tmp"
        File.open(tmpfile, "w") do |f|
          f.write(item.remote_content)
        end
        FileUtils.move(tmpfile, item.filename)
        puts "   OK".bold
      rescue => e
        puts "   ERROR".bold.red
        puts "  #{e}"
      end

    when :delete
      print "DELETE".bold.red + " " + item.display_filename
      begin
        File.unlink(item.filename)
        puts "   OK".bold
      rescue => e
        puts "   ERROR".bold.red
        puts "  #{e}"
      end

    else
      if Constancy.config.verbose?
        STDERR.puts "constancy: WARNING: unexpected operation '#{item.op}' for #{item.display_filename}"
        next
      end

    end
  end
end

.pull_file(diff) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/constancy/cli/pull_command.rb', line 95

def pull_file(diff)
  # build and write the file
  filename_list = diff.items_to_change.collect(&:filename).uniq
  if filename_list.length != 1
    raise Constancy::InternalError.new("Multiple filenames found for a 'file' type sync target. Something has gone wrong.")
  end
  filename = filename_list.first
  display_filename = filename.trim_path

  if File.exist?(filename)
    print "UPDATE".bold.blue + " " + display_filename
  else
    print "CREATE".bold.green + " " + display_filename
  end

  begin
    FileUtils.mkdir_p(File.dirname(filename))
    # attempt to write atomically-ish
    tmpfile = filename + ".constancy-tmp"
    File.open(tmpfile, "w") do |f|
      f.write(diff.final_items.to_yaml)
    end
    FileUtils.move(tmpfile, filename)
    puts "   OK".bold
  rescue => e
    puts "   ERROR".bold.red
    puts "  #{e}"
  end
end

.run(args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/constancy/cli/pull_command.rb', line 7

def run(args)
  Constancy::CLI.configure
  STDOUT.sync = true

  Constancy.config.sync_targets.each do |target|
    diff = target.diff(:pull)

    diff.print_report

    if not diff.any_changes?
      puts
      puts "Everything is in sync. No changes need to be made to this sync target."
      next
    end

    puts
    puts "Do you want to pull these changes?"
    print "  Enter '" + "yes".bold + "' to continue: "
    answer = args.include?('--yes') ? 'yes' : gets.chomp

    if answer.downcase != "yes"
      puts
      puts "Pull cancelled. No changes will be made to this sync target."
      next
    end

    puts
    case target.type
    when :dir then self.pull_dir(diff)
    when :file then self.pull_file(diff)
    end
  end
end