Class: MintPress::Resources::FileUtils

Inherits:
Resource
  • Object
show all
Includes:
MintLogger, Mixins::Properties
Defined in:
src/mintpress-resources/lib/mintpress-resources/fileutils.rb

Overview

This resource allows you to make line edits to files. It's generally recommended to use the augeas resources if possible, since it is more robust, however this one provides a simpler, more user servicable implementation.

Properties (Read/Write)

  • #name (Default Value: "#{self.class.name}_#{self.object_id}")

    Property Attributes
    • default"#{self.class.name}_#{self.object_id}"
    
    
    28
    # File 'src/mintpress-resources/lib/mintpress-resources/fileutils.rb', line 28
    
    property :name, default: "#{self.class.name}_#{self.object_id}"
  • #file ⇒ Pathname
    'The path of the file to work on.'

    Property Attributes
    • fromString
    • requiredtrue
    
    
    30
    # File 'src/mintpress-resources/lib/mintpress-resources/fileutils.rb', line 30
    
    property :file, type: Pathname, from: String, required: true, description: 'The path of the file to work on.'
  • #backup_file ⇒ [ TrueClass, FalseClass] (Default Value: false)
    'Should we backup the original file. If we do, we place it in the same directory with a timestamp.'

    Property Attributes
    • requiredtrue
    • defaultfalse
    
    
    31
    # File 'src/mintpress-resources/lib/mintpress-resources/fileutils.rb', line 31
    
    property :backup_file, type: [ TrueClass, FalseClass], required: true, default: false, description: 'Should we backup the original file. If we do, we place it in the same directory with a timestamp.'
  • #pattern ⇒ [String, Regexp]
    'A Regex pattern represented in a string or Regexp format.'

    Property Attributes
    
    
    32
    # File 'src/mintpress-resources/lib/mintpress-resources/fileutils.rb', line 32
    
    property :pattern, type: [String, Regexp], description: 'A Regex pattern represented in a string or Regexp format.'
  • #line ⇒ String
    'Line/text to add or replace if the pattern is found (applicable only for certain methods)'

    Property Attributes
    
    
    33
    # File 'src/mintpress-resources/lib/mintpress-resources/fileutils.rb', line 33
    
    property :line, type: String, description: 'Line/text to add or replace if the pattern is found (applicable only for certain methods)'
  • #escape_pattern ⇒ [TrueClass, FalseClass] (Default Value: false)
    'Should we escape the value supplied by the pattern attribute and treat it as a normal String instead, e.g. if the property pattern has special characters, setting this attribute to true will make MintPress treat those special characters as normal strings. This is not applicable if the input is a Regexp, only applies if the input is a String.'

    Property Attributes
    • defaultfalse
    
    
    34
    # File 'src/mintpress-resources/lib/mintpress-resources/fileutils.rb', line 34
    
    property :escape_pattern, type: [TrueClass, FalseClass], default: false, description: 'Should we escape the value supplied by the pattern attribute and treat it as a normal String instead, e.g. if the property pattern has special characters, setting this attribute to true will make MintPress treat those special characters as normal strings. This is not applicable if the input is a Regexp, only applies if the input is a String.'
  • #escape_line ⇒ [TrueClass, FalseClass] (Default Value: false)
    'Same as escape_pattern but for line attribute. This is mostly used when the line being inserted has special characters. Mint will not escape the special characters when inserting the line but will escape the characters when searching for the line being present. This helps in cases when dealing with say authorized_files entries.'

    Property Attributes
    • defaultfalse
    
    
    35
    # File 'src/mintpress-resources/lib/mintpress-resources/fileutils.rb', line 35
    
    property :escape_line, type: [TrueClass, FalseClass], default: false, description: 'Same as escape_pattern but for line attribute. This is mostly used when the line being inserted has special characters. Mint will not escape the special characters when inserting the line but will escape the characters when searching for the line being present. This helps in cases when dealing with say authorized_files entries.'
  • #all_occurences ⇒ [TrueClass, FalseClass] (Default Value: true)
    'Setting this to true will replace or delete all occurrences of the pattern in the file. Default is to replace/delete only first occurence.'

    Property Attributes
    • defaulttrue
    
    
    36
    # File 'src/mintpress-resources/lib/mintpress-resources/fileutils.rb', line 36
    
    property :all_occurences, type: [TrueClass, FalseClass], default: true, description: 'Setting this to true will replace or delete all occurrences of the pattern in the file. Default is to replace/delete only first occurence.'
  • #name ⇒ String
    Things like tmpdir and so on?

    Property Attributes
    • requiredtrue
    
    
    38
    # File 'src/mintpress-resources/lib/mintpress-resources/resource.rb', line 38
    
    property :name, type: String, required: true
  • #host ⇒ MintPress::Infrastructure::Host (Default Value: Proc.new { r=nil if self.default_localhost r = MintPress::Infrastructure::Localhost.new(final_user: self.default_localhost_user) end r })
    Host on which to operate. Depending on your system, this will usually default to localhost.

    Property Attributes
    • paralleltrue
    • defaultProc.new { r=nil if self.default_localhost r = MintPress::Infrastructure::Localhost.new(final_user: self.default_localhost_user) end r }
    
    
    41
    42
    43
    44
    45
    # File 'src/mintpress-resources/lib/mintpress-resources/resource.rb', line 41
    
    property :host, type: MintPress::Infrastructure::Host, parallel: true, default: Proc.new { r=nil
    if self.default_localhost
      r = MintPress::Infrastructure::Localhost.new(final_user: self.default_localhost_user)
     end
    r }
  • #as_admin ⇒ [TrueClass, FalseClass] (Default Value: false)
    Should the operation be performed as an administratrive user (root on linux/unix, Adminstrator on windows) or should it be performed as unprivledged? The default behaviour is to perform as unprivledged unless otherwise asked.

    Property Attributes
    • defaultfalse
    
    
    66
    # File 'src/mintpress-resources/lib/mintpress-resources/resource.rb', line 66
    
    property :as_admin, type: [TrueClass, FalseClass], default: false

