Class: Box::Release::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/box/release/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments = []) ⇒ CLI

Returns a new instance of CLI.



9
10
11
12
13
# File 'lib/box/release/cli.rb', line 9

def initialize(arguments = [])
  @install_command = "/usr/local/sbin/box-upgrade"

  parse_options(arguments)
end

Instance Attribute Details

#after_download_commandObject

Returns the value of attribute after_download_command.



7
8
9
# File 'lib/box/release/cli.rb', line 7

def after_download_command
  @after_download_command
end

#before_download_commandObject

Returns the value of attribute before_download_command.



7
8
9
# File 'lib/box/release/cli.rb', line 7

def before_download_command
  @before_download_command
end

#current_definitionObject

Returns the value of attribute current_definition.



7
8
9
# File 'lib/box/release/cli.rb', line 7

def current_definition
  @current_definition
end

#download_directoryObject

Returns the value of attribute download_directory.



7
8
9
# File 'lib/box/release/cli.rb', line 7

def download_directory
  @download_directory
end

#install_commandObject

Returns the value of attribute install_command.



7
8
9
# File 'lib/box/release/cli.rb', line 7

def install_command
  @install_command
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/box/release/cli.rb', line 7

def url
  @url
end

Instance Method Details

#after_downloadObject



77
78
79
# File 'lib/box/release/cli.rb', line 77

def after_download
  Box::Release.execute! after_download_command if after_download_command
end

#before_downloadObject



73
74
75
# File 'lib/box/release/cli.rb', line 73

def before_download
  Box::Release.execute! before_download_command if before_download_command
end

#currentObject



51
52
53
54
55
56
57
58
# File 'lib/box/release/cli.rb', line 51

def current
  @current ||= 
    if File.exists?(current_definition)
      Box::Release::Loader.release_at current_definition
    else
      Box::Release::Memory.new :name => current_definition
    end
end

#latestObject



47
48
49
# File 'lib/box/release/cli.rb', line 47

def latest
  @latest ||= Box::Release::Loader.release_at url
end

#option_parserObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/box/release/cli.rb', line 19

def option_parser
  @option_parser ||= OptionParser.new do |options|
    options.banner = <<-BANNER.gsub(/^            /,'')
      Box Release CLI : update your box in command line

      Usage: #{File.basename($0)} [options]

      Options are:
    BANNER
    options.separator ""
    options.on("-c", "--current=file/name", String, 
               "The current.yml file or the current name") { |arg| self.current_definition = arg }
    options.on("-i", "--install=command", String, 
               "The install command used to install the release") { |arg| self.install_command = arg }
    options.on("-b", "--before-download=command", String, 
               "The command executed before download starts") { |arg| self.before_download_command = arg }
    options.on("-a", "--after-download=command", String, 
               "The command executed after download ends (or fails)") { |arg| self.after_download_command = arg }
    options.on("-d", "--download-dir=directory", String, 
               "The direcotry where the release file will be temporary stored") { |arg| self.download_directory = arg }
  end
end

#parse_options(arguments) ⇒ Object



42
43
44
45
# File 'lib/box/release/cli.rb', line 42

def parse_options(arguments)
  option_parser.parse!(arguments)
  @url = arguments.shift
end

#upgradeObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/box/release/cli.rb', line 81

def upgrade
  if latest and latest.newer?(current)
    Box::Release.logger.info "New release : #{latest}"
    Box::Release.download_directory = download_directory
    before_download

    begin
      latest.download
    ensure
      after_download
    end

    Box::Release.logger.info "Replace #{current} with #{latest}"
    latest.install :install_command => install_command
    Box::Release.logger.info "Successfully installed #{latest}"
  else
    Box::Release.logger.info "Keep current release : #{current} (latest is #{latest})"
    false
  end
end

#usage(output = $stdout) ⇒ Object



15
16
17
# File 'lib/box/release/cli.rb', line 15

def usage(output = $stdout)
  output.puts option_parser
end

#valid?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/box/release/cli.rb', line 60

def valid?
  current_definition
end

#validate!(output = $stdout) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/box/release/cli.rb', line 64

def validate!(output = $stdout)
  unless valid?
    usage(output)
    exit 1
  end

  self
end