qb 0.1.14 → 0.1.15

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eff9442acda19ad45e2a0fc85dde3fa21f34d33a
4
- data.tar.gz: 0ca507c08588f891232f983b45e10b953281dce8
3
+ metadata.gz: ae9aeb0e2f878eadc6e1cf220a2a7e26783fd358
4
+ data.tar.gz: c13dd57a5475abe283a69ca5b941b14df7cf7394
5
5
  SHA512:
6
- metadata.gz: c05d0cf7fad902ca2a4988170b614a1c21444bf51d0c16ce0e6c63b5a17a826a93014bf6ef65adefb5507cd9c4ecc04ef79f51725464e7612fb269eeb014393d
7
- data.tar.gz: 22d00588741f03b6044dddb945388e12e08f07428ed8168ce6faa5aeb6494f3e01afc64096e2947cde146dbb6c90a4b8233a19bcbcb48a0776dd884b76aa6df0
6
+ metadata.gz: 94c65c40bee65ff720d9cb28ab0e119a4da69f62d831e0dbef002e0f9b77f4649d78ee97e85ca9e4d8f650e92075c81a80d3479d3db880185238fde38e18bdd2
7
+ data.tar.gz: 851f4d7785b1b68310b7003393d2deb1a638345da45de47912c85651f931936a79013132f90c79307f435a65ecf89d50254c91fa7b3a4ecacb4fb73abc200998
data/exe/qb CHANGED
@@ -143,7 +143,15 @@ def main args
143
143
  saved_options_path = Pathname.new(dir) + '.qb-options.yml'
144
144
 
145
145
  saved_options = if saved_options_path.exist?
146
- YAML.load(saved_options_path.read).tap {|saved_options|
146
+ # convert old _ separated names to - separated
147
+ YAML.load(saved_options_path.read).map {|role_options_key, role_options|
148
+ [
149
+ role_options_key,
150
+ role_options.map {|name, value|
151
+ [QB::Options.cli_ize_name(name), value]
152
+ }.to_h
153
+ ]
154
+ }.to_h.tap {|saved_options|
147
155
  debug "found saved options", saved_options
148
156
  }
149
157
  else
@@ -156,8 +164,8 @@ def main args
156
164
 
157
165
  debug "found saved options for role", role_saved_options
158
166
 
159
- role_saved_options.each do |cli_option_name, value|
160
- option = options[cli_option_name]
167
+ role_saved_options.each do |option_cli_name, value|
168
+ option = options[option_cli_name]
161
169
 
162
170
  if option.value.nil?
163
171
  debug "setting from saved options", option: option, value: value
@@ -181,7 +189,7 @@ def main args
181
189
  }
182
190
 
183
191
  set_options.values.each do |option|
184
- playbook_role[option.ansible_var_name] = option.value
192
+ playbook_role[option.var_name] = option.value
185
193
  end
186
194
 
