Module: Machines::Commands::FileOperations

Included in:
Installation, Machines::Core
Defined in:
lib/machines/commands/file_operations.rb

Instance Method Summary collapse

Instance Method Details

#append(text, options) ⇒ Object

Add a line of text to the end of a file unless it already exists

Parameters:

  • text (String)

    Text to add

  • options (Hash)

Options Hash (options):

  • :to (String)

    File to append to



8
9
10
11
# File 'lib/machines/commands/file_operations.rb', line 8

def append text, options
  text = text.gsub(/([\\$"`])/, '\\\\\1')
  Command.new("grep \"#{text}\" #{options[:to]} || echo \"#{text}\" >> #{options[:to]}", check_string(text, options[:to]))
end

#chmod(mode, path) ⇒ Object

Change permissions of a path

Parameters:

  • mode (String, Integer)

    chmod permissions to set

  • path (String)

    Path to set



16
17
18
# File 'lib/machines/commands/file_operations.rb', line 16

def chmod mode, path
  Command.new("chmod #{mode} #{path}", check_perms(mode, path))
end

#chown(user, path, options = {}) ⇒ Object

Change ownership of a path

Parameters:

  • user (String)

    sets user and group unless user:group is specified

  • path (String)

    Path to set

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :recursive (String)

    Chowns recursively if true



25
26
27
28
29
# File 'lib/machines/commands/file_operations.rb', line 25

def chown user, path, options = {}
  recursive = '-R ' if options[:recursive]
  user = "#{user}:#{user}" unless user.index(':')
  Command.new("chown #{recursive}#{user} #{path}", check_owner(user, path))
end

#copy(from, to) ⇒ Object

Copy a remote file or folder (will overwrite)

Parameters:

  • from (String)

    Existing path

  • to (String)

    Path to copy to



34
35
36
# File 'lib/machines/commands/file_operations.rb', line 34

def copy from, to
  Command.new("cp -rf #{from} #{to}", check_file(to))
end

#create_from(erb_path, options) ⇒ Object

Write a file from an ERB template

Parameters:

  • erb_path (String)

    Path to the ERB file to process

  • options (Hash)

Options Hash (options):

  • :settings (AppBuilder)

    Contains the settings as OpenStruct method calls for calling from the template

  • :to (String)

    File to write to



43
44
45
46
47
48
# File 'lib/machines/commands/file_operations.rb', line 43

def create_from erb_path, options
  erb = ERB.new(File.read(erb_path), nil, '<>')
  binding = options[:settings] ? options[:settings].get_binding : nil
  options[:name] = erb_path
  write erb.result(binding), options
end

Add a symlink

Parameters:

  • target (String)

    Existing path to link

  • link_name (String)

    path name for the link



53
54
55
# File 'lib/machines/commands/file_operations.rb', line 53

def link target, link_name
  Command.new("ln -sf #{target} #{link_name}", check_link(link_name))
end

#mkdir(*dirs) ⇒ Object

Create a path or paths on the remote host (Uses -p to be safe and create full path)

Parameters:

  • dirs (String, Array)

    A single path, multiple paths or array of paths to create



59
60
61
62
63
# File 'lib/machines/commands/file_operations.rb', line 59

def mkdir *dirs
  dirs.flatten.map do |dir|
    Command.new("mkdir -p #{dir}", check_dir(dir))
  end
end

#remove(file) ⇒ Object

Remove a remote file or folder

Parameters:

  • file (String)

    or folder to remove (uses rm with -rf which ignores non-existent files and is recursive)



74
75
76
# File 'lib/machines/commands/file_operations.rb', line 74

def remove file
  Command.new("rm -rf #{file}", check_file(file, false))
end

#remove_version_info(name) ⇒ Object

Take off the version numbers from a path name

Parameters:

  • name (String)

    Name of the path to rename



80
81
82
# File 'lib/machines/commands/file_operations.rb', line 80

def remove_version_info name
  Command.new("find . -maxdepth 1 -name \"#{name}*\" -a -type d | xargs -I xxx mv xxx #{name}", check_file(name))
end

#rename(oldname, newname) ⇒ Object

Rename a remote file or folder

Parameters:

  • oldname (String)

    Existing filename

  • newname (String)

    Rename to this



68
69
70
# File 'lib/machines/commands/file_operations.rb', line 68

def rename oldname, newname
  Command.new("mv -f #{oldname} #{newname}", check_file(newname))
end

#replace(regex, options) ⇒ Object

Replace some text in a file

Parameters:

  • regex (String)

    The expression to search for

  • options (Hash)

Options Hash (options):

  • :with (String)

    Text to use as the replacement

  • :in (String)

    Filename to replace text in



89
90
91
92
93
# File 'lib/machines/commands/file_operations.rb', line 89

def replace regex, options
  required_options options, [:with, :in]
  with = options[:with].gsub(/([\n\/\\$"`])/, '\\\\\1')
  Command.new("sed -i \"s/#{regex}/#{with}/\" #{options[:in]}", check_string(with, options[:in]))
end

#write(text, options) ⇒ Object

(Over)write a file with the specified content

Parameters:

  • text (String)

    Text to add

  • options (Hash)

Options Hash (options):

  • :to (String)

    File to write to

  • :name (String)

    Give the buffer a displayable name (e.g. when generated from a template)



100
101
102
# File 'lib/machines/commands/file_operations.rb', line 100

def write text, options
  Upload.new(NamedBuffer.new(options[:name], text), options[:to], check_string(text, options[:to]))
end