Skip to content

Commit 0609cb1

Browse files
committed
Simplify the knife runners by using a mixin
1 parent 59631d5 commit 0609cb1

5 files changed

Lines changed: 41 additions & 59 deletions

File tree

lib/chef/knife/cookbook_inspect.rb

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,13 @@
11
require 'chef/knife'
2+
require 'health_inspector'
23

34
class Chef
45
class Knife
56
class CookbookInspect < Knife
7+
include HealthInspector::Runner
68

7-
deps do
8-
require 'health_inspector'
9-
require 'chef/json_compat'
10-
require 'uri'
11-
require 'chef/cookbook_version'
12-
end
13-
14-
banner "knife cookbook inspect [COOKBOOK] (options)"
15-
16-
def run
17-
case @name_args.length
18-
when 1 # We are inspecting a cookbook
19-
cookbook_name = @name_args[0]
20-
# TODO: Support environments
21-
# env = config[:environment]
22-
# api_endpoint = env ? "environments/#{env}/cookbooks/#{cookbook_name}" : "cookbooks/#{cookbook_name}"
23-
24-
validator = HealthInspector::Checklists::Cookbooks.new(self)
25-
exit validator.validate_item( validator.load_item(cookbook_name) )
26-
when 0 # We are inspecting all the cookbooks
27-
exit HealthInspector::Checklists::Cookbooks.run(self)
28-
end
29-
end
9+
checklist HealthInspector::Checklists::Cookbooks
10+
banner "knife cookbook inspect [COOKBOOK] (options)"
3011
end
3112
end
3213
end
33-
34-
35-
36-
Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,13 @@
11
require 'chef/knife'
2+
require 'health_inspector'
23

34
class Chef
45
class Knife
56
class EnvironmentInspect < Knife
7+
include HealthInspector::Runner
68

7-
deps do
8-
require 'health_inspector'
9-
end
10-
11-
banner "knife environment inspect [ENVIRONMENT] (options)"
12-
13-
def run
14-
case @name_args.length
15-
when 1 # We are inspecting a environment
16-
environment_name = @name_args[0]
17-
validator = HealthInspector::Checklists::Environments.new(self)
18-
exit validator.validate_item( validator.load_item(environment_name) )
19-
when 0 # We are inspecting all the environments
20-
exit HealthInspector::Checklists::Environments.run(self)
21-
end
22-
end
9+
checklist HealthInspector::Checklists::Environments
10+
banner "knife environment inspect [ENVIRONMENT] (options)"
2311
end
2412
end
2513
end

lib/chef/knife/role_inspect.rb

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,13 @@
11
require 'chef/knife'
2+
require 'health_inspector'
23

34
class Chef
45
class Knife
56
class RoleInspect < Knife
7+
include HealthInspector::Runner
68

7-
deps do
8-
require 'health_inspector'
9-
end
10-
11-
banner "knife role inspect [ROLE] (options)"
12-
13-
def run
14-
case @name_args.length
15-
when 1 # We are inspecting a role
16-
role_name = @name_args[0]
17-
validator = HealthInspector::Checklists::Roles.new(self)
18-
exit validator.validate_item( validator.load_item(role_name) )
19-
when 0 # We are inspecting all the roles
20-
exit HealthInspector::Checklists::Roles.run(self)
21-
end
22-
end
9+
checklist HealthInspector::Checklists::Roles
10+
banner "knife role inspect [ROLE] (options)"
2311
end
2412
end
2513
end

lib/health_inspector.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require "health_inspector/color"
44
require "health_inspector/context"
55
require "health_inspector/pairing"
6+
require "health_inspector/runner"
67
require "health_inspector/checklists/base"
78
require "health_inspector/checklists/cookbooks"
89
require "health_inspector/checklists/data_bags"

lib/health_inspector/runner.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module HealthInspector
2+
module Runner
3+
def self.included(base)
4+
base.extend(ClassMethods)
5+
end
6+
7+
module ClassMethods
8+
attr_reader :checklist
9+
10+
def checklist(value = nil)
11+
return @checklist if value.nil?
12+
13+
@checklist = value
14+
end
15+
end
16+
17+
def run
18+
case @name_args.length
19+
when 1 # We are inspecting an item
20+
item = @name_args[0]
21+
validator = self.class.checklist.new(self)
22+
exit validator.validate_item(validator.load_item(item))
23+
when 0 # We are inspecting all the items
24+
exit self.class.checklist.run(self)
25+
end
26+
end
27+
end
28+
end

0 commit comments

Comments
 (0)