Skip to content

Commit

Permalink
Switch to mixlib-log
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
tas50 committed Oct 18, 2019
1 parent c72d269 commit a89b337
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 43 deletions.
14 changes: 8 additions & 6 deletions lib/stove.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require 'logify'
require 'pathname'

module Stove
Expand All @@ -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'
Expand Down Expand Up @@ -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]
Expand All @@ -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

#
Expand All @@ -81,7 +83,7 @@ def log_level=(level)
# @return [Symbol]
#
def log_level
Logify.level
Stove::Log.level
end
end
end
14 changes: 6 additions & 8 deletions lib/stove/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/stove/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

module Stove
class Config
include Logify
include Mixin::Instanceable

def method_missing(m, *args, &block)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions lib/stove/cookbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

module Stove
class Cookbook
include Logify

require_relative 'cookbook/metadata'

#
Expand Down
4 changes: 2 additions & 2 deletions lib/stove/cookbook/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 1 addition & 3 deletions lib/stove/filter.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module Stove
class Filter
include Logify

include Mixin::Insideable

#
Expand Down Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions lib/stove/log.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 0 additions & 2 deletions lib/stove/packager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

module Stove
class Packager
include Logify

ACCEPTABLE_FILES = [
'.foodcritic',
'README.*',
Expand Down
4 changes: 1 addition & 3 deletions lib/stove/plugins/base.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/stove/plugins/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}|

Expand Down
6 changes: 2 additions & 4 deletions lib/stove/runner.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module Stove
class Runner
include Logify

attr_reader :cookbook
attr_reader :options

Expand All @@ -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
Expand Down
8 changes: 3 additions & 5 deletions lib/stove/validator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module Stove
class Validator
include Logify

include Mixin::Insideable

#
Expand Down Expand Up @@ -49,20 +47,20 @@ 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")
raise error.new(path: Dir.pwd, result: result)
end
end

log.debug("Validation #{id} passed!")
Stove::Log.debug("Validation #{id} passed!")
end
end
end
4 changes: 2 additions & 2 deletions stove.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit a89b337

Please sign in to comment.