Class: Fig::Command

Inherits:
Object
  • Object
show all
Includes:
Listing, PackageLoad
Defined in:
lib/fig/command.rb,
lib/fig/command/listing.rb,
lib/fig/command/options.rb,
lib/fig/command/optionerror.rb,
lib/fig/command/packageload.rb

Overview

Main program

Defined Under Namespace

Modules: Listing, PackageLoad Classes: OptionError, Options

Constant Summary

Constants included from PackageLoad

PackageLoad::DEFAULT_FIG_FILE

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_versionObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fig/command.rb', line 36

def self.get_version()
  line = nil

  begin
    File.open(
      "#{File.expand_path(File.dirname(__FILE__) + '/../../VERSION')}"
    ) do |file|
      line = file.gets
    end
  rescue
    $stderr.puts 'Could not retrieve version number. Something has mucked with your Fig install.'

    return nil
  end

  # Note that we accept anything that contains three decimal numbers
  # seperated by periods.  This allows for versions like
  # "4.3.2-super-special-version-in-3D".
  if line !~ %r< \b \d+ [.] \d+ [.] \d+ \b >x
    $stderr.puts %Q<"#{line}" does not look like a version number. Something has mucked with your Fig install.>

    return nil
  end

  return line
end

Instance Method Details

#emit_versionObject



153
154
155
156
157
158
159
160
# File 'lib/fig/command.rb', line 153

def emit_version()
  version = Fig::Command.get_version()
  return 1 if version.nil?

  puts File.basename($0) + ' v' + version

  return 0
end

#run_fig(argv) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/fig/command.rb', line 63

def run_fig(argv)
  begin
    @options = Fig::Command::Options.new(argv)
  rescue Fig::UserInputError => error
    $stderr.puts error.to_s # Logging isn't set up yet.
    return 1
  end

  if not @options.exit_code.nil?
    return @options.exit_code
  end
  @descriptor = @options.descriptor

  if @options.help?
    return @options.help
  end

  if @options.version?
    return emit_version()
  end

  configure()

  if @options.clean?
    check_required_package_descriptor('to clean')
    ensure_descriptor_and_file_were_not_both_specified()
    @repository.clean(@descriptor)
    return 0
  end

  if handle_pre_parse_list_options()
    return 0
  end

  if @options.publishing?
    return publish()
  end

  ensure_descriptor_and_file_were_not_both_specified()

  load_package_object()

  if @options.listing()
    handle_post_parse_list_options()
  elsif @options.get()
    # Ruby v1.8 emits "nil" for nil, whereas ruby v1.9 emits the empty
    # string, so, for consistency, we need to ensure that we always emit the
    # empty string.
    puts @environment[@options.get()] || ''
  elsif @options.shell_command
    @environment.execute_shell(@options.shell_command) do
      |command| @operating_system.shell_exec command
    end
  elsif @descriptor
    @environment.include_config(@base_package, @descriptor, nil)
    @environment.execute_config(
      @base_package,
      @descriptor,
      @options.command_extra_argv || []
    ) { |cmd| @operating_system.shell_exec cmd }
  elsif not @repository.updating?
    $stderr.puts "Nothing to do.\n\n"
    $stderr.puts %q<Run "fig --help" for a full list of commands.>
    return 1
  end

  return 0
end

#run_with_exception_handling(argv) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/fig/command.rb', line 132

def run_with_exception_handling(argv)
  begin
    return_code = run_fig(argv)
    return return_code
  rescue Fig::URLAccessError => error
    urls = error.urls.join(', ')
    $stderr.puts "Access to #{urls} in #{error.package}/#{error.version} not allowed."
    return 1
  rescue Fig::UserInputError => error
    log_error_message(error)
    return 1
  rescue OptionParser::InvalidOption => error
    $stderr.puts error.to_s
    $stderr.puts Fig::Command::Options::USAGE
    return 1
  rescue Fig::RepositoryError => error
    log_error_message(error)
    return 1
  end
end