Class: TzPickup::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/tz_pickup/cli.rb

Constant Summary collapse

DOWNLOAD_URL =
'ftp://ftp.iana.org/tz/tzdata-latest.tar.gz'

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CLI

Returns a new instance of CLI.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tz_pickup/cli.rb', line 9

def initialize(*args)
  super

  @file = DOWNLOAD_URL.split('/')[-1]
  @dst_dir = File.join(TzPickup.root, TzPickup::TzTree::DB_PATH)
  @dst_file = File.join(@dst_dir, @file)

  @commands = [
    {
      cmd: "mkdir -p #{ @dst_dir }",
      success: "1. Using #{ @dst_dir }",
      error: "1. Failed to use #{ @dst_dir }"          
    },
    {
      cmd: "wget -q #{ DOWNLOAD_URL } -O #{ @dst_file }",
      success: "2. #{ @file } downloaded successfully and saved to #{ @dst_file }",
      error: "2. #{ @file } failed to be downloaded from #{ DOWNLOAD_URL } and saved to #{ @dst_file }"          
    },
    {
      cmd: "tar zxf #{ @dst_file } -C #{ @dst_dir } #{ TzPickup::TzTree::ZONE_FILE }",
      success: "3. #{ TzPickup::TzTree::ZONE_FILE } successfully extracted to #{ @dst_dir }",
      error: "3. #{ TzPickup::TzTree::ZONE_FILE } failed to be extracted from #{ @dst_file } to #{ @dst_dir }"          
    }
  ]

  @ensure_block = {
    cmd: "rm -f #{ @dst_file }",
    success: "4. #{ @dst_file } removed successfully",
    error: "4. #{ @dst_file } failed to be removed" 
  }
end

Instance Method Details

#chargeObject



53
54
55
56
57
58
59
60
61
# File 'lib/tz_pickup/cli.rb', line 53

def charge
  begin
    @commands.each { |cmd| execute cmd }
  rescue SystemCallError => error
    puts "Errno: #{ error.errno }"
  ensure
    execute @ensure_block
  end
end