Class: Mushy::Ls

Inherits:
Bash show all
Defined in:
lib/mushy/fluxs/ls.rb

Instance Attribute Summary

Attributes inherited from Flux

#config, #id, #masher, #parent_fluxs, #subscribed_to, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Flux

#convert_this_to_an_array, #convert_to_symbolized_hash, #execute, #execute_single_event, #group_these_results, #guard, inherited, #initialize, #join_these_results, #limit_these_results, #merge_these_results, #model_these_results, #outgoing_split_these_results, #shape_these, #sort_these_results, #standardize_these

Constructor Details

This class inherits a constructor from Mushy::Flux

Class Method Details

.detailsObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/mushy/fluxs/ls.rb', line 5

def self.details
  {
    name: 'Ls',
    description: 'Run the "ls" command.',
    config: Mushy::Bash.details[:config].tap { |c| c.delete :command },
  }.tap do |c|
    c[:config][:recursive] = {
                               description: 'Pull files recursively.',
                               type:        'boolean',
                               shrink:      true,
                               value:       '',
                             }
  end
end

Instance Method Details

#process(event, config) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mushy/fluxs/ls.rb', line 20

def process event, config

  arguments = ['-A', '-l', '--full-time', '-i']
  arguments << '-R' if config[:recursive].to_s == 'true'
  config[:command] = "ls #{arguments.join(' ')}"

  result = super event, config

  return result unless result[:success]

  lines = result[:text].split("\n")
  lines.shift

  origin = config[:directory] || Dir.pwd
  directory = origin
  lines.map do |x|
    segments = x.split ' '
    result = if segments.count > 5
               pull_file segments, directory
             elsif segments.count == 1
               dir_segments = segments[0].split("\/")
               dir_segments[0] = origin if dir_segments[0] == '.'
               dir_segments[-1] = dir_segments[-1].sub ':', ''
               directory = dir_segments.join("\/")
               nil
             else
               nil
             end
  end.select { |x| x }

end

#pull_file(segments, directory) ⇒ Object



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
# File 'lib/mushy/fluxs/ls.rb', line 52

def pull_file segments, directory
  result = {}

  [:inode, :help, :hard_links, :owner, :group, :size].each do |key|
    result[key] = segments.shift; x = segments.join ' '
  end

  result.tap do |r|
    r[:date] = []
    3.times { r[:date] << segments.shift }
    r[:date] = r[:date].join ' '
    r[:date] = Time.parse r[:date]
  end

  result[:name] = segments.shift

  result.tap do |r|
    help_segments = r[:help].split ''
    r[:type] = help_segments[0]
    r[:owner_permission] = [1, 2, 3].map { |i| help_segments[i] }.reduce('') { |t, i| t + i }
    r[:group_permission] = [4, 5, 6].map { |i| help_segments[i] }.reduce('') { |t, i| t + i }
    r[:other_permission] = [7, 8, 9].map { |i| help_segments[i] }.reduce('') { |t, i| t + i }
    r.delete :help
  end

  [:hard_links, :size].each { |x| result[x] = result[x].to_i }

  result[:date_parts] = Mushy::DateParts.parse result[:date]

  result[:directory] = directory
  result[:path] = File.join result[:directory], result[:name]

  result
end