lib/beerdb/cli/main.rb in beerdb-0.9.13 vs lib/beerdb/cli/main.rb in beerdb-0.10.0

- old
+ new

@@ -1,28 +1,62 @@ # encoding: utf-8 -require 'gli' - -include GLI::App +### NOTE: wrap gli config into a class +## see github.com/davetron5000/gli/issues/153 -require 'logutils/db' # add support for logging to db -require 'beerdb/cli/opts' +module BeerDb + class Tool + def initialize + LogUtils::Logger.root.level = :info # set logging level to info + end + def run( args ) + puts BeerDbCli.banner + Toolii.run( args ) + end + end -program_desc 'beer.db command line tool' -version BeerDb::VERSION + class Toolii + extend GLI::App -LogUtils::Logger.root.level = :info # set logging level to info + def self.logger=(value) @@logger=value; end + def self.logger() @@logger; end + + ## todo: find a better name e.g. change to settings? config? safe_opts? why? why not? + def self.opts=(value) @@opts = value; end + def self.opts() @@opts; end + + def self.connect_to_db( options ) + puts "working directory: #{Dir.pwd}" + + db_config = { + adapter: 'sqlite3', + database: "#{options.db_path}/#{options.db_name}" + } + + puts "Connecting to db using settings: " + pp db_config + + ActiveRecord::Base.establish_connection( db_config ) + + LogDb.setup # start logging to db (that is, save logs in logs table in db) + end + + + logger = LogUtils::Logger.root +opts = BeerDb::Opts.new -opts = BeerDb::Opts.new +program_desc 'beer.db command line tool' +version BeerDbCli::VERSION + ### global option (required) ## todo: add check that path is valid?? possible? desc 'Database path' arg_name 'PATH' @@ -40,29 +74,10 @@ desc 'Only show warnings, errors and fatal messages' switch [:q, :quiet], negatable: false -def connect_to_db( options ) - puts BeerDb.banner - - puts "working directory: #{Dir.pwd}" - - db_config = { - :adapter => 'sqlite3', - :database => "#{options.db_path}/#{options.db_name}" - } - - puts "Connecting to db using settings: " - pp db_config - - ActiveRecord::Base.establish_connection( db_config ) - - LogDb.setup # turn on logging to db -end - - desc 'Create DB schema' command [:create] do |c| c.desc 'Extra tables (notes,drinks,marks,users)' c.switch [:extras], negatable: false @@ -76,15 +91,11 @@ BeerDb::CreateDbExtrasUsers.new.up BeerDb::CreateDbExtrasBookmarks.new.up BeerDb::CreateDbExtrasNotes.new.up BeerDb::CreateDbExtrasDrinks.new.up else - LogDb.create - ConfDb.create - TagDb.create - WorldDb.create - BeerDb.create + BeerDb.create_all end puts 'Done.' end # action end # command create @@ -107,15 +118,11 @@ connect_to_db( opts ) ## todo: document optional setup profile arg (defaults to all) setup = args[0] || 'all' - LogDb.create - ConfDb.create - TagDb.create - WorldDb.create - BeerDb.create + BeerDb.create_all WorldDb.read_all( opts.world_data_path ) BeerDb.read_setup( "setups/#{setup}", opts.data_path ) puts 'Done.' end # action @@ -309,6 +316,9 @@ false # skip default error handling end -exit run(ARGV) \ No newline at end of file +### exit run(ARGV) ## note: use Toolii.run( ARGV ) outside of class + + end # class Toolii +end # module BeerDb