Class: Helpers::NewFile

Inherits:
Object
  • Object
show all
Defined in:
lib/spior/helpers.rb

Overview

Class Newfile Create a file and move at the dest

Example

string = “nameserver 127.0.0.1” name = “resolv.conf” dest = “/etc” new_file = Helpers::Newfile.new(string, name, dest) new_file.add

Direct Known Subclasses

NewSystemd

Instance Method Summary collapse

Constructor Details

#initialize(string, name, dest = '/tmp') ⇒ NewFile

Method #new

Parameters

  • string = string for the whole file

  • name = name of the file (e.g: resolv.conf)

  • dest = path (e.g: /etc)



69
70
71
72
73
# File 'lib/spior/helpers.rb', line 69

def initialize(string, name, dest = '/tmp')
  @string = string
  @name = name
  @dest = "#{dest}/#{@name}"
end

Instance Method Details

#addObject

Method #add Add the file at @dest



77
78
79
80
81
82
83
# File 'lib/spior/helpers.rb', line 77

def add
  @mv = Helpers::Exec.new('mv')
  tmp = Tempfile.new(@name)
  File.write tmp.path, "#{@string}\n"
  puts "move #{tmp.path} to #{@dest}"
  @mv.run("#{tmp.path} #{@dest}")
end

#perm(user, perm) ⇒ Object



85
86
87
88
89
90
# File 'lib/spior/helpers.rb', line 85

def perm(user, perm)
  chown = Helpers::Exec.new('chown')
  chmod = Helpers::Exec.new('chmod')
  chown.run("#{user}:#{user} #{@dest}")
  chmod.run("#{perm} #{@dest}")
end