Class: Codily::Cli

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

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Cli

Returns a new instance of Cli.



12
13
14
# File 'lib/codily/cli.rb', line 12

def initialize(argv)
  @argv = argv.dup
end

Instance Method Details

#do_applyObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/codily/cli.rb', line 38

def do_apply
  options[:file] ||= './codily.rb'
  Dir.chdir(File.dirname(options[:file]))

  present = importer.run.root
  desired = Root.new(debug: options[:debug], service_filter: options[:target]).run_string(File.read(File.basename(options[:file])), options[:file], 1)

  require_fastly_auth!

  engine = Engine.new(fastly, present, desired, service_filter: options[:target], activate: options[:activate])

  act = engine.run(dry_run: options[:dry_run])

  0
end

#do_exportObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/codily/cli.rb', line 54

def do_export
  if options[:file] && File.exist?(options[:file])
    raise "File #{options[:file].inspect} already exists!"
  end

  require_fastly_auth!

  importer.run

  rb = Dumper.new(importer.root).ruby_code

  if options[:file]
    File.write options[:file], rb
  else
    puts rb
  end

  0
end

#do_versionObject



33
34
35
36
# File 'lib/codily/cli.rb', line 33

def do_version
  puts "Codily #{Codily::VERSION}"
  0
end

#fastlyObject



128
129
130
131
132
# File 'lib/codily/cli.rb', line 128

def fastly
  @fastly ||= begin
    Fastly.new(api_key: ENV['FASTLY_API_KEY'])
  end
end

#importerObject



74
75
76
# File 'lib/codily/cli.rb', line 74

def importer
  @importer ||= Importer.new(fastly, service_filter: @options[:target], import_targets: @options[:export_versions], debug: @options[:debug])
end

#optionsObject



78
79
80
81
82
83
84
85
86
87
# File 'lib/codily/cli.rb', line 78

def options
  @options ||= {
    file: nil,
    export_versions: {},
    debug: false,
    dry_run: false,
    apply: false,
    activate: false,
  }
end

#optparseObject



89
90
91
92
93
94
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
124
125
126
# File 'lib/codily/cli.rb', line 89

def optparse
  @optparse ||= OptionParser.new do |opt|
    opt.on('-a', '--apply') { options[:mode] = :apply }
    opt.on('-e', '--export') { options[:mode] = :export }
    opt.on('-v', '--version') { options[:mode] = :version }

    opt.on('-f PATH', '--file PATH', 'file to apply, or file path to save exported file (default to ./codily.rb on applying)') do |file|
      options[:file] = file
    end

    opt.on('-t REGEXP', '--target REGEXP', 'Filter services by name to apply or export.') do |regexp|
      options[:target] = [Regexp.new(regexp)]
    end

    opt.on('-n', '--dry-run', "Just displays the oprerations that would be performed, without actually running them.") do
      options[:dry_run] = true
    end

    opt.on('-D', '--debug', "Debug mode") do
      options[:debug] = true
    end

    opt.on('-A', '--activate', "Activate after apply") do
      options[:activate] = true
    end

    #opt.on('-d', '--diff', "Call diff API after apply") do
    #  options[:diff] = true
    #end

    opt.on('-V SVC_VER', '--target-version SVC_VER', "Choose version to export (format= service_name:version) This option can be used multiple time.") do |svcvers|
      svcver = svcvers.split(?:)
      ver = svcver.pop
      svc = svcver.join(?:)
      options[:export_versions][svc] = ver.to_i
    end
  end
end

#runObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/codily/cli.rb', line 16

def run
  optparse.parse!(@argv)
  case options[:mode]
  when :version
    do_version
  when :apply
    do_apply
  when :export
    do_export
  else
    $stderr.puts "ERROR: you should choose operation from --apply (-a), or --export (-e)"
    $stderr.puts
    $stderr.puts optparse.help
    16
  end
end