Class: Simp::Cli::Config::Item::AnswersYAMLFileWriter

Inherits:
ActionItem show all
Defined in:
lib/simp/cli/config/item/answers_yaml_file_writer.rb

Instance Attribute Summary collapse

Attributes inherited from Simp::Cli::Config::Item

#allow_user_apply, #config_items, #description, #die_on_apply_fail, #fact, #fail_on_missing_answer, #key, #next_items_tree, #silent, #skip_apply, #skip_query, #skip_yaml, #value

Instance Method Summary collapse

Methods inherited from ActionItem

#query, #to_yaml_s, #validate

Methods included from SafeApplying

#safe_apply

Methods inherited from Simp::Cli::Config::Item

#default_value, #highline_question_type, #next_items, #not_valid_message, #os_value, #print_banner, #print_summary, #puppet_value, #query, #query_ask, #query_extras, #query_status, #recommended_value, #safe_apply, #say_blue, #say_green, #say_red, #say_yellow, #to_yaml_s, #validate

Constructor Details

#initializeAnswersYAMLFileWriter

Returns a new instance of AnswersYAMLFileWriter.



12
13
14
15
16
17
18
# File 'lib/simp/cli/config/item/answers_yaml_file_writer.rb', line 12

def initialize
  super
  @key             = 'yaml::file_writer'
  @description     = %Q{Writes Config::Item answers so far to YAML file (apply-only; noop).}
  @file            = '/etc/puppet/environments/simp/hieradata/simp_def.yaml'
  @backup_old_file = false
end

Instance Attribute Details

#backup_old_fileObject

Returns the value of attribute backup_old_file.



10
11
12
# File 'lib/simp/cli/config/item/answers_yaml_file_writer.rb', line 10

def backup_old_file
  @backup_old_file
end

#fileObject

Returns the value of attribute file.



10
11
12
# File 'lib/simp/cli/config/item/answers_yaml_file_writer.rb', line 10

def file
  @file
end

Instance Method Details

#applyObject



54
55
56
# File 'lib/simp/cli/config/item/answers_yaml_file_writer.rb', line 54

def apply
  write_answers_yaml_file( @file, @config_items ) if @config_items.size > 0
end

prints an answers file to an iostream



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/simp/cli/config/item/answers_yaml_file_writer.rb', line 22

def print_answers_yaml( iostream, answers )
  iostream.puts "#======================================="
  iostream.puts "# simp config answers"
  iostream.puts "#"
  iostream.puts "# generated on #{Time.now.to_s}"
  iostream.puts "#---------------------------------------"
  iostream.puts "# you can use these answers to quickly configure subsequent simp installations
                 # by running the command:
                 #
                 #   simp config -a /PATH/TO/THIS/FILE
                 #
                 # simp config will prompt for any missing items
                 ".gsub(/^\s+/, '').strip
  iostream.puts "#======================================="
  iostream.puts "---"
  answers.each do |k,v|
    if yaml = v.to_yaml_s  # filter out nil results (for ruby 1.8)
      iostream.puts yaml
      iostream.puts
    end
  end
end

#write_answers_yaml_file(file, answers) ⇒ Object

write a file



47
48
49
50
51
# File 'lib/simp/cli/config/item/answers_yaml_file_writer.rb', line 47

def write_answers_yaml_file( file, answers )
  say_green "Writing answers to: #{file}" if !@silent
  FileUtils.mkdir_p( File.dirname( file ) )
  File.open( file, 'w' ){ |fh| print_answers_yaml( fh, answers ) }
end