Class: Foobara::CommandConnectors::ShCliConnector

Inherits:
Foobara::CommandConnector show all
Defined in:
foobara-sh-cli-connector-0.0.16/src/sh_cli_connector.rb,
foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/request.rb,
foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/action_parser.rb,
foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/commands/help.rb,
foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser.rb,
foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/globalish_parser.rb,
foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option.rb,
foobara-sh-cli-connector-0.0.16/src/models_to_attributes_inputs_transformer.rb,
foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/commands/list_commands.rb,
foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option_set.rb,
foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option/flag.rb,
foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option/model.rb,
foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option/on_flag.rb,
foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option/off_flag.rb,
foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option/attributes.rb,
foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/serializers/cli_errors_serializer.rb,
foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/serializers/cli_result_serializer.rb,
foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/serializers/cli_tabular_serialzier.rb

Defined Under Namespace

Modules: Commands, Serializers Classes: ActionParser, GlobalishParser, InputsParser, ModelsToAttributesInputsTransformer, ParseError, Request

Instance Attribute Summary collapse

Attributes inherited from Foobara::CommandConnector

#authenticator, #capture_unknown_error, #command_registry

Instance Method Summary collapse

Methods inherited from Foobara::CommandConnector

#all_exposed_commands, allowed_rules_to_register, #authenticate, authenticator_registry, #build_command, #connect_delayed, #delayed_connections, find_builtin_command_class, #find_builtin_command_class, #foobara_manifest, #lookup_command, #mutate_response, #normalize_manifest, #patch_up_broken_parents_for_errors_with_missing_command_parents, #process_delayed_connections, register_allowed_rule, register_authenticator, #request_to_response, #run_command, #run_request, #serialize_response_body, to_authenticator, #transform_command_class, #type_from_name

Constructor Details

#initialize(program_name: File.basename($PROGRAM_NAME), single_command_mode: false, always_prefix_inputs: false) ⇒ ShCliConnector

Returns a new instance of ShCliConnector.



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
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector.rb', line 8

def initialize(
  *,
  program_name: File.basename($PROGRAM_NAME),
  single_command_mode: false,
  always_prefix_inputs: false,
  **,
  &
)
  self.program_name = program_name
  self.always_prefix_inputs = always_prefix_inputs

  connect_args = if single_command_mode
                   self.single_command_mode = true

                   if single_command_mode.is_a?(::Array)
                     single_command_mode
                   elsif single_command_mode.is_a?(::Class) && single_command_mode < Foobara::Command
                     [single_command_mode]
                   end
                 end

  super(*, **, &)

  if connect_args
    connect(*connect_args)
  end
end

Instance Attribute Details

#always_prefix_inputsObject

Returns the value of attribute always_prefix_inputs.



6
7
8
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector.rb', line 6

def always_prefix_inputs
  @always_prefix_inputs
end

#program_nameObject

Returns the value of attribute program_name.



6
7
8
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector.rb', line 6

def program_name
  @program_name
end

#single_command_modeObject

Returns the value of attribute single_command_mode.



6
7
8
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector.rb', line 6

def single_command_mode
  @single_command_mode
end

Instance Method Details

#build_requestObject



50
51
52
53
54
55
56
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector.rb', line 50

def build_request(*, **, &)
  command = if single_command_mode
              command_registry.foobara_all_command.first
            end

  super(*, **, command:, &)
end

#build_response(request) ⇒ Object

TODO: this needs a better name… it’s doing more than building.



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector.rb', line 59

def build_response(request)
  response = super
  status = response.status
  out = response.status.zero? ? request.stdout : request.stderr

  out.puts response.body

  if request.exit
    exit status
  end

  response
end

#connectObject



40
41
42
43
44
45
46
47
48
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector.rb', line 40

def connect(...)
  super.tap do
    if single_command_mode
      if command_registry.size > 1
        raise AlreadyHasAConnectedCommand, "Can't have more than one command in single command mode"
      end
    end
  end
end

#request_to_command(request) ⇒ Object



73
74
75
76
77
78
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector.rb', line 73

def request_to_command(request)
  super
rescue CommandConnector::NoCommandFoundError, ParseError => e
  request.error = e
  nil
end

#run(argv = ARGV) ⇒ Object



36
37
38
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector.rb', line 36

def run(argv = ARGV, *, **, &)
  super
end

#set_response_body(response) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector.rb', line 113

def set_response_body(response)
  request = response.request

  response.body = if request.error
                    case request.error
                    when ParseError, CommandConnector::NotFoundError
                      request.error.message
                    else
                      # :nocov:
                      raise "Not sure how to handle error: #{request.error}"
                      # :nocov:
                    end
                  else
                    request.response_body
                  end
end

#set_response_status(response) ⇒ Object



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
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector.rb', line 80

def set_response_status(response)
  request = response.request

  response.status = if request.error
                      6
                    elsif request.success?
                      0
                    else
                      errors = request.error_collection.error_array
                      error = errors.first

                      case error
                      when CommandConnector::NotFoundError, Foobara::Entity::NotFoundError
                        # TODO: we should not be coupled to Entities here...
                        # :nocov:
                        2
                        # :nocov:
                      when CommandConnector::UnauthenticatedError
                        # :nocov:
                        3
                        # :nocov:
                      when CommandConnector::NotAllowedError
                        # :nocov:
                        4
                        # :nocov:
                      when CommandConnector::UnknownError
                        # :nocov:
                        5
                        # :nocov:
                      end || 1
                    end
end