Properties (Read Only)

Constant Summary

Constants included from MintLogger

MintLogger::DEBUG, MintLogger::ERROR, MintLogger::FATAL, MintLogger::INFO, MintLogger::UNKNOWN, MintLogger::VERBOSE, MintLogger::WARN

Instance Attribute Summary

Attributes included from Mixins::Properties

#autopush_set_cache, #dynamic_create, #harvest_on_access, #harvest_undefined_only, #harvested, #tree_root

Instance Method Summary collapse

Methods included from Mixins::Properties

#[], #[]=, #add_validate_report_result, #armour_set_property, #array_contains?, #array_is_a?, #check_autopush, #check_stack_overflow, #clone_property_object, #cloner_handle_single_property, #coerce_single, #contains_as_string?, #display_validate_report_result, #double_initialize?, #dump_to_hash, #find_parent, #find_parent_by_identity, #generate_accessor_functions, #get_canonical_renamed, #get_from_opts, #get_my_name, #get_property, #get_property_item, #has?, included, #initialize_validate_report, #inspect, #is_cloned_object?, #is_mintpress_object?, #is_probably_canonical?, #is_set?, #local_debug, #local_info, #local_verbose, #mintpress_property_definitions, #place_object_by_identity, #process_properties, #prop_set?, #property, #property_definitions, #property_details, #property_is_simple_object?, #push_root!, #require_property, #require_update, #retrieve_docstring, #sanitize, #set_map_dirty, #set_property, #set_property_item, #show_short_array, #strip_defaults!, #synchronize, #uncloned_property_definitions, #update_map, #validate, #validate_generic, #validate_properties, #validate_property, #validate_required, #version_allowed?, #weakref

Methods included from MintLogger::Utils::Common

#boolean_val, #has_value?, #no_value?, #nvl, #path_as_symbol, #ruby_level_to_send

Constructor Details

#initialize(opts = {}) ⇒ FileUtils

Returns a new instance of FileUtils.

Instance Method Details

#append_if_no_linesObject

Append only if pattern not found

#deleteObject

Deletes a matching pattern in a file Example: FileUtils.new(file: 'address.properties', pattern: 'color: (red|blue)', line: 'color: green', host: host) Original: color: redish color: blueish color: black Outcome: ish ish color: black

#delete_linesObject

Deletes lines matching pattern, this method ignores all_occurrences and will always delete all matching lines Example: FileUtils.new(file: 'address.properties', pattern: 'color: (red|blue)', line: 'color: green', host: host) Original: color: redish color: blueish color: black Outcome: color: black

#replaceObject

Replace pattern in a file. Example: FileUtils.new(file: 'address.properties', pattern: 'color: (red|blue)', line: 'color: green', host: host) Original: color: redish color: blueish Outcome: color: greenish color: greenish

#replace_linesObject

Replace an entire line matching pattern in a file Example: FileUtils.new(file: 'address.properties', pattern: 'color: (red|blue)', line: 'color: green', host: host) Original: color: redish color: blueish Outcome: color: green color: green

#replace_or_add_linesObject

Replace lines in a file if they exist, or add if they do not