class Kojo::Commands::ToJsonCmd

Handle calls to the +kojo dir+ command

Attributes

input[R]
replace_files[R]
save_files[R]

Public Instance Methods

run() click to toggle source
# File lib/kojo/commands/to_json.rb, line 27
def run
  @input = get_input_files
  @save_files = args['--save'] || args['--replace']
  @replace_files = args['--replace']

  save_files ? write : show
end

Private Instance Methods

get_input_files() click to toggle source

Glob patterns are usually handled by the shell, but in case we still have '*' in our string (for example, if it was sent quoted), we will do the globbing ourselves

# File lib/kojo/commands/to_json.rb, line 44
def get_input_files
  args['INPUT'].map do |path|
    path.include?('*') ? Dir[path].sort : path
  end.flatten
end
show() click to toggle source
# File lib/kojo/commands/to_json.rb, line 50
def show
  input.each do |infile|
    outfile = infile.gsub(/\.ya?ml/, '.json')
    say "\n!txtgrn!# #{outfile}"
    say tojson(infile)
  end
end
tojson(path) click to toggle source
# File lib/kojo/commands/to_json.rb, line 37
def tojson(path)
  JSON.pretty_generate YAML.load_file(path)
end
write() click to toggle source
# File lib/kojo/commands/to_json.rb, line 58
def write
  input.each do |infile|
    outfile = infile.gsub(/\.ya?ml/, '.json')
    save outfile, tojson(infile)
    File.delete infile if replace_files
  end
end