Class: CrontabWrap
- Inherits:
-
Object
- Object
- CrontabWrap
- Defined in:
- lib/crontabwrap.rb
Overview
Crontab wrapper class
Constant Summary collapse
- BACKUP_FILE =
'.crontab'
Instance Attribute Summary collapse
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#backup(argv) ⇒ Object
backup crontab content for each user.
-
#backup_file(user) ⇒ Object
backup file name for crontab content.
- #echo(argv, opt = '') ⇒ Object
- #exec(argv, opt = '') ⇒ Object
-
#initialize(param = {}) ⇒ CrontabWrap
constructor
construct object.
-
#parse_args(argv) ⇒ Object
parse command line args.
-
#replace_switch_to_display(opts) ⇒ Object
replace ‘-e’ option to ‘-l’.
-
#run ⇒ Object
take all options to crontab.
Constructor Details
#initialize(param = {}) ⇒ CrontabWrap
construct object
default BACKUP_FILE is ~/.crontab
16 17 18 19 20 21 22 23 24 |
# File 'lib/crontabwrap.rb', line 16 def initialize( param = {} ) @opt = { :debug => false, :argv => ARGV }.merge( param ) @user = ENV['USER'] parse_args( @opt[:argv] ) end |
Instance Attribute Details
#user ⇒ Object (readonly)
Returns the value of attribute user.
25 26 27 |
# File 'lib/crontabwrap.rb', line 25 def user @user end |
Instance Method Details
#backup(argv) ⇒ Object
backup crontab content for each user
68 69 70 71 72 |
# File 'lib/crontabwrap.rb', line 68 def backup( argv ) argv = replace_switch_to_display( argv ) exec( argv, " > #{backup_file( @user )}" ) end |
#backup_file(user) ⇒ Object
backup file name for crontab content
89 90 91 |
# File 'lib/crontabwrap.rb', line 89 def backup_file( user ) return "~#{user}/#{BACKUP_FILE}" end |
#echo(argv, opt = '') ⇒ Object
56 57 58 |
# File 'lib/crontabwrap.rb', line 56 def echo( argv, opt = '' ) STDERR.puts 'crontab ' + argv.join( " " ) + opt end |
#exec(argv, opt = '') ⇒ Object
60 61 62 63 |
# File 'lib/crontabwrap.rb', line 60 def exec( argv, opt = '' ) echo( argv, " > #{backup_file( @user )}" ) if ( @opt[:debug] ) system( 'crontab ' + argv.join( " " ) + opt ) end |
#parse_args(argv) ⇒ Object
parse command line args
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/crontabwrap.rb', line 45 def parse_args( argv ) opt = OptionParser.new() opt.on( '-u USER' ) { |user| @user = user } begin opt.parse( argv ) rescue end end |
#replace_switch_to_display(opts) ⇒ Object
replace ‘-e’ option to ‘-l’
- Param
-
Array opts
- Return
-
Array
80 81 82 83 84 |
# File 'lib/crontabwrap.rb', line 80 def replace_switch_to_display( opts ) opts.map { |opt| ( opt == '-e' ) ? '-l' : opt } end |
#run ⇒ Object
take all options to crontab
do backup if ‘-e’ option given
32 33 34 35 36 37 38 39 40 |
# File 'lib/crontabwrap.rb', line 32 def run argv = @opt[:argv] if ( exec( argv ) ) if ( argv.include?( '-e' ) ) backup( argv ) end end end |