Class: Codily::Cli
- Inherits:
-
Object
- Object
- Codily::Cli
- Defined in:
- lib/codily/cli.rb
Instance Method Summary collapse
- #do_apply ⇒ Object
- #do_export ⇒ Object
- #do_version ⇒ Object
- #fastly ⇒ Object
- #importer ⇒ Object
-
#initialize(argv) ⇒ Cli
constructor
A new instance of Cli.
- #options ⇒ Object
- #optparse ⇒ Object
- #run ⇒ Object
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_apply ⇒ Object
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 [:file] ||= './codily.rb' Dir.chdir(File.dirname([:file])) present = importer.run.root desired = Root.new(debug: [:debug], service_filter: [:target]).run_string(File.read(File.basename([:file])), [:file], 1) require_fastly_auth! engine = Engine.new(fastly, present, desired, service_filter: [:target], activate: [:activate]) act = engine.run(dry_run: [:dry_run]) 0 end |
#do_export ⇒ Object
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 [:file] && File.exist?([:file]) raise "File #{[:file].inspect} already exists!" end require_fastly_auth! importer.run rb = Dumper.new(importer.root).ruby_code if [:file] File.write [:file], rb else puts rb end 0 end |
#do_version ⇒ Object
33 34 35 36 |
# File 'lib/codily/cli.rb', line 33 def do_version puts "Codily #{Codily::VERSION}" 0 end |
#fastly ⇒ Object
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 |
#importer ⇒ Object
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 |
#options ⇒ Object
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/codily/cli.rb', line 78 def @options ||= { file: nil, export_versions: {}, debug: false, dry_run: false, apply: false, activate: false, } end |
#optparse ⇒ Object
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') { [:mode] = :apply } opt.on('-e', '--export') { [:mode] = :export } opt.on('-v', '--version') { [: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| [:file] = file end opt.on('-t REGEXP', '--target REGEXP', 'Filter services by name to apply or export.') do |regexp| [:target] = [Regexp.new(regexp)] end opt.on('-n', '--dry-run', "Just displays the oprerations that would be performed, without actually running them.") do [:dry_run] = true end opt.on('-D', '--debug', "Debug mode") do [:debug] = true end opt.on('-A', '--activate', "Activate after apply") do [: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(?:) [:export_versions][svc] = ver.to_i end end end |
#run ⇒ Object
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 [: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 |