Class: Catlass::Actions

Inherits:
Object
  • Object
show all
Includes:
Logger::Helper
Defined in:
lib/catlass/actions.rb

Instance Method Summary collapse

Methods included from Logger::Helper

debug, fatal, info, log, warn

Constructor Details

#initialize(client, converter) ⇒ Actions

Returns a new instance of Actions.



8
9
10
11
# File 'lib/catlass/actions.rb', line 8

def initialize(client, converter)
  @client = client
  @converter = converter
end

Instance Method Details

#_apply_jobs(local, remote, dry_run, options) ⇒ Object



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
# File 'lib/catlass/actions.rb', line 43

def _apply_jobs(local, remote, dry_run, options)
  local.each do |l|
    r = _choice_by_name(remote, l)
    job_name = l['attributes']['name']

    if r.nil?
      info("#{dry_run}Create the new job #{job_name.inspect}")
      @client.create_job(l) if dry_run.empty?
    else
      job_id = r['id']
      diff = Catlass::Utils.diff(@converter, r, l, options['color'])

      if diff == "\n" or diff.empty?
        info("#{dry_run}No changes '#{job_name}'")
      else
        warn("#{dry_run}Update the job #{job_name.inspect}")
        STDERR.puts diff
        @client.update_job(job_id, l) if dry_run.empty?
      end
    end

  end

  remote.each do |r|
    if _choice_by_name(local, r).nil?
      job_name = r['attributes']['name']
      warn("#{dry_run}Delete the job #{job_name.inspect}")
      @client.delete_job(r['id']) if dry_run.empty?
    end
  end
end

#_choice_by_name(jobs, target) ⇒ Object



75
76
77
78
79
80
# File 'lib/catlass/actions.rb', line 75

def _choice_by_name(jobs, target)
  jobs.each do |d|
    return d if d['attributes']['name'] == target['attributes']['name']
  end
  nil
end

#_export_dsl_file(dsl, filename) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/catlass/actions.rb', line 82

def _export_dsl_file(dsl, filename)
  dsl = "#! /usr/bin/env ruby\n\n\#{dsl}\n"
  _export_file(dsl, filename)
end

#_export_file(dsl, filename) ⇒ Object



91
92
93
94
# File 'lib/catlass/actions.rb', line 91

def _export_file(dsl, filename)
  File.write(filename, dsl)
  info("Write #{filename.inspect}")
end

#apply(options) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/catlass/actions.rb', line 34

def apply(options)
  Catlass::TermColor.color = options['color']
  dry_run = options['dry_run'] ? '[Dry run] ' : ''
  local = @converter.dslfile_to_h(options['file'])
  remote = @client.get_jobs

  _apply_jobs(local, remote, dry_run, options)
end

#export(options) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/catlass/actions.rb', line 13

def export(options)
  Catlass::TermColor.color = options['color']
  jobs = @client.get_jobs

  if options['write']
    if options['split']
      content = ''
      jobs.each do |job|
        name = @converter.filename(job['attributes']['name'])
        _export_dsl_file(@converter.to_dsl(job), "#{name}.rb")
        content << "require #{name.inspect}\n"
      end
      _export_dsl_file(content, options['file'])
    else
      _export_dsl_file(@converter.to_dsl_all(jobs), options['file'])
    end
  else
    Catlass::Utils.print_ruby(@converter.to_dsl_all(jobs), color: options['color'])
  end
end