-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcli.rb
More file actions
executable file
·233 lines (183 loc) · 6.61 KB
/
cli.rb
File metadata and controls
executable file
·233 lines (183 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'thor'
module Facter
class Cli < Thor
class_option :color,
type: :boolean,
desc: 'Enable color output.'
class_option :no_color,
type: :boolean,
desc: 'Disable color output.'
class_option :config,
aliases: '-c',
type: :string,
desc: 'The location of the config file.'
class_option :custom_dir,
type: :string,
repeatable: true,
desc: 'A directory to use for custom facts.'
class_option :debug,
aliases: '-d',
type: :boolean,
desc: 'Enable debug output.'
class_option :external_dir,
type: :string,
repeatable: true,
desc: 'A directory to use for external facts.'
class_option :hocon,
type: :boolean,
desc: 'Output in Hocon format.'
class_option :json,
aliases: '-j',
type: :boolean,
desc: 'Output in JSON format.'
class_option :log_level,
aliases: '-l',
type: :string,
desc: 'Set logging level. Supported levels are: none, trace, debug, info, warn, error, and fatal.'
class_option :no_block,
type: :boolean,
desc: 'Disable fact blocking.'
class_option :no_cache,
type: :boolean,
desc: 'Disable loading and refreshing facts from the cache'
class_option :no_custom_facts,
type: :boolean,
desc: 'Disable custom facts.'
class_option :no_external_facts,
type: :boolean,
desc: 'Disable external facts.'
class_option :no_ruby,
type: :boolean,
desc: 'Disable loading Ruby, facts requiring Ruby, and custom facts.'
class_option :trace,
type: :boolean,
desc: 'Enable backtraces for custom facts.'
class_option :verbose,
type: :boolean,
desc: 'Enable verbose (info) output.'
class_option :show_legacy,
type: :boolean,
desc: 'Show legacy facts when querying all facts.'
class_option :yaml,
aliases: '-y',
type: :boolean,
desc: 'Output in YAML format.'
class_option :strict,
type: :boolean,
desc: 'Enable more aggressive error reporting.'
class_option :timing,
type: :boolean,
aliases: '-t',
desc: 'Show how much time it took to resolve each fact'
class_option :sequential,
type: :boolean,
desc: 'Resolve facts sequentially'
class_option :http_debug,
type: :boolean,
desc: 'Whether to write HTTP request and responses to stderr. This should never be used in production.'
class_option :puppet,
type: :boolean,
aliases: '-p',
desc: 'Load the Puppet libraries, thus allowing Facter to load Puppet-specific facts.'
desc '--man', 'Display manual.', hide: true
map ['--man'] => :man
def man(*args)
require 'erb'
negate_options = %w[block cache custom_facts external_facts]
template = File.join(File.dirname(__FILE__), '..', '..', 'templates', 'man.erb')
erb = ERB.new(File.read(template), nil, '-')
erb.filename = template
puts erb.result(binding)
end
desc 'query', 'Default method', hide: true
def query(*args)
Facter.puppet_facts if options[:puppet]
output, status = Facter.to_user_output(@options, *args)
puts output
status = 1 if Facter::Log.errors?
exit status
end
desc 'arg_parser', 'Parse arguments', hide: true
def arg_parser(*args)
# ignore unknown options
args.reject! { |arg| arg.start_with?('-') }
Facter.values(@options, args)
end
desc '--version, -v', 'Print the version'
map ['--version', '-v'] => :version
def version(*_args)
puts Facter::VERSION
end
desc '--list-block-groups', 'List block groups'
map ['--list-block-groups'] => :list_block_groups
def list_block_groups
options = @options.map { |(k, v)| [k.to_sym, v] }.to_h
Facter::Options.init_from_cli(options)
block_groups = Facter::FactGroups.new.groups.to_yaml.lines[1..-1].join
block_groups.gsub!(/:\s*\n/, "\n")
puts block_groups
end
desc '--list-cache-groups', 'List cache groups'
map ['--list-cache-groups'] => :list_cache_groups
def list_cache_groups
options = @options.map { |(k, v)| [k.to_sym, v] }.to_h
Facter::Options.init_from_cli(options)
cache_groups = Facter::FactGroups.new.groups.to_yaml.lines[1..-1].join
cache_groups.gsub!(/:\s*\n/, "\n")
puts cache_groups
end
desc '--help, -h', 'Help for all arguments'
def help(*args)
help_string = +''
help_string << help_header(args)
help_string << add_class_options_to_help
help_string << add_commands_to_help
puts help_string
end
no_commands do
def help_header(_args)
path = File.join(File.dirname(__FILE__), '../../')
Facter::Util::FileHelper.safe_read("#{path}fixtures/facter_help_header")
end
def add_class_options_to_help
help_class_options = +''
class_options = Cli.class_options
class_options.each do |class_option|
option = class_option[1]
next if option.hide
help_class_options << build_option(option.name, option.aliases, option.description)
end
help_class_options
end
def add_commands_to_help
help_command_options = +''
Cli.commands.values
.select { |command_class| command_class.instance_of?(Thor::Command) }
.each do |command|
help_command_options << build_option(
command['name'],
[command['usage'].split(',')[1]],
command['description']
)
end
help_command_options
end
def build_option(name, aliases, description)
name = name.tr('_', '-')
help_option = +''
help_option << aliases.join(',').rjust(10)
help_option << ' '
help_option << "[--#{name}]".ljust(30)
help_option << " #{description}"
help_option << "\n"
help_option
end
end
def self.exit_on_failure?
true
end
default_task :query
end
end