Class: Mushy::Ls

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

Instance Attribute Summary

Attributes inherited from Flux

#config, #flow, #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, #ignore_these_results, 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
19
20
21
22
23
24
# 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:       '',
                             }
    c[:config][:path] = {
                            description: 'Path, used to search for specific files.',
                            type:        'text',
                            shrink:      true,
                            value:       '',
                          }
  end
end

Instance Method Details

#build_the_arguments_from(config) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/mushy/fluxs/ls.rb', line 40

def build_the_arguments_from config
  arguments = ['-A', '-l', '--full-time', '-i']
  arguments << '-R' if config[:recursive].to_s == 'true'
  arguments << '-d' if config[:directory_only].to_s == 'true'
  arguments << "'#{config[:path]}'" if config[:path].to_s != ''
  arguments
end

#build_the_command_from(arguments) ⇒ Object



36
37
38
# File 'lib/mushy/fluxs/ls.rb', line 36

def build_the_command_from arguments
  "ls #{arguments.join(' ')}"
end

#process(event, config) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/mushy/fluxs/ls.rb', line 26

def process event, config
  arguments = build_the_arguments_from config

  config[:command] = build_the_command_from arguments
  result = super event, config

  things = turn_the_ls_output_to_events result, config, event
  things
end

#pull_file(segments, directory) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/mushy/fluxs/ls.rb', line 96

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.join ' '

  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] = Mushy::DateParts.parse result[:date]

  result[:directory] = directory

  if result[:type] == 'd' && result[:directory] == result[:name]
    result[:path] = result[:directory]
    name_segments = result[:name].split "\/"
    result[:name] = name_segments.pop
    result[:directory] = name_segments.join "\/"
  else
    result[:path] = File.join result[:directory], result[:name]
  end

  result
end

#turn_the_ls_output_to_events(result, config, event) ⇒ Object



48
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
89
90
91
92
93
94
# File 'lib/mushy/fluxs/ls.rb', line 48

def turn_the_ls_output_to_events result, config, event

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

  needs_special_work_for_path = config[:directory_only].to_s != 'true' &&
                                config[:path].to_s != '' &&
                                lines[0] &&
                                lines[0].start_with?('total ')

  origin = config[:directory] || Dir.pwd
  directory = needs_special_work_for_path ? '||DIRECTORY||' : origin

  things = 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("\/")

               if dir_segments[0] == '.'
                 dir_segments[0] = origin
               else
                 dir_segments.unshift origin
               end

               dir_segments[-1] = dir_segments[-1].sub ':', ''
               directory = dir_segments.join("\/")
               nil
             else
               nil
             end
  end.select { |x| x }

  if needs_special_work_for_path
    config[:directory_only] = true
    special_name = process(event, config)[0][:name]
    things.each do |x|
      [:directory, :path].each do |key|
        if x[key].include?('||DIRECTORY||')
          x[key].sub!('||DIRECTORY||', File.join(Dir.pwd, special_name))
        end
      end
    end
  end

  things
end