Class: OsCtl::Repo::Cli::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/osctl/repo/cli/command.rb

Direct Known Subclasses

Repo

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(global_opts, opts, args) ⇒ Command

Returns a new instance of Command.



39
40
41
42
43
# File 'lib/osctl/repo/cli/command.rb', line 39

def initialize(global_opts, opts, args)
  @gopts = global_opts
  @opts = opts
  @args = args
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



37
38
39
# File 'lib/osctl/repo/cli/command.rb', line 37

def args
  @args
end

#goptsObject (readonly)

Returns the value of attribute gopts.



37
38
39
# File 'lib/osctl/repo/cli/command.rb', line 37

def gopts
  @gopts
end

#optsObject (readonly)

Returns the value of attribute opts.



37
38
39
# File 'lib/osctl/repo/cli/command.rb', line 37

def opts
  @opts
end

Class Method Details

.run(klass, method) ⇒ Object



3
4
5
6
7
8
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
# File 'lib/osctl/repo/cli/command.rb', line 3

def self.run(klass, method)
  Proc.new do |global_opts, opts, args|
    cmd = klass.new(global_opts, opts, args)

    begin
      cmd.method(method).call

    rescue OsCtl::Repo::ImageNotFound => e
      raise GLI::CustomExit.new(
        "Image not found: #{e.message}",
        OsCtl::Repo::EXIT_IMAGE_NOT_FOUND
      )

    rescue OsCtl::Repo::FormatNotFound => e
      raise GLI::CustomExit.new(
        "Format not found: #{e.message}",
        OsCtl::Repo::EXIT_FORMAT_NOT_FOUND
      )

    rescue OsCtl::Repo::BadHttpResponse => e
      raise GLI::CustomExit.new(
        "Unexpected HTTP error: #{e.message}",
        OsCtl::Repo::EXIT_HTTP_ERROR
      )

    rescue OsCtl::Repo::NetworkError => e
      raise GLI::CustomExit.new(
        "Unexpected network error: #{e.message}",
        OsCtl::Repo::EXIT_NETWORK_ERROR
      )
    end
  end
end

Instance Method Details

#require_args!(*v) ⇒ Object

Parameters:

  • v (Array)

    list of required arguments

Raises:

  • (GLI::BadCommandLine)


46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/osctl/repo/cli/command.rb', line 46

def require_args!(*v)
  if v.count == 1 && v.first.is_a?(Array)
    required = v.first
  else
    required = v
  end

  return if args.count >= required.count

  arg = required[ args.count ]
  raise GLI::BadCommandLine, "missing argument <#{arg}>"
end