Class: ReleaseHx::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/releasehx/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/releasehx/cli.rb', line 10

def self.exit_on_failure?
  true
end

.start(original_args = ARGV, config = {}) ⇒ Object

======================================= OVERRIDE .start to handle no-arguments and default subcommand behavior =======================================


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/releasehx/cli.rb', line 18

def self.start original_args = ARGV, config = {}
  # If user gave no arguments at all, or only gave --help, allow that through
  if original_args.empty? || (original_args.length == 1 && original_args.first =~ /^--?h(elp)?$/)
    show_usage_snippet
    return
  elsif original_args.length == 1 && original_args.first =~ /^--man(page)?$/
    show_manpage
    return
  elsif original_args.length == 1 && original_args.first =~ /^--version$/
    puts ReleaseHx::VERSION
    return
  else
    first = original_args[0]
    original_args.unshift 'default' unless first.start_with?('-') || all_tasks.key?(first)
  end

  super
end

Instance Method Details

#default(source_arg) ⇒ Object

FIXME: This method is overly complex and handles too many concerns. It should be broken down into smaller methods, each handling a specific CLI action or workflow. A major refactor is planned for post-0.1.0.


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

def default source_arg
  setup_logger
  ReleaseHx.logger.debug "Starting ReleaseHx with version/source: #{source_arg}"
  load_and_configure_settings(ReleaseHx.attrs['app_default_config_path'])

  if options[:debug]
    begin
      config_dump = SgymlHelpers.deep_stringify_safe(@settings).to_yaml
      ReleaseHx.logger.debug "Operative config settings:\n#{config_dump}"
    rescue StandardError => e
      require 'pp' # pretty print
      ReleaseHx.logger.debug "Rescued config PP dump:\n#{PP.pp(@settings, +'')}"
      raise e
    end
  end

  source_arg_type = version_or_file(source_arg)
  if source_arg_type == :invalid
    raise Thor::Error, <<~ERRTXT
      ERROR: Invalid file extension for source file: #{source_arg}
             Valid draft file types are: #{ReleaseHx.attrs['draft_source_file_types']}
             Valid extensions are: #{ReleaseHx.attrs['draft_source_extensions']}
    ERRTXT
  end

  ReleaseHx.logger.info "Source type: #{source_arg_type}"

  if options[:verbose] && @settings['origin']
    ReleaseHx.logger.debug "✓ Source configured: #{@settings['origin']['source']}"
  end

  if options[:check]
    if source_arg_type == :file
      raise Thor::Error,
            'ERROR: Scan operations require a version number as the first argument.'
    end

    perform_scan(source_arg)
    return
  end

  if options[:api_data] && (options[:api_data].nil? || options[:api_data].empty?)
    raise Thor::Error, 'Must specify a PATH for --api-data. E.g. --api-data cached-1-1-1.json'
  end

  if options[:api_data] && !File.exist?(options[:api_data])
    raise Thor::Error, "API data file not found: #{options[:api_data]}"
  end

  if options[:api_data]
    ReleaseHx.logger.debug "✓ Using cached API data: #{options[:api_data]}"
  elsif options[:fetch]
    ReleaseHx.logger.info "✓ Will fetch fresh data from #{@settings['origin']['source']} API" if options[:verbose]
  end

  if options[:api_data] && options[:fetch]
    ReleaseHx.logger.warn 'Warning: --fetch ignored when --api-data is specified'
  end

  if [options[:adoc], options[:md], options[:yaml]].compact.size > 1
    raise Thor::Error, 'ERROR: Only one of --adoc, --md, or --yaml (or aliases) may be specified.'
  end

  if options[:append]
    perform_append(source_arg)
    return
  end

  determine_operations(source_arg)
end