Module: Cmdapp

Defined in:
lib/common/cmdapp.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) _backup(filename = @app_file_path)



126
127
128
129
# File 'lib/common/cmdapp.rb', line 126

def _backup filename=@app_file_path
  require 'fileutils'
  FileUtils.cp filename, "#{filename}.org"
end

- (Object) _get_serial_number

reads serial_number file, returns serialno for this app and increments the serial number and writes back.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/common/cmdapp.rb', line 92

def _get_serial_number
  require 'fileutils'
  appname = @appname
  filename = @app_serial_path || "serial_numbers"
  h = {}
  # check if serial file existing in curr dir. Else create
  if File.exists?(filename)
    File.open(filename).each { |line|
      #sn = $1 if line.match regex
      x = line.split ":"
      h[x[0]] = x[1].chomp
    }
  end
  sn = h[appname] || 1
  # update the sn in file
  nsn = sn.to_i + 1
  # this will create if not exists in addition to storing if it does
  h[appname] = nsn
  # write back to file
  File.open(filename, "w") do |f|
    h.each_pair {|k,v| f.print "#{k}:#{v}\n"}
  end
  return sn
end

- (Object) _set_serial_number(number)

After doing a redo of the numbering, we need to reset the numbers for that app



118
119
120
121
122
123
124
# File 'lib/common/cmdapp.rb', line 118

def _set_serial_number number
  appname = @appname
  pattern = Regexp.new "^#{appname}:.*$"
  filename = @app_serial_path || "serial_numbers"
  _backup filename
  change_row filename, pattern, "#{appname}:#{number}"
end

- (Object) add_action(name, descr)



84
85
86
87
# File 'lib/common/cmdapp.rb', line 84

def add_action name, descr
  @actions ||= {}
  @actions[name.to_s] = desc
end

- (Object) alias_command(name, *args)



80
81
82
83
# File 'lib/common/cmdapp.rb', line 80

def alias_command name, *args
  @aliases ||= {}
  @aliases[name.to_s] = args
end

- (Boolean) check_aliases(action, args)

external dependencies:

 @app_default_action - action to run if none specified
 @app_file_path - data file we are backing up, or reading into array
 @app_serial_path - serial_number file

check whether this action is mapped to some alias and changes variables@action and @argv if true.

Parameters:

  • (String) action

    asked by user

  • (Array) rest

    of args on command line

Returns:

  • (Boolean)

    whether it is mapped or not.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/common/cmdapp.rb', line 30

def check_aliases action, args
  ret = @aliases[action]
  if ret
    a = ret.shift
    b = [*ret, *args]
    @action = a
    @argv = b
    #puts " #{@action} ; argv: #{@argv} "
    return true
  end
  return false
end

- (Object) die(text)



130
131
132
133
# File 'lib/common/cmdapp.rb', line 130

def die text
  $stderr.puts text
  exit ERRCODE
end

- (Object) help(args)

not required if using Subcommand



69
70
71
72
73
74
75
76
77
78
# File 'lib/common/cmdapp.rb', line 69

def help args
  if @actions
    puts "Actions are "
    @actions.each_pair { |name, val| puts "#{name}\t#{val}" }
  end
  puts " "
  puts "Aliases are "
  @aliases.each_pair { |name, val| puts "#{name}:\t#{val.join(' ')}" }
  0
end

- (Object) load_array

load data into array as item and task

See Also:

  • to write


158
159
160
161
162
163
164
165
166
167
168
# File 'lib/common/cmdapp.rb', line 158

def load_array
  #return if $valid_array
  $valid_array = false
  @data = []
  File.open(@app_file_path).each do |line|
    # FIXME: use @app_delim
    row = line.chomp.split "\t"
    @data << row
  end
  $valid_array = true
end

- (Object) message(text)

prints messages to stderr All messages should go to stderr. Keep stdout only for output which can be used by other programs



137
138
139
# File 'lib/common/cmdapp.rb', line 137

def message text
  $stderr.puts text
end


151
152
153
# File 'lib/common/cmdapp.rb', line 151

def print_green text
  message "#{GREEN}#{text}#{CLEAR}"
end


148
149
150
# File 'lib/common/cmdapp.rb', line 148

def print_red text
  message "#{RED}#{text}#{CLEAR}"
end

- (0, ERRCODE) run

runs method after checking if valid or alias. If not found prints help.

Returns:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/common/cmdapp.rb', line 46

def run
  @action = @argv[0] || @app_default_action
  @action = @action.downcase


  ret = 0
  @argv.shift
  if respond_to? @action
    ret = send(@action, @argv)
  else
    # check aliases
    if check_aliases @action, @argv
      ret = send(@action, @argv)
    else
      help @argv
      ret = ERRCODE
    end
  end
  ret ||= 0
  ret = 0 if ret != ERRCODE
  return ret
end

- (Object) save_array

saves the task array to disk Please use load_array to load, and not populate



172
173
174
175
176
177
178
179
# File 'lib/common/cmdapp.rb', line 172

def save_array
  raise "Cannot save array! Please use load_array to load" if $valid_array == false

  File.open(@app_file_path, "w") do |file| 
    # FIXME: use join with @app_delim
    @data.each { |row| file.puts "#{row[0]}\t#{row[1]}" }
  end
end

- (Object) verbose(text)

print to stderr only if verbose set



141
142
143
# File 'lib/common/cmdapp.rb', line 141

def verbose text
  message(text) if @options[:verbose]
end

- (Object) warning(text)

print to stderr only if verbose set



145
146
147
# File 'lib/common/cmdapp.rb', line 145

def warning text
  print_red("WARNING: #{text}") 
end