Gusto / it-cpe-opensource

Tools used by the CPE team at Gusto to manage our endpoints and software deployment systems.
Other
59 stars 29 forks source link

Swap-out `node.macos?`, `node.windows?` for `macos?`, `windows?` #63

Closed ChefAustin closed 2 years ago

ChefAustin commented 2 years ago

This PR will replace all instances of node.windows?, node.macos? with windows?, macos? (respectively).

I recently noticed that Chef has some native helper functions available (See: below code block) for discerning platform family (i.e. macos?, windows?, debian?, linux?, et al) that make the corresponding Facebook's helper node functions moot.

$ sudo chef-shell
loading configuration: none (standalone session)
true
Session type: standalone
Loading.................done.

Welcome to the chef-shell 17.7.29
For usage see https://docs.chef.io/chef_shell/

run `help' for help, `exit' or ^D to quit.

chef (17.7.29)> require 'pry'
 => true
chef (17.7.29)> binding.pry

Frame number: 0/14
[1] pry(main)> show-method macos?

From: /opt/chef-workstation/embedded/lib/ruby/gems/3.0.0/gems/chef-utils-17.7.29/lib/chef-utils/dsl/platform_family.rb:72:
Owner: ChefUtils::DSL::PlatformFamily
Visibility: public
Signature: macos?(node=?)
Number of lines: 3

def macos?(node = __getnode)
  node ? node["platform_family"] == "mac_os_x" : macos_ruby?
end
[2] pry(main)> show-method windows?

From: /opt/chef-workstation/embedded/lib/ruby/gems/3.0.0/gems/chef-utils-17.7.29/lib/chef-utils/dsl/platform_family.rb:249:
Owner: ChefUtils::DSL::PlatformFamily
Visibility: public
Signature: windows?(node=?)
Number of lines: 13

def windows?(node = __getnode(true))
  # This is all somewhat complicated.  We prefer to get the node object so that chefspec can
  # stub the node object.  But we also have to deal with class-parsing time where there is
  # no node object, so we have to fall back to RUBY_PLATFORM based detection.  We cannot pull
  # the node object out of the Chef.run_context.node global object here (which is what the
  # false flag to __getnode is about) because some run-time code also cannot run under chefspec
  # on non-windows where the node is stubbed to windows.
  #
  # As a result of this the `windows?` helper and the `ChefUtils.windows?` helper do not behave
  # the same way in that the latter is not stubbable by chefspec.
  #
  node ? node["platform_family"] == "windows" : windows_ruby?
end
[3] pry(main)> exit
 => nil
chef (17.7.29)> windows?
 => false
chef (17.7.29)> macos?
 => true
chef (17.7.29)> debian?
 => false
chef (17.7.29)> linux?
 => false
chef (17.7.29)> exit