Sha256: 86740a7838d0142ad5607f58e6fc88e9d239e66413a1eb9fbe09d227bd4cb596

Contents?: true

Size: 1.45 KB

Versions: 3

Compression:

Stored size: 1.45 KB

Contents

#!/usr/bin/env ruby
# frozen_string_literal: true

require_relative '../lib/tabled'
require_relative '../lib/parsers/csv_parser'
require_relative '../lib/parsers/json_parser'
require 'dry/cli'
require 'pathname'

module TabledCli
  module CLI
    module Commands
      extend Dry::CLI::Registry

      class Print < Dry::CLI::Command
        desc 'Printing CSV to console'

        argument :input, desc: 'Input file to print'

        def call(input: nil, **)
          return p 'File must be provided' if input.nil?
          return p 'File doesn\'t exist' unless File.exist?(Pathname.new(input))
          return p 'Unsupported file format' unless %w[.csv .json].include?(File.extname(Pathname.new(input)).downcase)

          file = Pathname.new(input)

          $stdout << case File.extname(file).downcase
                     when '.csv'
                       print_csv(file)
                     when '.json'
                       print_json(file)
                     end
        end

        def print_csv(input)
          Tabled
            .new(Tabled::CSVParser.parse(Pathname.new(input)))
            .print_to_console
        end

        def print_json(input)
          titles, values = Tabled::JSONParser.parse(Pathname.new(input))

          Tabled
            .new(values, titles: titles)
            .print_to_console
        end
      end

      register 'print', Print, aliases: %w[p -p --print]
    end
  end
end

Dry::CLI.new(TabledCli::CLI::Commands).call

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tabled-1.2.1 bin/tabled
tabled-1.2.0 bin/tabled
tabled-1.1.0 bin/tabled