Class: Remocon::Command::Pull

Inherits:
Object
  • Object
show all
Includes:
InterpreterHelper
Defined in:
lib/remocon/command/pull_command.rb

Defined Under Namespace

Classes: RemoteConfig

Constant Summary

Constants included from ParameterSorter

ParameterSorter::PARAMETER_KEYS

Constants included from Remocon::ConditionSorter

Remocon::ConditionSorter::CONDITION_KEYS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from InterpreterHelper

#condition_array, #condition_names, #parameter_hash, #read_conditions, #read_parameters

Methods included from ParameterSorter

#sort_parameters

Methods included from Remocon::ConditionSorter

#sort_conditions

Constructor Details

#initialize(opts) ⇒ Pull

Returns a new instance of Pull.



36
37
38
39
40
# File 'lib/remocon/command/pull_command.rb', line 36

def initialize(opts)
  @config = Remocon::Config.new(opts)
  @cmd_opts = { validate_only: false }
  @left = RemoteConfig.new(opts)
end

Instance Attribute Details

#cmd_optsObject (readonly)

Returns the value of attribute cmd_opts.



34
35
36
# File 'lib/remocon/command/pull_command.rb', line 34

def cmd_opts
  @cmd_opts
end

#configObject (readonly)

Returns the value of attribute config.



34
35
36
# File 'lib/remocon/command/pull_command.rb', line 34

def config
  @config
end

#leftObject (readonly)

Returns the value of attribute left.



34
35
36
# File 'lib/remocon/command/pull_command.rb', line 34

def left
  @left
end

Instance Method Details

#conditions_diff(left, right) ⇒ Object



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
# File 'lib/remocon/command/pull_command.rb', line 74

def conditions_diff(left, right)
  left_names = left.map { |c| c[:name] }
  right_names = right.map { |c| c[:name] }

  added_names = right_names - left_names
  removed_names = left_names - right_names

  added = added_names.each_with_object([]) do |k, acc|
    acc.push(right.find { |c| c[:name] == k })
  end

  changed = []
  unchanged = []

  (right_names & left_names).each do |k|
    old = left.find { |c| c[:name] == k }
    new = Remocon::ConditionFileDumper.new(right.find { |c| c[:name] == k }).dump

    if old == new
      unchanged.push(old)
    else
      changed.push(new)
    end
  end

  removed = []

  removed_names.each do |k|
    removed.push(left.find { |c| c[:name] == k })
  end

  [unchanged, added, changed, removed]
end

#parameters_diff(left, right) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/remocon/command/pull_command.rb', line 108

def parameters_diff(left, right)
  added_keys = right.keys - left.keys
  removed_keys = left.keys - right.keys

  added = added_keys.each_with_object({}) do |k, acc|
    acc.merge!(Remocon::ParameterFileDumper.new({ k => right[k] }).dump)
  end

  changed = {}
  unchanged = {}

  (right.keys & left.keys).each do |k|
    old = { k => left[k] }
    new = Remocon::ParameterFileDumper.new({ k => right[k] }).dump

    if old == new
      unchanged.merge!(old)
    else
      changed.merge!(new)
    end
  end

  removed = {}

  removed_keys.each do |k|
    removed[k] = left[k]
  end

  [unchanged, added, changed, removed]
end

#require_conditions_file_pathObject



46
47
48
# File 'lib/remocon/command/pull_command.rb', line 46

def require_conditions_file_path
  config.conditions_file_path
end

#require_parameters_file_pathObject



42
43
44
# File 'lib/remocon/command/pull_command.rb', line 42

def require_parameters_file_path
  config.parameters_file_path
end

#runObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/remocon/command/pull_command.rb', line 50

def run
  raw_json, etag = Remocon::Request.pull(config)

  raw_hash = JSON.parse(raw_json).with_indifferent_access

  raise "etag cannot be fetched. please try again" unless etag

  conditions = raw_hash[:conditions] || []
  parameters = raw_hash[:parameters] || {}

  if config.merge? && File.exist?(config.parameters_file_path) && File.exist?(config.parameters_file_path)
    unchanged_conditions, added_conditions, changed_conditions, = conditions_diff(left.raw_conditions, conditions)
    unchanged_parameters, added_parameters, changed_parameters, = parameters_diff(left.raw_parameters, parameters)

    conditions_yaml = JSON.parse(sort_conditions(unchanged_conditions + added_conditions + changed_conditions).to_json).to_yaml
    parameters_yaml = JSON.parse(sort_parameters(unchanged_parameters.merge(added_parameters).merge(changed_parameters)).to_json).to_yaml
  else
    conditions_yaml = JSON.parse(Remocon::ConditionFileDumper.new(sort_conditions(conditions)).dump.to_json).to_yaml
    parameters_yaml = JSON.parse(Remocon::ParameterFileDumper.new(sort_parameters(parameters)).dump.to_json).to_yaml
  end

  write_to_files(conditions_yaml, parameters_yaml, etag)
end