lib/vzlimit/cli.rb in vzlimit-0.0.1 vs lib/vzlimit/cli.rb in vzlimit-0.0.2
- old
+ new
@@ -12,23 +12,33 @@
@arguments = arguments
@h = HighLine.new
begin
# Check action argument
- if !@arguments[0] ||! ACTIONS.include?(@arguments[0].to_sym)
+ if !@arguments[0] || (!ACTIONS.include?(@arguments[0].to_sym) &&! @arguments[0] =~ /^-.*/)
usage
end
# list
if @arguments[0].to_sym == :list
list
end
# limit
- if @arguments[0].to_sym == :limit
- limit
+ if @arguments[0].to_sym == :set
+ set
end
+
+ # help
+ if @arguments[0] == '-h' || @arguments[0] == '--help'
+ help
+ end
+
+ # version
+ if @arguments[0] == '-v' || @arguments[0] == '--version'
+ version
+ end
rescue
@h.say $!
end
end
@@ -94,34 +104,34 @@
#@h.say sprintf("%10sworld", "hello");
#@h.say @h.list vps_list, :columns_down
end
- def limit
+ def set
@arguments.shift
- usage = 'Usage: vzlimit limit vpsid [options]'
+ usage = 'Usage: vzlimit set vpsid [options]'
raise usage unless @arguments[0]
vpsid = @arguments.shift
memory = nil
disk = nil
while (arg = @arguments.shift) do
- if ['-m', '--memory'].include? arg
+ if ['-r', '--ram'].include? arg
memory = @arguments.shift
if !(memory =~ /^[-+]?[0-9]*\.?[0-9]+:[-+]?[0-9]*\.?[0-9]+$/)
- raise "Usage: vzlimit limit vpsid #{arg} soft:hard"
+ raise "Usage: vzlimit set vpsid #{arg} soft:hard"
end
elsif ['-d', '--diskspace'].include? arg
disk = @arguments.shift
if !(disk =~ /^\d+:\d+$/)
- raise "Usage: vzlimit limit vpsid #{arg} soft:hard"
+ raise "Usage: vzlimit set vpsid #{arg} soft:hard"
end
else
- raise "Argument #{arg} not valid for action limit."
+ raise "Argument #{arg} not valid for action 'set'."
end
end
if !memory &&! disk
raise "Nothing to set."
@@ -143,11 +153,11 @@
raise "Memory limits not valid."
end
# Convert MB limits to mempages
soft = (soft_mb.to_f * 1024 / 4).to_i
- hard = (soft_mb.to_f * 1024 / 4).to_i
+ hard = (hard_mb.to_f * 1024 / 4).to_i
# Execute command
execute "vzctl set #{vpsid} --privvmpages #{soft}:#{hard} --save"
# Success message
@@ -166,11 +176,11 @@
raise "Disk limits not valid."
end
# Convert MB limits to 1K-block
soft = (soft_mb.to_f * 1024).to_i
- hard = (soft_mb.to_f * 1024).to_i
+ hard = (hard_mb.to_f * 1024).to_i
# Execute command
execute "vzctl set #{vpsid} --diskspace #{soft}:#{hard} --save"
# Success message
@@ -194,8 +204,22 @@
raise @h.color "Error: Command execution failed, exit status #{$?}.", :red unless retval
end
def usage
@h.say "Usage: vzlimit action [params...]"
+ exit
end
+
+ def version
+ @h.say "vzlimit version #{VERSION}"
+ end
+
+ def help
+ @h.say "vzlimit list | set <veid>"
+ @h.say "vzlimit set <veid>"
+ @h.say "\t[--ram, -r <soft-limit-in-mb>:<hard-limit-in-mb>]"
+ @h.say "\t[--diskspace, -d <soft-limit-in-mb>:<hard-limit-in-mb>]"
+ @h.say "vzlimit --help, -h"
+ @h.say "vzlimit --version, -v"
+ end
end
-end
\ No newline at end of file
+end