-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(PDK-1113) Use PDK configuration instead of AnswerFile class
Previously the PDK had two ways to retrieve the same configuration information. One via the PDK::Config classes and the other the AnswerFile class. This should only be one. This commit removes the AnswerClass (and associated features) and instead uses PDK::Config for setting management * Removes the PDK::AnswerFile class, and instead has a single helper method for the default file location. This may eventually be moved into the PDK::Config namespace as well * Removes the hidden `--answer-file` command line option and instead uses an environment variable PDK_ANSWER_FILE. This feature is not supported and is only used to acceptance testing. Note that we moved to an environment variable as there was a subtle race condition where the CLI reads from PDK::Config, but the CLI also moves the user answer file location. By using an environment variable this race condition can no longer occur. * Move setting reads from PDK.answers to PDK.config.pdk_setting(....) * Move setting writes from PDK.answers.update! to PDK.config.user['module_defaults']['....'] = * Modify tests for the setting read/write changes * Added a Mock configuration RSpec Context to stop any test information affecting actual real setting files. Previously module answer files were being modified
- Loading branch information
1 parent
61a202a
commit 91127fc
Showing
18 changed files
with
182 additions
and
433 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,103 +1,12 @@ | ||
require 'pdk' | ||
autoload :JSON, 'json' | ||
|
||
module PDK | ||
class AnswerFile | ||
attr_reader :answers | ||
attr_reader :answer_file_path | ||
|
||
# Initialises the AnswerFile object, which stores the responses to certain | ||
# interactive questions. | ||
# | ||
# @param answer_file_path [String, nil] The path on disk to the file where | ||
# the answers will be stored and read from. If not specified (or `nil`), | ||
# the default path will be used (see #default_answer_file_path). | ||
# | ||
# @raise (see #read_from_disk) | ||
def initialize(answer_file_path = nil) | ||
@answer_file_path = answer_file_path || default_answer_file_path | ||
@answers = read_from_disk | ||
end | ||
|
||
# Retrieve the stored answer to a question. | ||
# | ||
# @param question [String] The question name/identifying string. | ||
# | ||
# @return [Object] The answer to the question, or `nil` if no answer found. | ||
def [](question) | ||
answers[question] | ||
end | ||
|
||
# Update the stored answers in memory and then save them to disk. | ||
# | ||
# @param new_answers [Hash{String => Object}] The new questions and answers | ||
# to be merged into the existing answers. | ||
# | ||
# @raise [PDK::CLI::FatalError] if the new answers are not provided as | ||
# a Hash. | ||
# @raise (see #save_to_disk) | ||
def update!(new_answers = {}) | ||
unless new_answers.is_a?(Hash) | ||
raise PDK::CLI::FatalError, _('Answer file can be updated only with a Hash') | ||
end | ||
|
||
answers.merge!(new_answers) | ||
|
||
save_to_disk | ||
end | ||
|
||
private | ||
|
||
# Determine the default path to the answer file. | ||
# | ||
# @return [String] The path on disk to the default answer file. | ||
def default_answer_file_path | ||
File.join(PDK::Util.cachedir, 'answers.json') | ||
end | ||
|
||
# Read existing answers into memory from the answer file on disk. | ||
# | ||
# @raise [PDK::CLI::FatalError] If the answer file exists but can not be | ||
# read. | ||
# | ||
# @return [Hash{String => Object}] The existing questions and answers. | ||
def read_from_disk | ||
return {} if !PDK::Util::Filesystem.file?(answer_file_path) || PDK::Util::Filesystem.zero?(answer_file_path) | ||
|
||
unless PDK::Util::Filesystem.readable?(answer_file_path) | ||
raise PDK::CLI::FatalError, _("Unable to open '%{file}' for reading") % { | ||
file: answer_file_path, | ||
} | ||
end | ||
|
||
answers = JSON.parse(PDK::Util::Filesystem.read_file(answer_file_path)) | ||
if answers.is_a?(Hash) | ||
answers | ||
else | ||
PDK.logger.warn _("Answer file '%{path}' did not contain a valid set of answers, recreating it") % { | ||
path: answer_file_path, | ||
} | ||
{} | ||
end | ||
rescue JSON::JSONError | ||
PDK.logger.warn _("Answer file '%{path}' did not contain valid JSON, recreating it") % { | ||
path: answer_file_path, | ||
} | ||
{} | ||
end | ||
|
||
# Save the in memory answer set to the answer file on disk. | ||
# | ||
# @raise [PDK::CLI::FatalError] if the answer file can not be written to. | ||
def save_to_disk | ||
PDK::Util::Filesystem.mkdir_p(File.dirname(answer_file_path)) | ||
|
||
PDK::Util::Filesystem.write_file(answer_file_path, JSON.pretty_generate(answers)) | ||
rescue SystemCallError, IOError => e | ||
raise PDK::CLI::FatalError, _("Unable to write '%{file}': %{msg}") % { | ||
file: answer_file_path, | ||
msg: e.message, | ||
} | ||
def self.default_answer_file_path | ||
PDK::Util::Filesystem.expand_path(File.join(PDK::Util.cachedir, 'answers.json')) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.