module Opencontrol::CLI

This module holds the Opencontrol Linter Command Line Interface.

Constants

ALIASES
CONFIG_FILENAME
PRESET
USAGE_TEXT

Public Class Methods

add_target(type, opts, specification) click to toggle source
# File lib/opencontrol/cli.rb, line 95
def self.add_target(type, opts, specification)
  if use_default_pattern?(opts, type)
    specification[:targets][type] = default_spec[:targets][type]
  elsif opts[type].is_a?(String)
    specification[:targets][type] = [opts[type]]
  end
end
all_selected?(opts, specification) click to toggle source
# File lib/opencontrol/cli.rb, line 162
def self.all_selected?(opts, specification)
  opts[:all] || all_targets_empty?(specification)
end
all_targets_empty?(specification) click to toggle source
# File lib/opencontrol/cli.rb, line 155
def self.all_targets_empty?(specification)
  specification[:targets][:components].empty? &&
    specification[:targets][:standards].empty? &&
    specification[:targets][:certifications].empty? &&
    specification[:targets][:opencontrols].empty?
end
construct_defaults(config) click to toggle source
# File lib/opencontrol/cli.rb, line 114
def self.construct_defaults(config)
  spec = {
    action: :run,
    targets: {}.merge(PRESET[:targets].dup).merge(config[:targets])
  }

  expand_components_filenames(spec)
end
default_spec() click to toggle source
# File lib/opencontrol/cli.rb, line 143
def self.default_spec
  if opencontrol_yaml_present?
    construct_defaults(load_config_from_yaml)
  else
    PRESET.dup
  end
end
expand_components_filenames(spec) click to toggle source
# File lib/opencontrol/cli.rb, line 134
def self.expand_components_filenames(spec)
  # the config file usually omits the component files full filename
  spec[:targets][:components] = spec[:targets][:components].collect do |f|
    f += '/component.yaml' if File.directory?(f)
    f
  end
  spec
end
load_config_from_yaml() click to toggle source
# File lib/opencontrol/cli.rb, line 123
def self.load_config_from_yaml
  yaml_config = YAML.load_file(CONFIG_FILENAME)
  yaml_config = Hash[yaml_config.map { |(k, v)| [k.to_sym, v] }]
  {
    action: :run,
    targets: yaml_config.select do |k, _v|
               %i[components standards certifications].include?(k)
             end
  }
end
opencontrol_yaml_present?() click to toggle source
# File lib/opencontrol/cli.rb, line 110
def self.opencontrol_yaml_present?
  File.exist?(CONFIG_FILENAME)
end
parse_args(arguments) click to toggle source
# File lib/opencontrol/cli.rb, line 170
def self.parse_args(arguments)
  opts = Rationalist.parse(arguments, alias: ALIASES)
  specification = {
    action: :run,
    targets: Opencontrol::Linter.empty_targets
  }
  specification[:action] = :version                     if opts[:version]
  specification[:action] = :help                        if opts[:help]
  add_target(:components, opts, specification)     if opts[:components]
  add_target(:standards, opts, specification)      if opts[:standards]
  add_target(:certifications, opts, specification) if opts[:certifications]
  add_target(:opencontrols, opts, specification)   if opts[:opencontrols]
  specification = default_spec if use_default?(opts, specification)
  specification
end
run_with_args(args) click to toggle source
# File lib/opencontrol/cli.rb, line 186
def self.run_with_args(args)
  specification = parse_args(args)
  result = 0
  case specification[:action]
  when :run
    result = Opencontrol::Linter.run(specification)
  when :version
    result = show_version
  when :help
    result = show_help
  end
  exit(result)
end
should_lint?(opts) click to toggle source
# File lib/opencontrol/cli.rb, line 151
def self.should_lint?(opts)
  !(opts[:version] || opts[:help])
end
show_help() click to toggle source
# File lib/opencontrol/cli.rb, line 84
def self.show_help
  puts USAGE_TEXT
  0 # exit with no error
end
show_version() click to toggle source
# File lib/opencontrol/cli.rb, line 89
def self.show_version
  puts 'Opencontrol linter version: v' + Opencontrol::Version.version
  puts 'CMS 2019 Adrian Kierman '
  0 # exit with no error
end
use_default?(opts, specification) click to toggle source
# File lib/opencontrol/cli.rb, line 166
def self.use_default?(opts, specification)
  all_selected?(opts, specification) && should_lint?(opts)
end
use_default_pattern?(opts, type) click to toggle source
# File lib/opencontrol/cli.rb, line 103
def self.use_default_pattern?(opts, type)
  # this is set when the user uses a flag on the command line but doesnt
  # add a specific file pattern - this way the user can restrict to just
  # one type
  opts[type] == true
end