Class: DataFile

Inherits:
Object
  • Object
show all
Defined in:
lib/Framework/DataFile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, separator = nil) ⇒ DataFile



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/Framework/DataFile.rb', line 12

def initialize(filename, separator = nil)

  filename.gsub!('/', "\\") if filename =~ /\w:\\/

  @filename = filename
  @separator = separator.nil? ? '| |' : separator

  @commands = []

  @verbose = 0
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



9
10
11
# File 'lib/Framework/DataFile.rb', line 9

def commands
  @commands
end

#separatorObject

TODO: create table with aliases of commands so if you call command as padmin or using swrole, it shouldn’t be difference as result is the same



6
7
8
# File 'lib/Framework/DataFile.rb', line 6

def separator
  @separator
end

#verboseObject

Returns the value of attribute verbose.



7
8
9
# File 'lib/Framework/DataFile.rb', line 7

def verbose
  @verbose
end

Instance Method Details

#close(command, value) ⇒ Object



40
41
42
# File 'lib/Framework/DataFile.rb', line 40

def close(command, value)
  write('echo "---end---"', '---end---')
end

#create(version, lparname, date) ⇒ Object



24
25
26
27
28
29
# File 'lib/Framework/DataFile.rb', line 24

def create(version, lparname, date)

  write('ScriptVersion', version)
  write('uname -n', lparname)
  write('date', date)
end

#find(command, separator = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/Framework/DataFile.rb', line 56

def find(command, separator = nil)

  unless File.readable?(@filename)
    puts "file #{@filename} is not readable!"
    return nil
   end

  list_of_commands if @commands.empty?

  similar_command = find_simiar_command(command)
  command = similar_command unless similar_command.nil?

  data_string = ''
  separator = @separator if separator.nil? or separator.empty?

  File.open(@filename).each do |line|
    tmp = line.split(separator)
    data_string += tmp[1] if tmp[0] =~ /^\s*#{command}\s*(?:2>&1\s*|)$/
  end
  data_string
end

#find_simiar_command(command) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/Framework/DataFile.rb', line 78

def find_simiar_command(command)

  # TODO: move list of commands to separete file, this part should be easier to update
  if command.include?('lsmap') and command.include?('-all')  and command.include?('npiv')
    aliases = [
        'lsmap -npiv -all',
        'lsmap -npiv -all -fmt :',
        'lsmap -all -npiv',
        'lsmap -all -npiv -fmt :',
        '/usr/ios/cli/ioscli lsmap -npiv -all',
        '/usr/ios/cli/ioscli lsmap -npiv -all -fmt :',
        '/usr/ios/cli/ioscli lsmap -all -npiv',
        '/usr/ios/cli/ioscli lsmap -all -npiv -fmt :',
    ]

    aliases.each do |command|
      return command if @commands.include?(command)
    end
  elsif command.include?('lsmap') and command.include?('-all')

    aliases = [
        'lsmap -all',
        'lsmap -all -fmt :',
        '/usr/ios/cli/ioscli lsmap -all',
        '/usr/ios/cli/ioscli lsmap -all -fmt :',
    ]

    aliases.each do |command|
      return command if @commands.include?(command)
    end

  end
  nil
end

#how_old?Boolean



125
126
127
# File 'lib/Framework/DataFile.rb', line 125

def how_old?
  # TODO: function will return seconds from date written in file (it will hep to check if check is not too  old)
end

#list_of_commandsObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/Framework/DataFile.rb', line 45

def list_of_commands
  return false unless File.readable?(@filename)
  separator = @separator
  @commands = []

  File.open(@filename).each do |line|
    tmp = line.split(separator)
    @commands.push(tmp[0]) unless @commands.include?(tmp[0])
  end
end

#make_emptyObject



129
130
131
132
# File 'lib/Framework/DataFile.rb', line 129

def make_empty
  File.open(@filename, 'w') { |file| file.truncate(0) }
  @commands = []
end

#validateObject



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/Framework/DataFile.rb', line 113

def validate
  # each file should have server name, date, version and "---end---" (to validate that all data are taken)
  #
  # TODO: function will return array with errors, if empty, no issues
  list_of_commands
  return false unless @commands.include?('date')
  return false unless @commands.include?('uname -n')
  return false unless @commands.include?('ScriptVersion')
  retunr false unless @commands.include?('echo "---end---"')
  true
end

#write(command, value) ⇒ Object Also known as: Write



31
32
33
34
35
36
37
# File 'lib/Framework/DataFile.rb', line 31

def write(command, value)
  File.open(@filename, 'a') do |file|
    value.split("\n").each do |line|
      file.write(command + '| |' + line + "\n")
    end
  end
end