187
195
  playbook = [
@@ -216,12 +224,12 @@ def main args
216
224
  # save the options back
217
225
  if (
218
226
  # we set some options that we can save
219
- set_options.values.select {|o| o.save }.length > 0 &&
227
+ set_options.values.select {|o| o.save? }.length > 0 &&
220
228
  # the role says to save options
221
229
  role.save_options
222
230
  )
223
231
  saved_options[role.options_key] = set_options.select{|key, option|
224
- option.save
232
+ option.save?
225
233
  }.map {|key, option|
226
234
  [key, option.value]
227
235
  }.to_h
@@ -1,16 +1,8 @@
1
1
  require 'optparse'
2
+ require 'weakref'
2
3
 
3
4
  module QB
4
5
  module Options
5
- # small struct to hold the differnt names of the option now that including
6
- # makes it more complicated
7
- Option = Struct.new :qb_meta_name,
8
- :cli_option_name,
9
- :ansible_var_name,
10
- :required,
11
- :save,
12
- :value
13
-
14
6
  # errors
15
7
  # ======
16
8
 
@@ -26,129 +18,203 @@ module QB
26
18
  end
27
19
  end
28
20
 
29
- def self.include_role opts, options, include_var
30
- role_name = include_var['include']
21
+ # raised when there's bad option metadata
22
+ class MetadataError < Error
23
+ end
24
+
25
+ def self.cli_ize_name option_name
26
+ option_name.gsub '_', '-'
27
+ end
28
+
29
+ def self.var_ize_name option_name
30
+ option_name.gsub '-', '_'
31
+ end
32
+
33
+ #
34
+ class Option
35
+ # the role that this option is for
36
+ # attr_reader :role
37
+
38
+ # the entry from the qb metadata for this option
39
+ attr_reader :meta
40
+
41
+ # array of strings representing how this option was included
42
+ # empty for top-level options
43
+ attr_reader :include_path
44
+
45
+ # the name of the option in the qb metadata, equal to #meta['name']
46
+ attr_reader :meta_name
47
+
48
+ # the name that this option will be available in the cli as
49
+ attr_reader :cli_name
50
+
51
+ # the name that the value will be passed to ansible as
52
+ attr_reader :var_name
53
+
54
+ # the value of the option, or `nil` if we never assign one
55
+ attr_accessor :value
56
+
57
+ def initialize role, meta, include_path
58
+ # @role = WeakRef.new role
59
+ @meta = meta
60
+ @include_path = include_path
61
+
62
+ @meta_name = meta.fetch 'name'
63
+
64
+ @cli_name = if @include_path.empty?
65
+ Options.cli_ize_name @meta_name
66
+ else
67
+ "#{ @include_path.join('-') }-#{ Options.cli_ize_name @meta_name }"
68
+ end
69
+
70
+ @var_name = if role.var_prefix
71
+ "#{ role.var_prefix }_#{ Options.var_ize_name @meta_name }"
72
+ else
73
+ Options.var_ize_name @meta_name
74
+ end
75
+
76
+ @value = nil
77
+ end
78
+
79
+ # if the option is required in the cli
80
+ def required?
81
+ !!meta_or(['required', 'require'], false)
82
+ end
83
+
84
+ # if we should save the option value in .qb-options.yml
85
+ def save?
86
+ !!meta_or('save', true)
87
+ end
88
+
89
+ def description
90
+ value = meta_or 'description',
91
+ "set the #{ @var_name } role variable"
92
+
93
+ if @meta['type'].is_a?(Hash) && @meta['type'].key?('one_of')
94
+ line_break = "\n" + "\t" * 5
95
+ value += " options:" +
96
+ "#{ line_break }#{ @meta['type']['one_of'].join(line_break) }"
97
+ end
98
+
99
+ value
100
+ end
101
+
102
+ private
103
+
104
+ # get the value at the first found of the keys or the default.
105
+ def meta_or keys, default
106
+ keys = [keys] if keys.is_a? String
107
+ keys.each do |key|
108
+ if @meta.key? key
109
+ return @meta[key]
110
+ end
111
+ end
112
+ default
113
+ end
114
+
115
+ end # Option
116
+
117
+ def self.include_role opts, options, include_meta, include_path
118
+ role_name = include_meta['include']
31
119
  role = QB::Role.require role_name
32
- include_as = include_var['as'] || role.namespaceless
120
+ new_include_path = if include_meta.key? 'as'
121
+ case include_meta['as']
122
+ when nil, false
123
+ # include it in with the parent role's options
124
+ include_path
125
+ when String
126
+ include_path + [include_meta['as']]
127
+ else
128
+ raise MetadataError.new,
129
+ "bad 'as' value: #{ include_meta.inspect }"
130
+ end
131
+ else
132
+ include_path + [role.namespaceless]
133
+ end
33
134
 
34
- QB.debug "including #{ role.name } as #{ include_as }"
135
+ QB.debug "including #{ role.name } as #{ new_include_path.join('-') }"
35
136
 
36
- add opts, options, role, include_as
137
+ add opts, options, role, new_include_path
37
138
  end
38
139
 
39
140
  # add the options from a role to the OptionParser
40
- def self.add opts, options, role, include_as = nil
141
+ def self.add opts, options, role, include_path = []
41
142
  QB.debug "adding options", "role" => role
42
143
 
43
- role.vars.each do |var|
44
- if var.key? 'include'
45
- # we don't support nested includes
46
- unless include_as.nil?
47
- raise NestedIncludeError.new
48
- end
49
-
50
- include_role opts, options, var
144
+ role.options.each do |option_meta|
145
+ if option_meta.key? 'include'
146
+ include_role opts, options, option_meta, include_path
51
147
 
52
148
  else
53
149
  # create an option
150
+ option = Option.new role, option_meta, include_path
54
151
 
55
- # variable's name in meta
56
- qb_meta_name = var.fetch 'name'
57
-
58
- # name that will be used in QB cli
59
- cli_option_name = if include_as
60
- "#{ include_as }_#{ qb_meta_name }"
152
+ arg_style = if option.required?
153
+ :REQUIRED
61
154
  else
62
- qb_meta_name
155
+ :OPTIONAL
63
156
  end
64
157
 
65
- # name that is passed to ansible role
66
- ansible_var_name = "#{ role.var_prefix }_#{ qb_meta_name }"
67
-
68
- required = var['required'] || false
69
- save = if var.key? 'save'
70
- !!var['save']
71
- else
72
- true
73
- end
74
-
75
- option = options[cli_option_name] = Option.new qb_meta_name,
76
- cli_option_name,
77
- ansible_var_name,
78
- required,
79
- save,
80
- nil
81
-
82
- arg_style = required ? :REQUIRED : :OPTIONAL
83
-
84
158
  # on_args = [arg_style]
85
159
  on_args = []
86
160
 
87
- if var['type'] == 'boolean'
161
+ if option.meta['type'] == 'boolean'
88
162
  # don't use short names when included (for now)
89
- if include_as.nil? && var['short']
90
- on_args << "-#{ var['short'] }"
163
+ if include_path.empty? && option.meta['short']
164
+ on_args << "-#{ option.meta['short'] }"
91
165
  end
92
166
 
93
- on_args << "--[no-]#{ cli_option_name }"
167
+ on_args << "--[no-]#{ option.cli_name }"
94
168
 
95
169
  else
96
- ruby_type = case var['type']
170
+ ruby_type = case option.meta['type']
171
+ when nil
172
+ raise MetadataError,
173
+ "must provide type in qb metadata for option #{ option.meta_name }"
97
174
  when 'string'
98
175
  String
99
176
  when Hash
100
- if var['type'].key? 'one_of'
177
+ if option.meta['type'].key? 'one_of'
101
178
  klass = Class.new
102
179
  opts.accept(klass) {|value|
103
- if var['type']['one_of'].include? value
180
+ if option.meta['type']['one_of'].include? value
104
181
  value
105
182
  else
106
- raise ArgumentError, "argument '#{ cli_option_name }' must be " +
107
- "one of: #{ var['type']['one_of'].join(', ') }"
183
+ raise MetadataError,
184
+ "option '#{ option.cli_name }' must be one of: #{ option.meta['type']['one_of'].join(', ') }"
108
185
  end
109
186
  }
110
187
  klass
111
188
  else
112
- raise ArgumentError, "bad type: #{ var['type'].inspect }"
189
+ raise MetadataError,
190
+ "bad type for option #{ option.meta_name }: #{ option.meta['type'].inspect }"
113
191
  end
114
192
  else
115
- raise ArgumentError, "bad type: #{ var['type'].inspect }"
193
+ raise MetadataError,
194
+ "bad type for option #{ option.meta_name }: #{ option.meta['type'].inspect }"
116
195
  end
117
196
 
118
197
  # don't use short names when included (for now)
119
- if include_as.nil? && var['short']
120
- on_args << "-#{ var['short'] } #{ cli_option_name.upcase }"
198
+ if include_path.empty? && option.meta['short']
199
+ on_args << "-#{ option.meta['short'] } #{ option.cli_name.upcase }"
121
200
  end
122
201
 
123
- on_args << "--#{ cli_option_name }=#{ cli_option_name.upcase }"
202
+ on_args << "--#{ option.cli_name }=#{ option.cli_name.upcase }"
124
203
 
125
204
  on_args << ruby_type
126
205
  end
127
206
 
128
- # description
129
- description = if var.key? 'description'
130
- var['description']
131
- else
132
- "set the #{ ansible_var_name } variable"
133
- end
207
+ on_args << option.description
134
208
 
135
- if var['type'].is_a?(Hash) && var['type'].key?('one_of')
136
- lb = "\n" + "\t" * 5
137
- description += " options:" +
138
- "#{ lb }#{ var['type']['one_of'].join(lb) }"
139
- end
140
-
141
- on_args << description
142
-
143
- if role.defaults.key? ansible_var_name
144
- on_args << if var['type'] == 'boolean'
145
- if role.defaults[ansible_var_name]
146
- "default --#{ cli_option_name }"
209
+ if role.defaults.key? option.var_name
210
+ on_args << if option.meta['type'] == 'boolean'
211
+ if role.defaults[option.var_name]
212
+ "default --#{ option.cli_name }"
147
213
  else
148
- "default --no-#{ cli_option_name }"
214
+ "default --no-#{ option.cli_name }"
149
215
  end
150
216
  else
151
- "default = #{ role.defaults[ansible_var_name] }"
217
+ "default = #{ role.defaults[option.var_name] }"
152
218
  end
153
219
  end
154
220
 
@@ -161,6 +227,8 @@ module QB
161
227
 
162
228
  option.value = value
163
229
  end
230
+
231
+ options[option.cli_name] = option
164
232
  end
165
233
  end # each var
166
234
  end # add
@@ -183,11 +183,14 @@ module QB
183
183
  meta['var_prefix'] || namespaceless
184
184
  end
185
185
 
186
- # gets the vars from the metadata, defaulting to [] if noe defined.
187
- def vars
188
- meta['vars'] || []
186
+ # get the options from the metadata, defaulting to [] if none defined
187
+ def options
188
+ meta['options'] || meta['opts'] || meta['vars'] || []
189
189
  end
190
190
 
191
+ # old name
192
+ alias_method :vars, :options
193
+
191
194
  # loads the defaults from defaults/main.yml, caching by default
192
195
  def load_defaults cache = true
193
196
  defaults_path = @path + 'defaults' + 'main.yml'
@@ -1,7 +1,7 @@
1
1
  module QB
2
2
  GEM_NAME = 'qb'
3
3
 
4
- VERSION = "0.1.14"
4
+ VERSION = "0.1.15"
5
5
 
6
6
  def self.gemspec
7
7
  Gem.loaded_specs[GEM_NAME]
@@ -5,11 +5,3 @@ allow_duplicates: yes
5
5
 
6
6
  dependencies:
7
7
  - role: qb.role
8
- role_role_name: "{{ qb_role_name }}"
9
- role_defaults: "{{ qb_role_defaults }}"
10
- role_files: "{{ qb_role_files }}"
11
- role_handlers: "{{ qb_role_handlers }}"
12
- role_meta: "{{ qb_role_meta }}"
13
- role_tasks: "{{ qb_role_tasks }}"
14
- role_templates: "{{ qb_role_templates }}"
15
- role_vars: "{{ qb_role_vars }}"
@@ -10,38 +10,6 @@ var_prefix: null
10
10
  # how to get a default for `dir` if it's not provided as the
11
11
  default_dir: null
12
12
 
13
- vars:
14
- - name: name
15
- type: string
16
- description: name of the role.
17
- short: n
18
-
19
- - name: files
20
- type: boolean
21
- description: create a files directory.
22
- short: f
23
-
24
- - name: handlers
25
- type: boolean
26
- description: create handlers/main.yml
27
- short: a
28
-
29
- - name: meta
30
- type: boolean
31
- description: create meta/main.yml
32
- short: m
33
-
34
- - name: tasks
35
- type: boolean
36
- description: create tasks/main.yml
37
- short: t
38
-
39
- - name: templates
40
- type: boolean
41
- description: create a templates directory.
42
- short: e
43
-
44
- - name: vars
45
- type: boolean
46
- description: create vars/main.yml
47
- short: v
13
+ options:
14
+ - include: qb.role
15
+ as: null
@@ -1,5 +1,5 @@
1
1
  ---
2
- # meta/qb.yml file for {{ qb_role_name }}
2
+ # meta/qb.yml file for {{ role_role_name }}
3
3
  #
4
4
  # qb settings for this role. see README.md for more info.
5
5
  #
@@ -13,7 +13,7 @@ default_dir: null
13
13
  # set to false to not save options in {{ qb_dir }}/.qb-options.yml
14
14
  save_options: true
15
15
 
16
- vars: []
16
+ options: []
17
17
  # - name: example
18
18
  # description: an example of a variable.
19
19
  # required: false
@@ -9,23 +9,12 @@ role_tasks: true
9
9
  role_templates: false
10
10
  role_vars: false
11
11
 
12
- # switch to init a project repo for the role
13
- role_project: false
14
-
15
- # owner if the repo, which defauts to the `GITHUB_USER` env var
16
- role_project_owner: "{{ ansible_env.GITHUB_USER }}"
12
+ # galaxy
13
+ role_galaxy: false
14
+ role_author: "{{ git_user_name }}"
15
+ role_description: "{{ role_role_name }} ansible role."
16
+ role_license: "BSD"
17
+ role_min_ansible_version: 2.1.2.0
17
18
 
18
- # the name of the project repo, which defaults to "ansible-<role-name>"
19
- role_project_name: "ansible-{{ role_role_name }}"
20
-
21
- # rest of the project variables default to false except readme
22
- role_project_bin: false
23
- role_project_dev: false
24
- role_project_dev_bin: false
25
- role_project_setup: false
26
- role_project_scratch: false
27
- role_project_tmp: false
28
- role_project_readme: true
29
- role_project_hub: false
30
- role_project_private: false
31
- role_project_force: false
19
+ # init a project repo for the role
20
+ role_project: false
@@ -3,16 +3,4 @@
3
3
 
4
4
  dependencies:
5
5
  - role: qb.project
6
- project_owner: "{{ role_project_owner }}"
7
- project_name: "{{ role_project_name }}"
8
- project_bin: "{{ role_project_bin }}"
9
- project_dev: "{{ role_project_dev }}"
10
- project_dev_bin: "{{ role_project_dev_bin }}"
11
- project_setup: "{{ role_project_setup }}"
12
- project_scratch: "{{ role_project_scratch }}"
13
- project_tmp: "{{ role_project_tmp }}"
14
- project_readme: "{{ role_project_readme }}"
15
- project_hub: "{{ role_project_hub }}"
16
- project_private: "{{ role_project_private }}"
17
- project_force: "{{ role_project_force }}"
18
6
  when: role_project
@@ -16,6 +16,11 @@ vars:
16
16
  description: name of the role.
17
17
  short: n
18
18
 
19
+ - name: defaults
20
+ type: boolean
21
+ description: create a defaults directory.
22
+ short: d
23
+
19
24
  - name: files
20
25
  type: boolean
21
26
  description: create a files directory.
@@ -46,19 +51,15 @@ vars:
46
51
  description: create vars/main.yml
47
52
  short: v
48
53
 
54
+ - name: galaxy
55
+ type: boolean
56
+ description: include galaxy info in meta/main.yml
57
+ short: g
58
+
49
59
  - name: project
50
60
  type: boolean
51
61
  description: create a project repo for this role
62
+ short: p
52
63
 
53
- - name: project_owner
54
- type: string
55
- description: the github owner of the project repo
56
-
57
- - name: project_name
58
- type: string
59
- description: the github name of the project repo
60
-
61
- - name: project_hub
62
- type: boolean
63
- description: switch to `hub create` the project repo
64
+ - include: qb.project
64
65
 
@@ -5,3 +5,11 @@ allow_duplicates: yes
5
5
 
6
6
  dependencies: []
7
7
  # - role: role-name
8
+
9
+ {% if role_galaxy %}
10
+ galaxy_info:
11
+ author: {{ role_author | to_json }}
12
+ description: {{ role_description | to_json }}
13
+ license: {{ role_license | to_json }}
14
+ min_ansible_version: {{ role_min_ansible_version | to_json }}
15
+ {% endif %}
@@ -0,0 +1,20 @@
1
+ ---
2
+ # vars file for qb.role
3
+
4
+ # overriding the qb.project defaults has to happen here - values in
5
+ # defaults/main.yml **will not** be visible to the included role
6
+
7
+ # the name of the project repo, which defaults to "ansible-<role-name>"
8
+ project_name: "ansible-{{ role_role_name }}"
9
+
10
+ # rest of the project variables default to false except readme
11
+ project_bin: false
12
+ project_dev: false
13
+ project_dev_bin: false
14
+ project_setup: false
15
+ project_scratch: false
16
+ project_tmp: false
17
+ project_readme: true
18
+ project_hub: false
19
+ project_private: false
20
+ project_force: false
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.14
4
+ version: 0.1.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - nrser
@@ -440,7 +440,6 @@ files:
440
440
  - roles/qb.project/templates/bootstrap.yml.j2
441
441
  - roles/qb.project/templates/setup.yml.j2
442
442
  - roles/qb.qb_role/.qb-options.yml
443
- - roles/qb.qb_role/defaults/main.yml
444
443
  - roles/qb.qb_role/meta/main.yml
445
444
  - roles/qb.qb_role/meta/qb.yml
446
445
  - roles/qb.qb_role/tasks/main.yml
@@ -461,6 +460,7 @@ files:
461
460
  - roles/qb.role/templates/meta/main.yml.j2
462
461
  - roles/qb.role/templates/tasks/main.yml.j2
463
462
  - roles/qb.role/templates/vars/main.yml.j2
463
+ - roles/qb.role/vars/main.yml
464
464
  - temp.yml
465
465
  - tmp/.gitkeep
466
466
  homepage: https://github.com/nrser/qb
@@ -1,10 +0,0 @@
1
- ---
2
- # defaults file for qb.qb_role
3
- qb_role_name: "{{ dir | basename }}"
4
- qb_role_defaults: true
5
- qb_role_files: false
6
- qb_role_handlers: false
7
- qb_role_meta: true
8
- qb_role_tasks: true
9
- qb_role_templates: false
10
- qb_role_vars: false