pdk-akerl 1.8.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +826 -0
  3. data/LICENSE +201 -0
  4. data/README.md +133 -0
  5. data/exe/pdk +10 -0
  6. data/lib/pdk.rb +10 -0
  7. data/lib/pdk/answer_file.rb +121 -0
  8. data/lib/pdk/cli.rb +113 -0
  9. data/lib/pdk/cli/build.rb +76 -0
  10. data/lib/pdk/cli/bundle.rb +42 -0
  11. data/lib/pdk/cli/convert.rb +41 -0
  12. data/lib/pdk/cli/errors.rb +23 -0
  13. data/lib/pdk/cli/exec.rb +246 -0
  14. data/lib/pdk/cli/exec_group.rb +67 -0
  15. data/lib/pdk/cli/module.rb +14 -0
  16. data/lib/pdk/cli/module/build.rb +14 -0
  17. data/lib/pdk/cli/module/generate.rb +45 -0
  18. data/lib/pdk/cli/new.rb +17 -0
  19. data/lib/pdk/cli/new/class.rb +32 -0
  20. data/lib/pdk/cli/new/defined_type.rb +30 -0
  21. data/lib/pdk/cli/new/module.rb +41 -0
  22. data/lib/pdk/cli/new/provider.rb +27 -0
  23. data/lib/pdk/cli/new/task.rb +31 -0
  24. data/lib/pdk/cli/test.rb +12 -0
  25. data/lib/pdk/cli/test/unit.rb +88 -0
  26. data/lib/pdk/cli/update.rb +32 -0
  27. data/lib/pdk/cli/util.rb +193 -0
  28. data/lib/pdk/cli/util/command_redirector.rb +26 -0
  29. data/lib/pdk/cli/util/interview.rb +63 -0
  30. data/lib/pdk/cli/util/option_normalizer.rb +53 -0
  31. data/lib/pdk/cli/util/option_validator.rb +56 -0
  32. data/lib/pdk/cli/validate.rb +124 -0
  33. data/lib/pdk/generate.rb +11 -0
  34. data/lib/pdk/generate/defined_type.rb +49 -0
  35. data/lib/pdk/generate/module.rb +318 -0
  36. data/lib/pdk/generate/provider.rb +82 -0
  37. data/lib/pdk/generate/puppet_class.rb +48 -0
  38. data/lib/pdk/generate/puppet_object.rb +288 -0
  39. data/lib/pdk/generate/task.rb +86 -0
  40. data/lib/pdk/i18n.rb +4 -0
  41. data/lib/pdk/logger.rb +28 -0
  42. data/lib/pdk/module.rb +21 -0
  43. data/lib/pdk/module/build.rb +214 -0
  44. data/lib/pdk/module/convert.rb +209 -0
  45. data/lib/pdk/module/metadata.rb +193 -0
  46. data/lib/pdk/module/templatedir.rb +313 -0
  47. data/lib/pdk/module/update.rb +111 -0
  48. data/lib/pdk/module/update_manager.rb +210 -0
  49. data/lib/pdk/report.rb +112 -0
  50. data/lib/pdk/report/event.rb +357 -0
  51. data/lib/pdk/template_file.rb +89 -0
  52. data/lib/pdk/tests/unit.rb +213 -0
  53. data/lib/pdk/util.rb +271 -0
  54. data/lib/pdk/util/bundler.rb +253 -0
  55. data/lib/pdk/util/filesystem.rb +12 -0
  56. data/lib/pdk/util/git.rb +74 -0
  57. data/lib/pdk/util/puppet_version.rb +242 -0
  58. data/lib/pdk/util/ruby_version.rb +147 -0
  59. data/lib/pdk/util/vendored_file.rb +88 -0
  60. data/lib/pdk/util/version.rb +42 -0
  61. data/lib/pdk/util/windows.rb +13 -0
  62. data/lib/pdk/util/windows/api_types.rb +57 -0
  63. data/lib/pdk/util/windows/file.rb +36 -0
  64. data/lib/pdk/util/windows/string.rb +16 -0
  65. data/lib/pdk/validate.rb +14 -0
  66. data/lib/pdk/validate/base_validator.rb +209 -0
  67. data/lib/pdk/validate/metadata/metadata_json_lint.rb +86 -0
  68. data/lib/pdk/validate/metadata/metadata_syntax.rb +109 -0
  69. data/lib/pdk/validate/metadata_validator.rb +30 -0
  70. data/lib/pdk/validate/puppet/puppet_lint.rb +67 -0
  71. data/lib/pdk/validate/puppet/puppet_syntax.rb +112 -0
  72. data/lib/pdk/validate/puppet_validator.rb +30 -0
  73. data/lib/pdk/validate/ruby/rubocop.rb +77 -0
  74. data/lib/pdk/validate/ruby_validator.rb +29 -0
  75. data/lib/pdk/validate/tasks/metadata_lint.rb +126 -0
  76. data/lib/pdk/validate/tasks/name.rb +88 -0
  77. data/lib/pdk/validate/tasks_validator.rb +33 -0
  78. data/lib/pdk/version.rb +4 -0
  79. data/locales/config.yaml +21 -0
  80. data/locales/pdk.pot +1283 -0
  81. metadata +304 -0
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright {yyyy} {name of copyright owner}
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,133 @@
1
+ **Fork of https://github.com/puppetlabs/pdk**:
2
+
3
+ Changes from upstream:
4
+
5
+ * Addition of this notice
6
+ * Gem name changed to pdk-akerl
7
+ * Addition of .prospectus file
8
+ * Update of Bundler and Ruby dependencies (https://github.com/puppetlabs/pdk/pull/610)
9
+
10
+ # pdk [![Build Status](https://travis-ci.org/puppetlabs/pdk.svg?branch=master)](https://travis-ci.org/puppetlabs/pdk) [![Appveyor Build Status](https://ci.appveyor.com/api/projects/status/x70e2fqllbaootpd?svg=true)](https://ci.appveyor.com/project/puppetlabs/pdk) [![Coverage Status](https://coveralls.io/repos/github/puppetlabs/pdk/badge.svg?branch=master)](https://coveralls.io/github/puppetlabs/pdk?branch=master)
11
+
12
+ * [Installation](#installation)
13
+ * [Basic usage](#basic-usage)
14
+ * [Experimental features](#experimental-features)
15
+ * [Module compatibility](#module-compatibility)
16
+ * [Contributing](#contributing)
17
+ * [Contact Information](#contact-information)
18
+
19
+ The Puppet Development Kit (PDK) includes key Puppet code development and testing tools for Linux, Windows, and OS X workstations, so you can install one package with the tools you need to create and validate new modules.
20
+
21
+ PDK includes testing tools, a complete module skeleton, and command line tools to help you create, validate, and run tests on Puppet modules. PDK also includes all dependencies needed for its use.
22
+
23
+ PDK includes the following tools:
24
+
25
+ Tool | Description
26
+ ----------------|-------------------------
27
+ metadata-json-lint | Validates and lints `metadata.json` files in modules against Puppet's module metadatastyle guidelines.
28
+ pdk | Command line tool for generating and testing modules
29
+ puppet-lint | Checks your Puppet code against the recommendations in the Puppet Language style guide.
30
+ puppet-syntax | Checks for correct syntax in Puppet manifests, templates, and Hiera YAML.
31
+ puppetlabs_spec_helper | Provides classes, methods, and Rake tasks to help with spec testing Puppet code.
32
+ rspec-puppet | Tests the behavior of Puppet when it compiles your manifests into a catalog of Puppet resources.
33
+ rspec-puppet-facts | Adds support for running rspec-puppet tests against the facts for your supported operating systems.
34
+
35
+
36
+ ## Installation
37
+
38
+ Download and install the newest package matching your platform from the [download](https://puppet.com/download-puppet-development-kit) page. If you are using Windows, remember to close any open PowerShell windows.
39
+
40
+ For complete installation information, see the [PDK documentation](https://puppet.com/docs/pdk/latest/pdk_install.html).
41
+
42
+ ## Basic usage
43
+
44
+ PDK can generate modules and classes, validate module metadata, style, and syntax, and run unit tests. This README contains very basic usage information---for complete usage information, see the [PDK documentation](https://puppet.com/docs/pdk/latest/pdk_install.html).
45
+
46
+ ### Generate a module
47
+
48
+ Generates the basic components of a module and set up an infrastructure for testing it with PDK.
49
+
50
+ 1. Run the `pdk new module` command, specifying the name of the new module:
51
+
52
+ ```
53
+ pdk new module my_module
54
+ ```
55
+
56
+ This command asks a series of metadata questions and then generates the basic components of a new module.
57
+
58
+ ### Generate a class
59
+
60
+ To generate a class in your module, use the `pdk new class` command, specifying the name of your new class. To generate the main class of the module, which is defined in an `init.pp` file, give the class the same name as the module.
61
+
62
+ 1. From the command line, in your module's directory, run:
63
+ ```
64
+ pdk new class class_name
65
+ ```
66
+
67
+ PDK creates the new class manifest and a test file (as `class_name_spec.rb`) in your module's `/spec/classes` directory.
68
+
69
+ ### Generate a defined type
70
+
71
+ To generate a defined type in your module, use the `pdk new defined_type` command, specifying the name of your new defined type.
72
+
73
+ 1. From the command line, in your module's directory, run:
74
+ ```
75
+ pdk new defined_type defined_type_name
76
+ ```
77
+
78
+ PDK creates the new defined\_type manifest and a test file (as `defined_type_name_spec.rb`) in your module's `/spec/defines` directory.
79
+
80
+ ### Generate a task
81
+
82
+ To generate a task in your module, use the `pdk new task` command, specifying the name of your new task.
83
+
84
+ 1. From the command line, in your module's directory, run:
85
+ ```
86
+ pdk new task task_name
87
+ ```
88
+
89
+ PDK creates the new task file and metadata.
90
+
91
+ ### Validating a module
92
+
93
+ PDK can validate the well-formedness of the module and style and syntax of its files.
94
+
95
+ 1. In the module's directory, run:
96
+
97
+ ```
98
+ pdk validate
99
+ ```
100
+
101
+ This command validates the metadata, Puppet code syntax and style, and Ruby syntax and style for the entire module.
102
+
103
+ ### Run unit tests
104
+
105
+ PDK's [default template](https://github.com/puppetlabs/pdk-templates) sets up [rspec](http://rspec.info/) for Ruby-level unit testing, and [rspec-puppet](https://github.com/rodjek/rspec-puppet/) for catalog-level unit testing.
106
+
107
+ In the module's directory, run unit tests with:
108
+
109
+ ```
110
+ pdk test unit
111
+ ```
112
+
113
+ This command runs all available unit tests.
114
+
115
+ ## Experimental features
116
+
117
+ ### `pdk bundle` command
118
+
119
+ This command executes arbitrary commands in a bundler context within the module you're currently working on. Arguments to this command are passed straight through to bundler. This command is experimental and can lead to errors that can't be resolved by the pdk itself.
120
+
121
+ Note that for most uses, you must use the `--` to separate bundler options from pdk options. Compare the following two commands:
122
+
123
+ ## Module Compatibility
124
+
125
+ **PDK Version Compatibility:** Modules created with PDK version validate against and run on all Puppet and Ruby version combinations currently under maintenance (see https://docs.puppet.com/puppet/latest/about_agent.html and https://puppet.com/misc/puppet-enterprise-lifecycle)
126
+
127
+ ## Contributing
128
+
129
+ PDK encourages community contributions. See the [CONTRIBUTING.md](CONTRIBUTING.md) file for development notes.
130
+
131
+ ## Contact Information
132
+
133
+ To contact us with questions: [pdk-maintainers@puppet.com](mailto:pdk-maintainers@puppet.com)
data/exe/pdk ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'pdk/cli'
4
+
5
+ begin
6
+ PDK::CLI.run(ARGV)
7
+ rescue Interrupt
8
+ warn "\nAborted!"
9
+ exit 1
10
+ end
data/lib/pdk.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'pdk/answer_file'
2
+ require 'pdk/generate'
3
+ require 'pdk/i18n'
4
+ require 'pdk/logger'
5
+ require 'pdk/report'
6
+ require 'pdk/template_file'
7
+ require 'pdk/validate'
8
+ require 'pdk/version'
9
+
10
+ module PDK; end
@@ -0,0 +1,121 @@
1
+ require 'json'
2
+ require 'pdk/util/filesystem'
3
+
4
+ module PDK
5
+ # Singleton accessor to the current answer file being used by the PDK.
6
+ #
7
+ # @return [PDK::AnswerFile] The AnswerFile instance currently being used by
8
+ # the PDK.
9
+ def self.answers
10
+ @answer_file ||= PDK::AnswerFile.new
11
+ end
12
+
13
+ # Specify the path to a custom answer file that the PDK should use.
14
+ #
15
+ # @param path [String] A path on disk to the file where the PDK should store
16
+ # answers to interactive questions.
17
+ def self.answer_file=(path)
18
+ @answer_file = PDK::AnswerFile.new(path)
19
+ end
20
+
21
+ class AnswerFile
22
+ attr_reader :answers
23
+ attr_reader :answer_file_path
24
+
25
+ include PDK::Util::Filesystem
26
+
27
+ # Initialises the AnswerFile object, which stores the responses to certain
28
+ # interactive questions.
29
+ #
30
+ # @param answer_file_path [String, nil] The path on disk to the file where
31
+ # the answers will be stored and read from. If not specified (or `nil`),
32
+ # the default path will be used (see #default_answer_file_path).
33
+ #
34
+ # @raise (see #read_from_disk)
35
+ def initialize(answer_file_path = nil)
36
+ @answer_file_path = answer_file_path || default_answer_file_path
37
+ @answers = read_from_disk
38
+ end
39
+
40
+ # Retrieve the stored answer to a question.
41
+ #
42
+ # @param question [String] The question name/identifying string.
43
+ #
44
+ # @return [Object] The answer to the question, or `nil` if no answer found.
45
+ def [](question)
46
+ answers[question]
47
+ end
48
+
49
+ # Update the stored answers in memory and then save them to disk.
50
+ #
51
+ # @param new_answers [Hash{String => Object}] The new questions and answers
52
+ # to be merged into the existing answers.
53
+ #
54
+ # @raise [PDK::CLI::FatalError] if the new answers are not provided as
55
+ # a Hash.
56
+ # @raise (see #save_to_disk)
57
+ def update!(new_answers = {})
58
+ unless new_answers.is_a?(Hash)
59
+ raise PDK::CLI::FatalError, _('Answer file can be updated only with a Hash')
60
+ end
61
+
62
+ answers.merge!(new_answers)
63
+
64
+ save_to_disk
65
+ end
66
+
67
+ private
68
+
69
+ # Determine the default path to the answer file.
70
+ #
71
+ # @return [String] The path on disk to the default answer file.
72
+ def default_answer_file_path
73
+ File.join(PDK::Util.cachedir, 'answers.json')
74
+ end
75
+
76
+ # Read existing answers into memory from the answer file on disk.
77
+ #
78
+ # @raise [PDK::CLI::FatalError] If the answer file exists but can not be
79
+ # read.
80
+ #
81
+ # @return [Hash{String => Object}] The existing questions and answers.
82
+ def read_from_disk
83
+ return {} if !File.file?(answer_file_path) || File.zero?(answer_file_path)
84
+
85
+ unless File.readable?(answer_file_path)
86
+ raise PDK::CLI::FatalError, _("Unable to open '%{file}' for reading") % {
87
+ file: answer_file_path,
88
+ }
89
+ end
90
+
91
+ answers = JSON.parse(File.read(answer_file_path))
92
+ if answers.is_a?(Hash)
93
+ answers
94
+ else
95
+ PDK.logger.warn _("Answer file '%{path}' did not contain a valid set of answers, recreating it") % {
96
+ path: answer_file_path,
97
+ }
98
+ {}
99
+ end
100
+ rescue JSON::JSONError
101
+ PDK.logger.warn _("Answer file '%{path}' did not contain valid JSON, recreating it") % {
102
+ path: answer_file_path,
103
+ }
104
+ {}
105
+ end
106
+
107
+ # Save the in memory answer set to the answer file on disk.
108
+ #
109
+ # @raise [PDK::CLI::FatalError] if the answer file can not be written to.
110
+ def save_to_disk
111
+ FileUtils.mkdir_p(File.dirname(answer_file_path))
112
+
113
+ write_file(answer_file_path, JSON.pretty_generate(answers))
114
+ rescue SystemCallError, IOError => e
115
+ raise PDK::CLI::FatalError, _("Unable to write '%{file}': %{msg}") % {
116
+ file: answer_file_path,
117
+ msg: e.message,
118
+ }
119
+ end
120
+ end
121
+ end