-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add module lib/ to LOAD_PATH #9
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this module should rely entirely on STDIN rather than using the environment variables.
I'm not 100% sure whether we should magically add lib to the load path or force the user to opt into that behavior.
I think ideally we should be parsing stdin and saving it to a class variable with a new class method params
perhaps. That raises some complexity around handling though which may blow up the scope of ticket too much.
- new class variables
params
anderror
- new class method
params
which returns theparams
class variable if set otherwise it reads and parses params from stdin. If something goes wrong it rescues the exception and saves it inerror
- All class methods(except
run
) check iferror
is set and immediately exit if so. run
raises the error if set.- new
reset
class method to clear out all the class variables when testing.
@adreyer i was able to get the I'll work on the "capturing errors" part next. |
@@ -1,6 +1,8 @@ | |||
require 'json' | |||
|
|||
class TaskHelper | |||
@@params = nil # rubocop:disable Style/ClassVars |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's safe to just add this to .rubocop.yml
. I trust codereview to catch misuse of class variables.
|
Currently, when writing ruby tasks, there can be a bit of an issue with
require
statements for things in a module'slib
directory.Normally these files do a
require puppet/whatever
. Puppet makes this possible by adding thelib
directory to Ruby'sLOAD_PATH
when it compiles its catalogs.In Bolt however the LOAD_PATH isn't modified in the same way and it causes a bit of grief if you try to
require
something inlib
. This is especially evident when that class/module inlib
also does arequire
for something else inlib
.To fix this problem i noticed that the
cisco_ios
module solves this by modifying theLOAD_PATH
early on in the Task's lifetime:https://github.com/puppetlabs/cisco_ios/blob/master/lib/puppet/util/task_helper.rb#L34-L39
I've taken this idea and expanded upon it by allowing it to be used in two different ways in the Ruby TaskHelper:
Option 1 - Global access using PT__installdir
At the top of a Ruby task file you could do something like so:
Option 2 - Using _installdir from stdin before task() is called
Alternatively, if that environment variable doesn't exist, we can allow the user to do the
require
inside of thetask()
method in their ruby code.We can accomplish this by reading in the parameters from
stdin
, parsing out_installdir
and then modifyingLOAD_PATH
beforetask()
is called.