From a89b337f972c2d2c9da2e9819fe5a023c672dea5 Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Fri, 11 Oct 2019 13:17:26 -0700 Subject: [PATCH] Switch to mixlib-log Reduce the number of dependencies in our overall stack and use the same logging mechanism we use in other Chef CLI tools. Signed-off-by: Tim Smith --- lib/stove.rb | 14 ++++++++------ lib/stove/cli.rb | 14 ++++++-------- lib/stove/config.rb | 3 +-- lib/stove/cookbook.rb | 2 -- lib/stove/cookbook/metadata.rb | 4 ++-- lib/stove/filter.rb | 4 +--- lib/stove/log.rb | 24 ++++++++++++++++++++++++ lib/stove/packager.rb | 2 -- lib/stove/plugins/base.rb | 4 +--- lib/stove/plugins/git.rb | 8 ++++---- lib/stove/runner.rb | 6 ++---- lib/stove/validator.rb | 8 +++----- stove.gemspec | 4 ++-- 13 files changed, 54 insertions(+), 43 deletions(-) create mode 100644 lib/stove/log.rb diff --git a/lib/stove.rb b/lib/stove.rb index 4149a02..58a11ed 100644 --- a/lib/stove.rb +++ b/lib/stove.rb @@ -1,4 +1,3 @@ -require 'logify' require 'pathname' module Stove @@ -15,6 +14,7 @@ module Stove autoload :Util, 'stove/util' autoload :Validator, 'stove/validator' autoload :VERSION, 'stove/version' + autoload :Log, 'stove/log' module Middleware autoload :ChefAuthentication, 'stove/middlewares/chef_authentication' @@ -53,7 +53,7 @@ module Plugin class << self # - # The source root of the ChefAPI gem. This is useful when requiring files + # The source root of the Stove gem. This is useful when requiring files # that are relative to the root of the project. # # @return [Pathname] @@ -66,13 +66,15 @@ def root # Set the log level. # # @example Set the log level to :info - # ChefAPI.log_level = :info + # Stove.log_level = :info # # @param [#to_sym] level # the log level to set # - def log_level=(level) - Logify.level = level.to_sym + def log_level=(lev) + puts lev.to_sym.class + puts lev.to_sym + Stove::Log.level = (lev.to_sym) end # @@ -81,7 +83,7 @@ def log_level=(level) # @return [Symbol] # def log_level - Logify.level + Stove::Log.level end end end diff --git a/lib/stove/cli.rb b/lib/stove/cli.rb index 09172d0..972ede1 100644 --- a/lib/stove/cli.rb +++ b/lib/stove/cli.rb @@ -3,8 +3,6 @@ module Stove class Cli - include Logify - def initialize(argv, stdin=STDIN, stdout=STDOUT, stderr=STDERR, kernel=Kernel) @argv, @stdin, @stdout, @stderr, @kernel = argv, stdin, stdout, stderr, kernel end @@ -48,8 +46,8 @@ def execute! # Useful debugging output for when people type the wrong fucking command # and then open an issue like it's somehow my fault - log.info("Options: #{options.inspect}") - log.info("ARGV: #{@argv.inspect}") + Stove::Log.info("Options: #{options.inspect}") + Stove::Log.info("ARGV: #{@argv.inspect}") # Make a new cookbook object - this will raise an exception if there is # no cookbook at the given path @@ -62,10 +60,10 @@ def execute! # If we got this far, everything was successful :) @kernel.exit(0) rescue => e - log.error('Stove experienced an error!') - log.error(e.class.name) - log.error(e.message) - log.error(e.backtrace.join("\n")) + Stove::Log.error('Stove experienced an error!') + Stove::Log.error(e.class.name) + Stove::Log.error(e.message) + Stove::Log.error(e.backtrace.join("\n")) @kernel.exit(e.respond_to?(:exit_code) ? e.exit_code : 500) ensure diff --git a/lib/stove/config.rb b/lib/stove/config.rb index 52a0040..5ed0a34 100644 --- a/lib/stove/config.rb +++ b/lib/stove/config.rb @@ -3,7 +3,6 @@ module Stove class Config - include Logify include Mixin::Instanceable def method_missing(m, *args, &block) @@ -69,7 +68,7 @@ def __raw__ @__raw__ rescue Errno::ENOENT => e - log.warn { "No config file found at `#{__path__}'!" } + Stove::Log.warn { "No config file found at `#{__path__}'!" } @__raw__ = {} end end diff --git a/lib/stove/cookbook.rb b/lib/stove/cookbook.rb index 32ede6e..feea722 100644 --- a/lib/stove/cookbook.rb +++ b/lib/stove/cookbook.rb @@ -4,8 +4,6 @@ module Stove class Cookbook - include Logify - require_relative 'cookbook/metadata' # diff --git a/lib/stove/cookbook/metadata.rb b/lib/stove/cookbook/metadata.rb index 172cf93..ba176d9 100644 --- a/lib/stove/cookbook/metadata.rb +++ b/lib/stove/cookbook/metadata.rb @@ -3,9 +3,9 @@ module Stove class Cookbook # Borrowed and modified from: - # {https://raw.github.com/opscode/chef/11.4.0/lib/chef/cookbook/metadata.rb} + # {https://raw.github.com/chef/chef/11.4.0/lib/chef/cookbook/metadata.rb} # - # Copyright:: Copyright 2008-2017 Chef Software, Inc. + # Copyright:: Copyright 2008-2019 Chef Software, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/lib/stove/filter.rb b/lib/stove/filter.rb index 1b7d7f9..57b731d 100644 --- a/lib/stove/filter.rb +++ b/lib/stove/filter.rb @@ -1,7 +1,5 @@ module Stove class Filter - include Logify - include Mixin::Insideable # @@ -49,7 +47,7 @@ def initialize(klass, message, &block) # the cookbook to run this filter against # def run(cookbook, options = {}) - log.info(message) + Stove::Log.info(message) instance = klass.new(cookbook, options) inside(cookbook) do diff --git a/lib/stove/log.rb b/lib/stove/log.rb new file mode 100644 index 0000000..50d769d --- /dev/null +++ b/lib/stove/log.rb @@ -0,0 +1,24 @@ +# +# Copyright:: 2019 Chef Software, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'mixlib/log' + +module Stove + class Log + extend Mixlib::Log + end +end diff --git a/lib/stove/packager.rb b/lib/stove/packager.rb index 2222bb8..78b293c 100644 --- a/lib/stove/packager.rb +++ b/lib/stove/packager.rb @@ -5,8 +5,6 @@ module Stove class Packager - include Logify - ACCEPTABLE_FILES = [ '.foodcritic', 'README.*', diff --git a/lib/stove/plugins/base.rb b/lib/stove/plugins/base.rb index 2565e49..d2de575 100644 --- a/lib/stove/plugins/base.rb +++ b/lib/stove/plugins/base.rb @@ -1,14 +1,12 @@ module Stove class Plugin::Base - include Logify - extend Mixin::Optionable extend Mixin::Validatable class << self def run(description, &block) actions << Proc.new do |instance| - log.info { description } + Stove::Log.info { description } instance.instance_eval(&block) end end diff --git a/lib/stove/plugins/git.rb b/lib/stove/plugins/git.rb index 184a102..8b359a7 100644 --- a/lib/stove/plugins/git.rb +++ b/lib/stove/plugins/git.rb @@ -16,8 +16,8 @@ class Plugin::Git < Plugin::Base local_sha = git_null("rev-parse #{branch}").strip remote_sha = git_null("rev-parse #{remote}/#{branch}").strip - log.debug("Local SHA: #{local_sha}") - log.debug("Remote SHA: #{remote_sha}") + Stove::Log.debug("Local SHA: #{local_sha}") + Stove::Log.debug("Remote SHA: #{remote_sha}") local_sha == remote_sha end @@ -34,8 +34,8 @@ class Plugin::Git < Plugin::Base private def git(command, errors = true) - log.debug("the command matches") - log.debug("Running `git #{command}', errors: #{errors}") + Stove::Log.debug("the command matches") + Stove::Log.debug("Running `git #{command}', errors: #{errors}") Dir.chdir(cookbook.path) do response = %x|git #{command}| diff --git a/lib/stove/runner.rb b/lib/stove/runner.rb index 817b038..e3ca70c 100644 --- a/lib/stove/runner.rb +++ b/lib/stove/runner.rb @@ -1,7 +1,5 @@ module Stove class Runner - include Logify - attr_reader :cookbook attr_reader :options @@ -23,9 +21,9 @@ def run def run_plugin(name) if skip?(name) - log.info { "Skipping plugin `:#{name}'" } + Stove::Log.info { "Skipping plugin `:#{name}'" } else - log.info { "Running plugin `:#{name}'" } + Stove::Log.info { "Running plugin `:#{name}'" } klass = Plugin.const_get(Util.camelize(name)) klass.new(cookbook, options).run end diff --git a/lib/stove/validator.rb b/lib/stove/validator.rb index 184eb31..9154783 100644 --- a/lib/stove/validator.rb +++ b/lib/stove/validator.rb @@ -1,7 +1,5 @@ module Stove class Validator - include Logify - include Mixin::Insideable # @@ -49,12 +47,12 @@ def initialize(klass, id, &block) # the cookbook to run this validation against # def run(cookbook, options = {}) - log.info("Running validations for `#{klass.id}.#{id}'") + Stove::Log.info("Running validations for `#{klass.id}.#{id}'") inside(cookbook) do instance = klass.new(cookbook, options) unless result = instance.instance_eval(&block) - log.debug("Validation failed, result: #{result.inspect}") + Stove::Log.debug("Validation failed, result: #{result.inspect}") # Convert the class and id to their magical equivalents error = Error.const_get("#{Util.camelize(klass.id)}#{Util.camelize(id)}ValidationFailed") @@ -62,7 +60,7 @@ def run(cookbook, options = {}) end end - log.debug("Validation #{id} passed!") + Stove::Log.debug("Validation #{id} passed!") end end end diff --git a/stove.gemspec b/stove.gemspec index b3c5b03..a941d40 100644 --- a/stove.gemspec +++ b/stove.gemspec @@ -21,8 +21,8 @@ Gem::Specification.new do |spec| spec.required_ruby_version = '>= 2.3' # Runtime dependencies - spec.add_dependency 'chef-infra-api', '~> 0.5' - spec.add_dependency 'logify', '~> 0.2' + spec.add_dependency 'chef-api', '~> 0.5' + spec.add_dependency 'mixlib-log', '>= 2.0' spec.add_development_dependency 'aruba', '~> 0.6.0' spec.add_development_dependency 'bundler'