Class: Razor::CLI::Parse

Inherits:
Object
  • Object
show all
Defined in:
lib/razor/cli/parse.rb

Constant Summary collapse

DEFAULT_RAZOR_API =
"http://localhost:8080/api"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Parse

Returns a new instance of Parse.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/razor/cli/parse.rb', line 106

def initialize(args)
  parse_and_set_api_url(ENV["RAZOR_API"] || DEFAULT_RAZOR_API, :env)
  @args = args.dup
  @format = 'short'
  @args = get_optparse.order(@args)
  @args = set_help_vars(@args)
  if @args == ['version'] or @show_version
    @show_version = true
  elsif @args.any?
    @navigation = @args.dup
  else
    # Called with no remaining arguments to parse.
    @option_help = true
  end
end

Instance Attribute Details

#api_urlObject (readonly)

Returns the value of attribute api_url.



104
105
106
# File 'lib/razor/cli/parse.rb', line 104

def api_url
  @api_url
end

#argsObject (readonly)

Returns the value of attribute args.



104
105
106
# File 'lib/razor/cli/parse.rb', line 104

def args
  @args
end

#formatObject (readonly)

Returns the value of attribute format.



104
105
106
# File 'lib/razor/cli/parse.rb', line 104

def format
  @format
end

Instance Method Details

#dump_response?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/razor/cli/parse.rb', line 100

def dump_response?
  !!@dump
end

#get_optparseObject



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
40
41
42
# File 'lib/razor/cli/parse.rb', line 9

def get_optparse
  @optparse ||= OptionParser.new do |opts|
    opts.banner = "Usage: razor [FLAGS] NAVIGATION\n"

    opts.on "-d", "--dump", "Dumps API output to the screen" do
      @dump = true
    end

    opts.on "-f", "--full", "Show full details when viewing entities" do
      @format = 'full'
    end

    opts.on "-s", "--short", "Show shortened details when viewing entities" do
      @format = 'short'
    end

    opts.on "-u", "--url URL",
      "The full Razor API URL, can also be set\n" + " "*37 +
      "with the RAZOR_API environment variable\n" + " "*37 +
      "(default #{DEFAULT_RAZOR_API})" do |url|
      parse_and_set_api_url(url, :opts)
    end

    opts.on "-v", "--version", "Show the version of Razor" do
      @show_version = true
    end

    opts.on "-h", "--help", "Show this screen" do
      # If searching for a command's help, leave the argument for navigation.
      @option_help = true
    end

  end
end

#helpObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/razor/cli/parse.rb', line 58

def help
  output = get_optparse.to_s
  begin
    output << "\#{list_things(\"Collections\", navigate.collections)}\n\n  Navigate to entries of a collection using COLLECTION NAME, for example,\n  'nodes node15'  for the  details of a node or 'nodes node15 log' to see\n  the log for node15\n\#{list_things(\"Commands\", navigate.commands)}\n\n  Pass arguments to commands either directly by name ('--name=NAME')\n  or save the JSON body for the  command  in a file and pass it with\n  '--json FILE'.  Using --json is the only way to pass  arguments in\n  nested structures such as the configuration for a broker.\n\n"
  rescue RestClient::Unauthorized
    output << "Error: Credentials are required to connect to the server at \#{@api_url}\"\n"
  rescue
    output << "Error: Could not connect to the server at \#{@api_url}. More help is available after pointing\nthe client to a Razor server\n"
  end
  output
end

#list_things(name, items) ⇒ Object



44
45
46
47
48
49
# File 'lib/razor/cli/parse.rb', line 44

def list_things(name, items)
  "\n    #{name}:\n" +
    items.map {|x| x["name"]}.compact.sort.map do |name|
    "        #{name}"
  end.join("\n")
end


142
143
144
# File 'lib/razor/cli/parse.rb', line 142

def navigate
  @navigate ||=Navigate.new(self, @navigation)
end

#set_help_vars(rest) ⇒ Object

This method sets the appropriate help flags ‘@command_help` and `@option_help`, then returns a new set of arguments.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/razor/cli/parse.rb', line 124

def set_help_vars(rest)
  # Find and remove 'help' variations anywhere in the command.
  if rest.any? { |arg| ['-h', '--help'].include? arg } or
      rest.first == 'help' or rest.drop(1).first == 'help'
    rest = rest.reject { |arg| ['-h', '--help', 'help'].include? arg }
    # If anything is left, assume it is a command.
    if rest.any?
      @command_help = true
    else
      @option_help = true
    end
  end
  if @option_help && rest.any?
    @command_help = true
  end
  rest
end

#show_command_help?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/razor/cli/parse.rb', line 96

def show_command_help?
  !!@command_help
end

#show_help?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/razor/cli/parse.rb', line 92

def show_help?
  !!@option_help
end

#show_version?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/razor/cli/parse.rb', line 88

def show_version?
  !!@show_version
end

#versionObject



51
52
53
54
55
56
# File 'lib/razor/cli/parse.rb', line 51

def version
  "Razor Server version: \#{navigate.server_version}\nRazor Client version: \#{Razor::CLI::VERSION}\n  VERSION\nend\n"