|
| 1 | +#!/usr/bin/env ruby |
| 2 | +require 'thor' |
| 3 | +require 'yaml' |
| 4 | +require 'colorize' |
| 5 | +require_relative '../lib/alterant' |
| 6 | + |
| 7 | +module Alterant |
| 8 | + class AlterantCLI < Thor |
| 9 | + package_name "alterant" |
| 10 | + |
| 11 | + desc "version", "Show Alterant version" |
| 12 | + def version |
| 13 | + say "#{::Alterant::APP_NAME} #{::Alterant::VERSION}\n#{::Alterant::COPYRIGHT_MESSAGE}" |
| 14 | + end |
| 15 | + |
| 16 | + desc "update", "Update Alterant to the latest version" |
| 17 | + option :version, type: :string, desc: "Force a specific version" |
| 18 | + def update |
| 19 | + say "Updating Alterant..." |
| 20 | + unless options[:version] |
| 21 | + say `gem install alterant --no-ri --no-rdoc` |
| 22 | + else |
| 23 | + say `gem install alterant -v #{options[:version]} --no-ri --no-rdoc` |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + desc "modify", "Runs the given script against a file" |
| 28 | + option :modifier, type: :string, desc: "Alterant JS script" |
| 29 | + option :in, type: :string, desc: "Input configuration file" |
| 30 | + option :out, type: :string, desc: "Output configuration file" |
| 31 | + option :output_format, type: :string, enum: ['yaml', 'json'], default: 'yaml', desc: "Output format" |
| 32 | + option :diff, type: :boolean, default: false, desc: "Return the diff instead of the output itself" |
| 33 | + option :debug, type: :boolean, default: false |
| 34 | + option :input_format, type: :string, enum: ['yaml', 'json'], default: 'yaml', desc: "Input format if it's based on a stream and not a file" |
| 35 | + option :overwrite, type: :boolean, default: false |
| 36 | + def modify |
| 37 | + $debug = options[:debug] || false |
| 38 | + overwrite = options[:overwrite] |
| 39 | + diff = options[:diff] |
| 40 | + output_format = options[:output_format] |
| 41 | + input_format = options[:input_format] |
| 42 | + |
| 43 | + in_file = options[:in] |
| 44 | + if !in_file |
| 45 | + STDERR.puts "No input file provided. Use --in option".red |
| 46 | + exit(1) |
| 47 | + end |
| 48 | + if in_file != '-' |
| 49 | + unless File.exists?(in_file) |
| 50 | + STDERR.puts "Input file #{in_file} not found".red |
| 51 | + exit(1) |
| 52 | + end |
| 53 | + # detect the type |
| 54 | + input_ext = File.extname(in_file) |
| 55 | + unless ['.yaml', '.yml', '.json'].include? input_ext |
| 56 | + STDERR.puts "Only yaml and json files are supported for input".red |
| 57 | + exit(1) |
| 58 | + end |
| 59 | + if ['.yaml', '.yml'].include?(input_ext) |
| 60 | + input_format = 'yaml' |
| 61 | + else |
| 62 | + input_format = 'json' |
| 63 | + end |
| 64 | + |
| 65 | + output_format = input_format if !output_format |
| 66 | + end |
| 67 | + |
| 68 | + modifier_file = options[:modifier] |
| 69 | + if !modifier_file |
| 70 | + STDERR.puts "No script file provided. Use --modifier option".red |
| 71 | + exit(1) |
| 72 | + end |
| 73 | + unless File.exists?(modifier_file) |
| 74 | + STDERR.puts "Modifier file #{modifier_file} not found".red |
| 75 | + exit(1) |
| 76 | + end |
| 77 | + |
| 78 | + out_file = options[:out] |
| 79 | + if out_file |
| 80 | + if !overwrite && File.exists?(out_file) |
| 81 | + STDERR.puts "Output file #{out_file} already exists. Use --overwrite flag to overwrite it".red |
| 82 | + exit(1) |
| 83 | + end |
| 84 | + else |
| 85 | + out_file = '-' # output to stdout |
| 86 | + end |
| 87 | + |
| 88 | + |
| 89 | + data = [] |
| 90 | + if in_file == '-' |
| 91 | + input_text = STDIN.read |
| 92 | + else |
| 93 | + input_text = File.read(in_file) |
| 94 | + end |
| 95 | + |
| 96 | + if input_format == 'yaml' |
| 97 | + if input_text.empty? |
| 98 | + STDERR.puts "Empty input file".red |
| 99 | + exit(2) |
| 100 | + end |
| 101 | + |
| 102 | + input_text.split('---').each_with_index do |part, idx| |
| 103 | + part_data = YAML.load(part) |
| 104 | + data << part_data |
| 105 | + end |
| 106 | + |
| 107 | + else |
| 108 | + file_data = JSON.parse(input_text) |
| 109 | + if file_data.is_a? Array |
| 110 | + data = file_data |
| 111 | + else |
| 112 | + data = [file_data] |
| 113 | + end |
| 114 | + end |
| 115 | + |
| 116 | + run_context = {} |
| 117 | + modifier = File.read(modifier_file) |
| 118 | + if in_file != '-' |
| 119 | + run_context[:basedir] = File.dirname(in_file) |
| 120 | + end |
| 121 | + |
| 122 | + run_context[:js_preload] = ::Alterant::Classes.LoadClasses |
| 123 | + alter = ::Alterant::Alterant.new(input: data, modifier: modifier, filename: modifier_file, options: run_context) |
| 124 | + results = alter.execute(timeout: 500) |
| 125 | + |
| 126 | + if results.nil? |
| 127 | + STDERR.puts "Aborting".red |
| 128 | + exit(2) |
| 129 | + end |
| 130 | + |
| 131 | + if output_format == 'yaml' || output_format == 'yml' |
| 132 | + converted_as_text = results.map { |r| r.to_yaml }.join("\n") |
| 133 | + input = data.map { |r| r.to_yaml }.join("\n") |
| 134 | + elsif output_format == 'json' |
| 135 | + converted_as_text = JSON.pretty_generate(results) |
| 136 | + input = JSON.pretty_generate(data) |
| 137 | + end |
| 138 | + |
| 139 | + if diff |
| 140 | + output_as_text = Diffy::Diff.new(input, converted_as_text) |
| 141 | + else |
| 142 | + output_as_text = converted_as_text |
| 143 | + end |
| 144 | + |
| 145 | + if out_file == "-" |
| 146 | + puts output_as_text |
| 147 | + else |
| 148 | + File.open(out_file, 'w') do |file| |
| 149 | + file.write(output_as_text) |
| 150 | + end |
| 151 | + end |
| 152 | + return true |
| 153 | + rescue ::Alterant::ParseError => exc |
| 154 | + STDERR.puts "Syntax error: #{exc.message}".red |
| 155 | + return false |
| 156 | + rescue ::Alterant::RuntimeError => exc |
| 157 | + STDERR.puts "Runtime error: #{exc.message}".red |
| 158 | + return false |
| 159 | + rescue => exc |
| 160 | + if $debug |
| 161 | + raise |
| 162 | + else |
| 163 | + STDERR.puts exc |
| 164 | + end |
| 165 | + return false |
| 166 | + end |
| 167 | + end |
| 168 | + |
| 169 | + AlterantCLI.start |
| 170 | +end |
0 commit comments