lib/arison/cli.rb in arison-0.1.3 vs lib/arison/cli.rb in arison-0.2.0

- old
+ new

@@ -1,8 +1,7 @@ # -*- coding: utf-8 -*- require "thor" -require 'yaml' require 'json' require 'pp' module Arison class CLI < Thor @@ -10,25 +9,27 @@ map '--version' => :version map '-s' => :query_inline map '-f' => :query_file map '-b' => :import - class_option :profile, aliases: '-p', type: :string, default: 'default', desc: 'profile by .database.yml' + class_option :profile, aliases: '-p', type: :string, default: DEFAULT_CONFIG_PROFILE, desc: 'profile by .database.yml' class_option :pretty, aliases: '-P', type: :boolean, default: false, desc: 'pretty print' - class_option :config, aliases: '--config', type: :string, default: "#{ENV['HOME']}/.database.yml", desc: 'config file' + class_option :config, aliases: '--config', type: :string, default: DEFAULT_CONFIG_FILE_PATH, desc: 'config file' def initialize(args = [], options = {}, config = {}) super(args, options, config) @global_options = config[:shell].base.options - @config = YAML.load_file(@global_options['config']) - profile = @config[@global_options['profile']] - @core = Core.new(profile) + + if @global_options[:config] && File.exist?(@global_options[:config]) + profile = Util.get_profile(@global_options[:config], @global_options[:profile]) + @core = Core.new(profile) + end end desc 'query_inline', 'Sample task' option :query, aliases: '-q', type: :string, required: true, desc: 'query' def query_inline - puts_json @core.query(options['query']) + puts_json @core.query(options[:query]) end desc 'query_file', 'query_file' def query_file(file) puts_json @core.query(File.read(file)) @@ -38,18 +39,21 @@ def tables puts_json @core.tables end desc 'columns', 'columns' - def columns(table_name) - puts_json @core.columns_with_table_name(table_name) + option :table, aliases: '-t', type: :string, desc: 'table' + def columns + puts_json @core.columns_with_table_name(options[:table]) end - desc 'import', 'import' - def import(table) - data = @core.parse_json(STDIN.read) - @core.create_table(table, data) - @core.import(table, data) + desc 'import', 'import json data.' + option :table, aliases: '-t', type: :string, desc: 'table' + option :data, type: :hash, desc: 'buffer' + def import + data = (options[:data] ? options[:data] : nil) || Util.parse_json(STDIN.read) + data = [data] if data.class == Hash + @core.import(options[:table], data) end desc 'info', 'info' def info puts_json @config