Class: Balmora::Command::Files

Inherits:
Balmora::Command show all
Defined in:
lib/balmora/command/files.rb

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Methods inherited from Balmora::Command

#_execute, #execute, #initialize, #option

Constructor Details

This class inherits a constructor from Balmora::Command

Instance Method Details

#_filesObject



43
44
45
46
47
# File 'lib/balmora/command/files.rb', line 43

def _files()
  files = _find_files()
  files = _filter_files(files)
  return files
end

#_filter_files(files) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/balmora/command/files.rb', line 90

def _filter_files(files)
  if !@exclude.nil?()
    exclude = Regexp.new('(' + @exclude.collect() { |part| "(#{part})" }.
      join('|') + ')')

    files = files.select() { |file|
      result =
        if file.instance_of?(::Hash)
          !file[:file].match(exclude)
        else
          !file.match(exclude)
        end

      next result
    }
  end

  return files
end

#_find_filesObject



49
50
51
52
53
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
80
81
82
83
84
85
86
87
88
# File 'lib/balmora/command/files.rb', line 49

def _find_files()
  storage = @shell.expand(@storage)

  files = []
  @files.each() { |file|
    path = file
    if path.instance_of?(::Hash)
      path = path[:file]
    end

    if @action == 'pull'
      path = ::File.join(storage, path.gsub(/^(\~\/|\~$|\/)/, ''))
    else
      path = @shell.expand(path)
    end

    command = ['test', '-d', path, @shell.expression('&&'), *@shell.sudo(),
      'find', path, '-type', 'f']

    dir_status, dir_files = @shell.run(command, verbose: false)
    if dir_status != 0
      files.push(file)
      next
    end

    dir_files.
      strip().
      split("\n").
      each() { |found|
        found = found[(path.length + 1)..-1]
        if file.instance_of?(::Hash)
          files.push(file.merge(file: ::File.join(file[:file], found)))
        else
          files.push(::File.join(file, found))
        end
      }
  }

  return files
end

#_get_command(file) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/balmora/command/files.rb', line 110

def _get_command(file)
  command = (@options || {}).merge(command: 'file', file: file)

  if file.instance_of?(::Hash)
    command.merge!(file)
  end

  command.merge!(action: @action, storage: @storage)

  return command
end

#initObject



5
6
7
8
9
10
11
# File 'lib/balmora/command/files.rb', line 5

def init()
  super()

  @files = @variables.inject(@files)
  @storage = @variables.inject(@storage)
  @exclude = @variables.inject(@exclude)
end

#optionsObject



13
14
15
# File 'lib/balmora/command/files.rb', line 13

def options()
  return super().concat([:action, :options, :files, :storage, :exclude])
end

#runObject



36
37
38
39
40
41
# File 'lib/balmora/command/files.rb', line 36

def run()
  _files().each() { |file|
    command = _get_command(file)
    @balmora.run_command(@state, command)
  }
end

#verifyObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/balmora/command/files.rb', line 17

def verify()
  if @files.nil?() && @storage.nil?()
    raise Error.new('"files" or "storage" should be defined')
  end

  if @files.nil?() || @files.empty?()
    raise Error.new('"files" should be defined')
  end

  if !@options.nil?() && @options.has_key?(:storage) && !@storage.nil?()
    raise Error.new('"file.storage" and "storage" could not be defined ' +
      'together')
  end

  if @action.nil?()
    raise Error.new('"action" should be defined')
  end
end