Class: Simp::Cli::Config::Questionnaire

Inherits:
Object
  • Object
show all
Defined in:
lib/simp/cli/config/questionnaire.rb

Overview

Builds a SIMP configuration profile based on an Array of Config::Items

The configuration profile is built on a Questionnaire, which is interactive by default, but can be automated.

Constant Summary collapse

INTERACTIVE =
0
NONINTERACTIVE =
1
REALLY_NONINTERACTIVE =
2

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Questionnaire

Returns a new instance of Questionnaire.



19
20
21
22
23
24
# File 'lib/simp/cli/config/questionnaire.rb', line 19

def initialize( options = {} )
  @options = {
   :noninteractive          => INTERACTIVE,
   :verbose                 => 0
  }.merge( options )
end

Instance Method Details



81
82
83
84
85
# File 'lib/simp/cli/config/questionnaire.rb', line 81

def print_invalid_item_error item
  error =  "ERROR: '#{item.value}' is not a valid value for #{item.key}"
  error += "\n#{item.not_valid_message}" if item.not_valid_message
  say "<%= color(%q{#{error}}, RED) %>\n"
end

#process(item_queue = [], answers = {}) ⇒ Object

processes an Array of Config::Items and returns a hash of Config::Item answers



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/simp/cli/config/questionnaire.rb', line 29

def process( item_queue=[], answers={} )
  if item = item_queue.shift
    item.config_items = answers
    process_item item

    # add (or replace) this item's answer to the answers list
    answers[ item.key ] = item

    # add any next_items to the queue
    item_queue = item.next_items + item_queue

    process item_queue, answers
  end

  answers
end

#process_item(item) ⇒ Object

process a Config::Item

simp config can run in the following modes:

- interactive (prompt each item)
- mostly non-interactive (-f/-A; prompt items that can't be inferred)
- never prompt (-a/-ff);
- never prompt (-ff; relies on cli args for non-inferrable items))


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
# File 'lib/simp/cli/config/questionnaire.rb', line 54

def process_item item
  item.skip_query = true if @options[ :noninteractive ] >= NONINTERACTIVE
  if @options.fetch( :fail_on_missing_answers, false )
    item.fail_on_missing_answer = true
  end

  if @options[ :noninteractive ] == INTERACTIVE
    item.query
  else
    value = item.default_value

    if item.validate( value )
      item.value = value
      item.print_summary if @options.fetch( :verbose ) >= 0
    else
      # alert user that the value is wrong
      print_invalid_item_error item

      # present an interactive prompt for invalid answers unless '-ff'
      exit 1 if @options.fetch( :noninteractive ) >= REALLY_NONINTERACTIVE
      item.skip_query = false
      value = item.query
    end
  end
  item.safe_apply
end