Module: CemAcpt::Cli

Defined in:
lib/cem_acpt/cli.rb

Constant Summary collapse

OPTIONS_DESC =
[
  'Set options. Example: -O "param1=value1,param2=value2"',
  'For nested options, use dot notation to specify the nesting. For example,',
  '-O "nested.param1=value1,nested.param2=value2"',
  'Use this to set any config option or other runtime option. For example, to set the max_run_duration for a test',
  'node, you would use: -O "node_data.max_run_duration=<duration in seconds>"',
].freeze

Class Method Summary collapse

Class Method Details

.parse_opts_for(command) ⇒ Object



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
# File 'lib/cem_acpt/cli.rb', line 15

def self.parse_opts_for(command)
  cmd = command
  options = {}
  parser = OptionParser.new do |opts|
    opts.banner = "Usage: #{command} [options]"

    opts.on('-h', '--help', 'Show this help message') do
      puts opts
      exit 0
    end

    # Command specific options
    case command
    when :cem_acpt
      opts.on('-m', '--module-dir DIR', 'Set module directory. Example: -m "/tmp/module"') do |m|
        options[:module_dir] = m
      end

      opts.on('-a', '--only-actions ACTIONS', 'Set actions. Example: -a "acpt,noop"') do |a|
        options[:actions] ||= {}
        options[:actions][:only] = a.split(',')
      end

      opts.on('-A', '--except-actions ACTIONS', 'Set excluded actions. Example: -A "noop,idempotent"') do |a|
        options[:actions] ||= {}
        options[:actions][:except] = a.split(',')
      end

      opts.on('-t', '--tests TESTS', 'Set tests. Example: -t "test1,test2"') do |t|
        options[:tests] = t.split(',')
      end

      opts.on('-B', '--no-bolt-tests', 'Do not run Bolt tests') do
        options[:bolt] ||= {}
        options[:bolt][:tests] ||= {}
        options[:bolt][:tests][:enabled] = false
      end

      opts.on('--bolt-keep-inventory', 'Keep the Bolt inventory after tests') do
        options[:bolt] ||= {}
        options[:bolt][:keep_inventory] = true
      end

      opts.on('--bolt-keep-project', 'Keep the Bolt project after tests') do
        options[:bolt] ||= {}
        options[:bolt][:keep_project] = true
      end

      opts.on('--windows-bucket BUCKET', 'GCS bucket name for the Windows test path. Example: --windows-bucket "my-win-bucket"') do |b|
        options[:platform] ||= {}
        options[:platform][:gcp] ||= {}
        options[:platform][:gcp][:windows_bucket] = b
      end
    when :cem_acpt_image
      opts.on('--dry-run', 'Logs the information for the images that would be created. Does not create images.') do
        options[:dry_run] = true
      end

      opts.on('--no-build-images', 'Do not build images from the provisioned nodes.') do
        options[:no_build_images] = true
      end

      opts.on('--provision-only', 'Provision the nodes only. Equivalent to --no-build-images and --no-destroy-nodes.') do
        options[:no_build_images] = true
        options[:no_destroy_nodes] = true
      end

      opts.on('--no-linux', 'Do not build Linux images') do
        options[:cem_acpt_image] ||= {}
        options[:cem_acpt_image][:no_linux] = true
      end

      opts.on('--no-windows', 'Do not build Windows images') do
        options[:cem_acpt_image] ||= {}
        options[:cem_acpt_image][:no_windows] = true
      end

      opts.on('-F', '--filter FILTER', 'Filter images to build by name with regex. Example: -F "(alma|rhel)-8"') do |f|
        options[:image_name_filter] = Regexp.new(f)
      end
    end

    opts.on('-D', '--debug', 'Enable debug logging') do
      options[:log_level] = 'debug'
    end

    opts.on('-L', '--log-file FILE', 'Log to FILE') do |file|
      options[:log_file] = file
    end

    opts.on('-O', '--options OPTS', OPTIONS_DESC.join(' ')) do |o|
      params = o.split(',').map { |s| s.split('=', 2) }.to_h
      params.each do |k, v|
        keys = k.split('.') # Split the option key into parts for nested hashes
        last_key = keys.pop # The last part is the actual key to set the value for
        nested_hash = keys.reduce(options) do |h, key|
          h[key.to_sym] ||= {} # Create nested hash if it doesn't exist
          h[key.to_sym] # Return the nested hash for the next iteration
        end
        nested_hash[last_key.to_sym] = v # Set the value for the last key
      end
    end

    opts.on('-p', '--platform PLATFORM', 'Set platform. Example: -p "gcp"') do |p|
      options[:platform] = p
    end

    opts.on('-c', '--config-file FILE', 'Set config file. Example: -c "/tmp/config.yaml"') do |c|
      options[:config_file] = c
    end

    opts.on('-I', '--CI', 'Run in CI mode (modifies logging for Github Actions output)') do
      options[:ci_mode] = true
      options[:log_format] = 'github_action'
    end

    opts.on('-E', '--no-destroy-nodes', 'Do not destroy nodes') do
      options[:no_destroy_nodes] = true
    end

    opts.on(
      '-q', '--quiet',
      'Drop stdout (requires --log-file outside CI; CI mode keeps stdout for ::group:: support)',
    ) do
      options[:quiet] = true
    end

    opts.on('-v', '--verbose', 'Enables verbose logging mode') do
      options[:verbose] = true
    end

    opts.on('-S', '--no-ephemeral-ssh-key', 'Do not generate an ephemeral SSH key for test suites') do
      options[:no_ephemeral_ssh_key] = true
    end

    opts.on('-V', '--version', 'Show the cem_acpt version') do
      cmd = :version
    end

    opts.on('-Y', '--print-yaml-config', 'Loads and prints the config as YAML. Other specified options will be added to the config.') do
      cmd = :print_yaml_config
    end

    opts.on('-X', '--explain-config', 'Loads and prints the config explanation. Other specified options will be added to the config.') do
      cmd = :print_explain_config
    end

    opts.on('--trace', 'Enables trace output') do
      options[:trace] = true
    end

    opts.on(
      '--trace-events [EVENTS]',
      [
        'Comma-separated list of trace events to enable. Example: --trace-events "line,call,b_call,thread_begin,thread_end"',
        'See here for list of events: https://ruby-doc.org/core-2.5.0/TracePoint.html#class-TracePoint-label-Events.',
        'Default: line,call,b_call,thread_begin,thread_end',
      ].join(' '),
    ) do |events|
      options[:trace_events] = events.split(',')
    end

    opts.on('--puppet-no-debug', 'Disables Puppet debug logging') do
      options[:puppet] ||= {}
      options[:puppet][:no_debug] = true
    end

    opts.on('--puppet-no-verbose', 'Disables Puppet verbose logging') do
      options[:puppet] ||= {}
      options[:puppet][:no_verbose] = true
    end
  end
  parser.parse!
  [cmd, options]
end