Skip to content

Commit

Permalink
Fold KnifeWindowsBase into WinrmCommandSharedFunctions
Browse files Browse the repository at this point in the history
It only had a single method and that method was only still used in WinrmCommandSharedFunctions

Signed-off-by: Tim Smith <tsmith@chef.io>
  • Loading branch information
tas50 committed Jun 26, 2020
1 parent 66ee9e9 commit bf46b7f
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 59 deletions.
34 changes: 0 additions & 34 deletions lib/chef/knife/knife_windows_base.rb

This file was deleted.

4 changes: 1 addition & 3 deletions lib/chef/knife/winrm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,18 @@

require_relative "../knife"
require_relative "winrm_knife_base" # WinrmCommandSharedFunctions
require_relative "winrm_session"
require_relative "knife_windows_base"

class Chef
class Knife
class Winrm < Knife

include Chef::Knife::WinrmCommandSharedFunctions
include Chef::Knife::KnifeWindowsBase

deps do
require_relative "windows_cert_generate"
require_relative "windows_cert_install"
require_relative "windows_listener_create"
require_relative "winrm_session"
require "readline"
require_relative "../search/query"
end
Expand Down
44 changes: 24 additions & 20 deletions lib/chef/knife/winrm_knife_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@
require_relative "../knife"
require_relative "winrm_base"
require_relative "winrm_shared_options"
require_relative "knife_windows_base"

class Chef
class Knife
module WinrmCommandSharedFunctions

FAILED_BASIC_HINT ||= "Hint: Please check winrm configuration 'winrm get winrm/config/service' AllowUnencrypted flag on remote server.".freeze
FAILED_NOT_BASIC_HINT ||= <<-eos.gsub /^\s+/, ""
FAILED_NOT_BASIC_HINT ||= <<-EOS.gsub /^\s+/, ""
Hint: Make sure to prefix domain usernames with the correct domain name.
Hint: Local user names should be prefixed with computer name or IP address.
EXAMPLE: my_domain\\user_namer
eos
EOS

def self.included(includer)
includer.class_eval do
Expand All @@ -39,7 +38,13 @@ def self.included(includer)

include Chef::Knife::WinrmBase
include Chef::Knife::WinrmSharedOptions
include Chef::Knife::KnifeWindowsBase

def locate_config_value(key)
key = key.to_sym
value = config[key] || Chef::Config[:knife][key] || default_config[key]
Chef::Log.debug("Looking for key #{key} and found value #{value}")
value
end

def validate_winrm_options!
winrm_auth_protocol = locate_config_value(:winrm_authentication_protocol)
Expand All @@ -64,18 +69,18 @@ def configure_session

def resolve_target_nodes
@list = case config[:manual]
when true
@name_args[0].split(" ")
when false
r = []
q = Chef::Search::Query.new
@action_nodes = q.search(:node, @name_args[0])[0]
@action_nodes.each do |item|
i = extract_nested_value(item, config[:attribute])
r.push(i) unless i.nil?
end
r
end
when true
@name_args[0].split(" ")
when false
r = []
q = Chef::Search::Query.new
@action_nodes = q.search(:node, @name_args[0])[0]
@action_nodes.each do |item|
i = extract_nested_value(item, config[:attribute])
r.push(i) unless i.nil?
end
r
end

if @list.length == 0
if @action_nodes.length == 0
Expand All @@ -86,7 +91,7 @@ def resolve_target_nodes
"Try setting another attribute to open the connection using --attribute.")
end
exit 10
end
end
end

# TODO: Copied from Knife::Core:GenericPresenter. Should be extracted
Expand Down Expand Up @@ -307,9 +312,8 @@ def warn_no_ssl_peer_verification
```
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
WARN
end
end

end
end
end
end
end
Expand Down
20 changes: 20 additions & 0 deletions spec/unit/knife/dummy_winrm_connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Dummy
class WinRMTransport
attr_reader :httpcli

def initialize
@httpcli = HTTPClient.new
end
end

class Connection
attr_reader :transport
attr_accessor :logger

def initialize
@transport = WinRMTransport.new
end

def shell; end
end
end
2 changes: 1 addition & 1 deletion spec/unit/knife/winrm_session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

require "spec_helper"
require_relative "../../../lib/chef/knife/winrm"
require "dummy_winrm_connection"
require_relative "dummy_winrm_connection"

Chef::Knife::Winrm.load_deps

Expand Down
2 changes: 1 addition & 1 deletion spec/unit/knife/winrm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

require "spec_helper"
require_relative "../../../lib/chef/knife/winrm"
require "dummy_winrm_connection"
require_relative "dummy_winrm_connection"

Chef::Knife::Winrm.load_deps

Expand Down

0 comments on commit bf46b7f

Please sign in to comment.