abide_dev_utils 0.2.3 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +7 -0
- data/lib/abide_dev_utils/cli/puppet.rb +11 -2
- data/lib/abide_dev_utils/ppt/new_obj.rb +86 -47
- data/lib/abide_dev_utils/resources/generic_spec.erb +13 -0
- data/lib/abide_dev_utils/version.rb +1 -1
- data/lib/abide_dev_utils/xccdf/cis/hiera.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c4ee71223292cc98028db5407eaef69cb2a0a150de9552b4d51d3b1ee17197bf
|
4
|
+
data.tar.gz: 3f56a0ad99e2ae3ccc8478210f7bb1961c4500267bedc1d42612c1947ee7c3fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 821d519b152ffc0a246939050b2b6117b5f3986fdf094d6333cfef8617b06c1572517a93b96f76039368c860715a4ef7ef39c78c039581369c4e61b484bce0c0
|
7
|
+
data.tar.gz: 6010ad3359da0de05b352e48210f3e4ddbd95b30e942598c6e6cfa77c175a1041ab87491d2a2afa15490a12fc197c549ab5d58dbb4bfc816e569afe5ac66dd9e
|
data/README.md
CHANGED
@@ -128,6 +128,8 @@ Install the gem:
|
|
128
128
|
* `--absolute-template-dir`, `-A` - Allows you to specify an absolute path with `--template-dir`. This is useful if your template directory is not relative to your module's root directory
|
129
129
|
* `--template-name`, `-n` - Allows you to specify a template name if it is different than the `TYPE` parameter
|
130
130
|
* `--vars`, `-V` - Comma-separated key=value pairs to pass in to the template renderer. This allows you to pass arbitrary values that can be used in your templates.
|
131
|
+
* `--spec-template`, `-S` - Path to an ERB template to use for rspec test generation instead of the default
|
132
|
+
* `--force`, `-f` - Skips any prompts and executes the command
|
131
133
|
|
132
134
|
`abide puppet new` exposes a few variables for you to use in your templates by default:
|
133
135
|
|
@@ -179,8 +181,10 @@ $ ls manifests
|
|
179
181
|
init.pp
|
180
182
|
$ abide puppet new control_class 'test_module::controls::test_new_control'
|
181
183
|
Created file /Users/the.dude/test_module/manifests/controls/test_new_control.pp
|
184
|
+
Created file /Users/the.dude/test_module/spec/classes/controls/test_new_control_spec.rb
|
182
185
|
$ abide puppet new util_class 'test_module::utils::test_new_util' -v 'testvar1=dude,testvar2=sweet'
|
183
186
|
Created file /Users/the.dude/test_module/manifests/utils/test_new_util.pp
|
187
|
+
Created file /Users/the.dude/test_module/spec/classes/utils/test_new_util_spec.rb
|
184
188
|
$ cat manifests/controls/test_new_control.pp
|
185
189
|
# @api private
|
186
190
|
class test_module::controls::test_new_control (
|
@@ -204,6 +208,9 @@ class test_module::utils::test_new_util (
|
|
204
208
|
|
205
209
|
```
|
206
210
|
|
211
|
+
**NOTE**: You can use two special prefixes on your template files to denote where the rspec test should be generated for that object.
|
212
|
+
If the prefix `c-` is used, the test will be generated in the `spec/classes` directory. If the prefix `d-` is used, the test will be generated in the `spec/defines` directory. For example, to create a template for a defined type, name the template something like this: `d-my_defined_type.pp.erb`.
|
213
|
+
|
207
214
|
### XCCDF Command Reference
|
208
215
|
|
209
216
|
#### to_hiera
|
@@ -87,6 +87,16 @@ module Abide
|
|
87
87
|
'--vars [VARNAME=VALUE]',
|
88
88
|
'Allows you to specify comma-separated variable names and values that will be converted into a hash that is available for you to use in your templates'
|
89
89
|
) { |v| @data[:vars] = v }
|
90
|
+
options.on(
|
91
|
+
'-S [PATH]',
|
92
|
+
'--spec-template [PATH]',
|
93
|
+
'Path to an ERB template to use for rspec test generation instead of the default'
|
94
|
+
)
|
95
|
+
options.on(
|
96
|
+
'-f',
|
97
|
+
'--force',
|
98
|
+
'Skips any prompts and executes the command'
|
99
|
+
) { |_| @data[:force] = true }
|
90
100
|
end
|
91
101
|
|
92
102
|
def execute(type, name)
|
@@ -97,8 +107,7 @@ module Abide
|
|
97
107
|
opts: @data,
|
98
108
|
vars: @data.fetch(:vars, '').split(',').map { |i| i.split('=') }.to_h # makes the str a hash
|
99
109
|
)
|
100
|
-
|
101
|
-
Abide::CLI::OUTPUT.simple(result)
|
110
|
+
builder.build
|
102
111
|
end
|
103
112
|
end
|
104
113
|
end
|
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require 'erb'
|
4
4
|
require 'pathname'
|
5
|
+
require 'abide_dev_utils/output'
|
5
6
|
require 'abide_dev_utils/prompt'
|
6
7
|
require 'abide_dev_utils/errors/ppt'
|
7
8
|
|
@@ -9,6 +10,10 @@ module AbideDevUtils
|
|
9
10
|
module Ppt
|
10
11
|
class NewObjectBuilder
|
11
12
|
DEFAULT_EXT = '.pp'
|
13
|
+
VALID_EXT = /(\.pp|\.rb)\.erb$/.freeze
|
14
|
+
TMPL_PATTERN = /^[a-zA-Z][^\s]*\.erb$/.freeze
|
15
|
+
OBJ_PREFIX = /^(c-|d-)/.freeze
|
16
|
+
PREFIX_TEST_PATH = { 'c-' => 'classes', 'd-' => 'defines' }.freeze
|
12
17
|
|
13
18
|
def initialize(obj_type, obj_name, opts: {}, vars: {})
|
14
19
|
@obj_type = obj_type
|
@@ -19,23 +24,14 @@ module AbideDevUtils
|
|
19
24
|
validate_class_vars
|
20
25
|
end
|
21
26
|
|
22
|
-
attr_reader :obj_type, :obj_name, :
|
23
|
-
|
24
|
-
def render
|
25
|
-
ERB.new(File.read(@tmpl_path.to_s), 0, '<>-').result(binding)
|
26
|
-
end
|
27
|
+
attr_reader :obj_type, :obj_name, :root_dir, :tmpl_dir, :obj_path, :vars
|
27
28
|
|
28
29
|
def build
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
content = render
|
35
|
-
File.open(obj_path, 'w') { |f| f.write(render) } unless content.empty?
|
36
|
-
raise AbideDevUtils::Errors::Ppt::FailedToCreateFileError, obj_path unless File.file?(obj_path)
|
37
|
-
|
38
|
-
"Created file #{obj_path}"
|
30
|
+
force = @opts.fetch(:force, false)
|
31
|
+
obj_cont = force ? true : continue?(obj_path)
|
32
|
+
spec_cont = force ? true : continue?(@tmpl_data[:spec_path])
|
33
|
+
write_file(obj_path, @tmpl_data[:path]) if obj_cont
|
34
|
+
write_file(@tmpl_data[:spec_path], @spec_tmpl) if spec_cont
|
39
35
|
end
|
40
36
|
|
41
37
|
# If a method gets called on the Hiera object which is not defined,
|
@@ -59,42 +55,99 @@ module AbideDevUtils
|
|
59
55
|
|
60
56
|
private
|
61
57
|
|
58
|
+
def continue?(path)
|
59
|
+
continue = if File.exist?(path)
|
60
|
+
AbideDevUtils::Prompt.yes_no('File exists, would you like to overwrite?')
|
61
|
+
else
|
62
|
+
true
|
63
|
+
end
|
64
|
+
AbideDevUtils::Output.simple("Not overwriting file #{path}") unless continue
|
65
|
+
|
66
|
+
continue
|
67
|
+
end
|
68
|
+
|
69
|
+
def write_file(path, tmpl_path)
|
70
|
+
dir, = Pathname.new(path).split
|
71
|
+
Pathname.new(dir).mkpath unless Dir.exist?(dir)
|
72
|
+
content = render(tmpl_path)
|
73
|
+
File.open(path, 'w') { |f| f.write(content) } unless content.empty?
|
74
|
+
raise AbideDevUtils::Errors::Ppt::FailedToCreateFileError, path unless File.file?(path)
|
75
|
+
|
76
|
+
AbideDevUtils::Output.simple("Created file #{path}")
|
77
|
+
end
|
78
|
+
|
79
|
+
def build_obj; end
|
80
|
+
|
62
81
|
def class_vars
|
63
|
-
@tmpl_name = @opts.fetch(:tmpl_name, "#{@obj_type}.erb")
|
64
82
|
@root_dir = Pathname.new(@opts.fetch(:root_dir, Dir.pwd))
|
65
83
|
@tmpl_dir = if @opts.fetch(:absolute_template_dir, false)
|
66
84
|
@opts.fetch(:tmpl_dir)
|
67
85
|
else
|
68
|
-
"#{@
|
86
|
+
"#{@root_dir}/#{@opts.fetch(:tmpl_dir, 'object_templates')}"
|
69
87
|
end
|
70
|
-
@
|
71
|
-
@type_path_map = @opts.fetch(:type_path_map, {})
|
88
|
+
@tmpl_data = template_data(@opts.fetch(:tmpl_name, @obj_type))
|
72
89
|
@obj_path = new_obj_path
|
90
|
+
@spec_tmpl = @opts.fetch(:spec_template, File.expand_path(File.join(__dir__, '../resources/generic_spec.erb')))
|
73
91
|
end
|
74
92
|
|
75
93
|
def validate_class_vars
|
76
94
|
raise AbideDevUtils::Errors::PathNotDirectoryError, @root_dir unless Dir.exist? @root_dir
|
77
95
|
raise AbideDevUtils::Errors::PathNotDirectoryError, @tmpl_dir unless Dir.exist? @tmpl_dir
|
78
|
-
raise AbideDevUtils::Errors::Ppt::TemplateNotFoundError, @tmpl_path.to_s unless @tmpl_path.file?
|
79
96
|
end
|
80
97
|
|
81
98
|
def basename(obj_name)
|
82
99
|
obj_name.split('::')[-1]
|
83
100
|
end
|
84
101
|
|
85
|
-
def
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
102
|
+
def prefix
|
103
|
+
pfx = basename.match(OBJ_PREFIX)
|
104
|
+
return pfx[1] unless pfx.empty?
|
105
|
+
end
|
106
|
+
|
107
|
+
def templates
|
108
|
+
return [] if Dir.entries(tmpl_dir).empty?
|
109
|
+
|
110
|
+
file_names = Dir.entries(tmpl_dir).select { |f| f.match?(TMPL_PATTERN) }
|
111
|
+
file_names.map { |i| File.join(tmpl_dir, i) }
|
112
|
+
end
|
113
|
+
|
114
|
+
def template_data(query)
|
115
|
+
raise AbideDevUtils::Errors::Ppt::TemplateNotFoundError, @tmpl_dir if Dir.entries(@tmpl_dir).empty?
|
116
|
+
|
117
|
+
data = {}
|
118
|
+
pattern = /#{Regexp.quote(query)}/
|
119
|
+
templates.each do |i|
|
120
|
+
pn = Pathname.new(i)
|
121
|
+
next unless pn.basename.to_s.match?(pattern)
|
122
|
+
|
123
|
+
data[:path] = pn.to_s
|
124
|
+
data[:fname] = pn.basename.to_s
|
90
125
|
end
|
126
|
+
raise AbideDevUtils::Errors::Ppt::TemplateNotFoundError, @tmpl_dir unless data.key?(:fname)
|
127
|
+
|
128
|
+
data[:ext] = data[:fname].match?(VALID_EXT) ? data[:fname].match(VALID_EXT)[1] : '.pp'
|
129
|
+
data[:pfx] = data[:fname].match?(OBJ_PREFIX) ? data[:fname].match(OBJ_PREFIX)[1] : 'c-'
|
130
|
+
data[:spec_base] = PREFIX_TEST_PATH[data[:pfx]]
|
131
|
+
data[:obj_name] = normalize_obj_name(data.dup)
|
132
|
+
data[:spec_name] = "#{data[:obj_name].slice(/([^\s]+)(?:#{Regexp.quote(data[:ext])})/, 1)}_spec.rb"
|
133
|
+
data[:spec_path] = spec_path(data[:spec_base], data[:spec_name])
|
134
|
+
data
|
135
|
+
end
|
136
|
+
|
137
|
+
def normalize_obj_name(data)
|
138
|
+
new_name = data[:fname].slice(/^(?:#{Regexp.quote(data[:pfx])})?(?<name>[^\s.]+)(?:#{Regexp.quote(data[:ext])})?\.erb$/, 'name')
|
139
|
+
"#{new_name}#{data[:ext]}"
|
140
|
+
end
|
141
|
+
|
142
|
+
def render(path)
|
143
|
+
ERB.new(File.read(path), 0, '<>-').result(binding)
|
91
144
|
end
|
92
145
|
|
93
146
|
def namespace_format(name)
|
94
147
|
name.split(':').reject(&:empty?).join('::')
|
95
148
|
end
|
96
149
|
|
97
|
-
def
|
150
|
+
def new_obj_path
|
98
151
|
parts = @obj_name.split('::')[1..-2]
|
99
152
|
parts.insert(0, 'manifests')
|
100
153
|
parts.insert(-1, "#{basename(@obj_name)}#{DEFAULT_EXT}")
|
@@ -102,27 +155,13 @@ module AbideDevUtils
|
|
102
155
|
path.to_s
|
103
156
|
end
|
104
157
|
|
105
|
-
def
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
abs_path = Pathname.new(map_val).absolute? ? map_val : "#{Dir.pwd}/#{map_val}"
|
113
|
-
"#{abs_path}/#{basename(@obj_name)}#{DEFAULT_EXT}"
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
def custom_obj_path_from_hash(map_val, obj_name)
|
118
|
-
raise AbideDevUtils::Errors::Ppt::CustomObjPathKeyError, map_val unless map_val.key?(:path)
|
119
|
-
|
120
|
-
abs_path = Pathname.new(map_val[:path]).absolute? ? map_val[:path] : "#{Dir.pwd}/#{map_val[:path]}"
|
121
|
-
if map_val.key?(:extension)
|
122
|
-
"#{abs_path}/#{basename(obj_name)}#{map_val[:extension]}"
|
123
|
-
else
|
124
|
-
"#{abs_path}/#{basename(obj_name)}#{DEFAULT_EXT}"
|
125
|
-
end
|
158
|
+
def spec_path(base_dir, spec_name)
|
159
|
+
parts = @obj_name.split('::')[1..-2]
|
160
|
+
parts.insert(0, 'spec')
|
161
|
+
parts.insert(1, base_dir)
|
162
|
+
parts.insert(-1, spec_name)
|
163
|
+
path = @root_dir + Pathname.new(parts.join('/'))
|
164
|
+
path.to_s
|
126
165
|
end
|
127
166
|
end
|
128
167
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: abide_dev_utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Heston Snodgrass
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-04-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -278,6 +278,7 @@ files:
|
|
278
278
|
- lib/abide_dev_utils/ppt/coverage.rb
|
279
279
|
- lib/abide_dev_utils/ppt/new_obj.rb
|
280
280
|
- lib/abide_dev_utils/prompt.rb
|
281
|
+
- lib/abide_dev_utils/resources/generic_spec.erb
|
281
282
|
- lib/abide_dev_utils/utils/general.rb
|
282
283
|
- lib/abide_dev_utils/validate.rb
|
283
284
|
- lib/abide_dev_utils/version.rb
|