foobara-sh-cli-connector 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5d178ae7c8cb4da43e12cd56a3c3fac65dd337f43a99769312aa65db91c1383a
4
- data.tar.gz: d49bc4d7519b53c994ded1a8a25159e2a81253da5f53fa752b49e429d663d490
3
+ metadata.gz: 49472f1f26b8949b20ca528c5c31222114081776021ae2136f4ce869192c53df
4
+ data.tar.gz: c663144dbd5aa9398d86d2d52593485e3bece7bdd10f18fbec1e3a4bb5c3e18d
5
5
  SHA512:
6
- metadata.gz: 9f0e8c3f9d4502411f16040ed9aa0ce1973c1cdaa9b1c35d4924ef9300369b594517ef01d14343c4d68577fb2b9f7add74993fcbb467ab4d5bdb0d6c8da80d80
7
- data.tar.gz: ce1ff601a3485f1e727a530bfe7463086bfbd5d30e355ff2fd1ffa092caeb45493d6cc21b39aeb2fa1ff4cde4348d3c1e95974645b775362cc5952e19ef1a6cd
6
+ metadata.gz: 2b6cb7dbb901da3888895fc2b01b036c9e3bfe10bc295d4adaed6fcf76748d3223b94933d9f71785cfb5766b0fd0788734076d2c36749117a7cea022ebbc1d94
7
+ data.tar.gz: da9ef8d6866ae4eaa8422932186fd061a1da733983664104629c959b198d136cff80e9488f804a59c54942629beb65919858ce4114a31fbcff47596ac7ca37c9
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## [0.0.3]
2
+
3
+ * Make use of single-command mode in Help command
4
+
5
+ ## [0.0.2]
6
+
7
+ * Add single-command mode
8
+
1
9
  ## [0.0.1]
2
10
 
3
11
  * Add Apache-2.0 license, resulting in a dual-licensed project
@@ -12,21 +12,34 @@ module Foobara
12
12
  def execute
13
13
  print_usage
14
14
 
15
- if valid_argument?
16
- if argument_is_command?
17
- print_command_description
18
- print_command_input_options
19
- end
15
+ if single_command_mode?
16
+ print_command_description
17
+ print_command_input_options
20
18
  else
21
- print_available_actions
22
- print_available_commands
23
- end
19
+ if valid_argument?
20
+ if argument_is_command?
21
+ print_command_description
22
+ print_command_input_options
23
+ end
24
+ else
25
+ print_available_actions
26
+ print_available_commands
27
+ end
24
28
 
25
- print_global_options
29
+ print_global_options
30
+ end
26
31
 
27
32
  output_string
28
33
  end
29
34
 
35
+ def single_command_mode?
36
+ command_connector.single_command_mode
37
+ end
38
+
39
+ def command_connector
40
+ request.command_connector
41
+ end
42
+
30
43
  def print_usage
31
44
  if argument
32
45
  if valid_argument?
@@ -48,7 +61,13 @@ module Foobara
48
61
  end
49
62
 
50
63
  def print_usage_without_argument
51
- output.puts "Usage: #{program_name} [GLOBAL_OPTIONS] [ACTION] [COMMAND_OR_TYPE] [COMMAND_INPUTS]"
64
+ usage = if single_command_mode?
65
+ "[INPUTS]"
66
+ else
67
+ "[GLOBAL_OPTIONS] [ACTION] [COMMAND_OR_TYPE] [COMMAND_INPUTS]"
68
+ end
69
+
70
+ output.puts "Usage: #{program_name} #{usage}"
52
71
  end
53
72
 
54
73
  def print_usage_for_argument
@@ -78,10 +97,6 @@ module Foobara
78
97
  command_connector.program_name
79
98
  end
80
99
 
81
- def command_connector
82
- request.command_connector
83
- end
84
-
85
100
  def command_registry
86
101
  command_connector.command_registry
87
102
  end
@@ -101,7 +116,11 @@ module Foobara
101
116
  def command_class
102
117
  return @command_class if defined?(@command_class)
103
118
 
104
- @command_class = argument && command_registry.transformed_command_from_name(argument)
119
+ @command_class = if single_command_mode?
120
+ command_registry.all_transformed_command_classes.first
121
+ else
122
+ argument && command_registry.transformed_command_from_name(argument)
123
+ end
105
124
  end
106
125
 
107
126
  def argument_is_action?
@@ -156,7 +175,7 @@ module Foobara
156
175
 
157
176
  def print_command_input_options
158
177
  output.puts
159
- output.puts "Command inputs:"
178
+ output.puts single_command_mode? ? "Inputs:" : "Command inputs:"
160
179
  output.puts
161
180
  output.puts request.inputs_parser_for(command_class).parser.summarize
162
181
  end
@@ -5,6 +5,8 @@ module Foobara
5
5
 
6
6
  class Request < CommandConnectors::Request
7
7
  attr_accessor :argv,
8
+ :single_command_mode,
9
+ :command,
8
10
  :globalish_options,
9
11
  :inputs_argv,
10
12
  :action,
@@ -15,7 +17,8 @@ module Foobara
15
17
  :stderr,
16
18
  :exit
17
19
 
18
- def initialize(argv, exit: true, stdout: $stdout, stderr: $stderr, stdin: $stdin)
20
+ def initialize(argv, command:, exit: true, stdout: $stdout, stderr: $stderr, stdin: $stdin)
21
+ self.command = command
19
22
  self.argv = argv
20
23
  self.exit = exit
21
24
  self.stdin = stdin
@@ -31,6 +34,10 @@ module Foobara
31
34
  super()
32
35
  end
33
36
 
37
+ def single_command_mode?
38
+ !!command
39
+ end
40
+
34
41
  def input_serializer
35
42
  @input_serializer ||= begin
36
43
  input_format = globalish_options[:input_format]
@@ -86,6 +93,37 @@ module Foobara
86
93
  private
87
94
 
88
95
  def parse!
96
+ if single_command_mode?
97
+ parse_single_command!
98
+ else
99
+ parse_multi_command!
100
+ end
101
+
102
+ if action == "help"
103
+ globalish_options[:output_format] = "noop"
104
+ elsif action == "list"
105
+ globalish_options[:output_format] = "cli_tabular"
106
+ end
107
+
108
+ validate_parse_result!
109
+
110
+ set_serializers
111
+ end
112
+
113
+ def parse_single_command!
114
+ self.globalish_options = {}
115
+
116
+ if argv == ["--help"]
117
+ self.action = "help"
118
+ else
119
+ self.action = "run"
120
+ # TODO: needs to be the only registered command
121
+ self.argument = command
122
+ self.inputs_argv = argv
123
+ end
124
+ end
125
+
126
+ def parse_multi_command!
89
127
  if argv.empty?
90
128
  self.action = "help"
91
129
  self.globalish_options = {}
@@ -103,16 +141,6 @@ module Foobara
103
141
  self.action_options = result.parsed
104
142
  self.inputs_argv = result.remainder
105
143
  end
106
-
107
- if action == "help"
108
- globalish_options[:output_format] = "noop"
109
- elsif action == "list"
110
- globalish_options[:output_format] = "cli_tabular"
111
- end
112
-
113
- validate_parse_result!
114
-
115
- set_serializers
116
144
  end
117
145
 
118
146
  def validate_parse_result!
@@ -1,15 +1,36 @@
1
1
  module Foobara
2
2
  module CommandConnectors
3
+ class AlreadyHasAConnectedCommand < StandardError; end
4
+
3
5
  class ShCliConnector < CommandConnector
4
- attr_accessor :program_name
6
+ attr_accessor :program_name, :single_command_mode
5
7
 
6
- def initialize(*, program_name: File.basename($PROGRAM_NAME), **, &)
8
+ def initialize(*, program_name: File.basename($PROGRAM_NAME), single_command_mode: false, **, &)
7
9
  self.program_name = program_name
10
+ self.single_command_mode = single_command_mode
8
11
  super(*, **, &)
9
12
 
10
13
  # add_default_inputs_transformer ModelsToAttributesInputsTransformer
11
14
  end
12
15
 
16
+ def connect(...)
17
+ super.tap do
18
+ if single_command_mode
19
+ if command_registry.size > 1
20
+ raise AlreadyHasAConnectedCommand, "Can't have more than one command in single command mode"
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ def build_request(*, **, &)
27
+ command = if single_command_mode
28
+ command_registry.foobara_all_command.first
29
+ end
30
+
31
+ self.class::Request.new(*, **, command:, &)
32
+ end
33
+
13
34
  # TODO: this needs a better name... it's doing more than building.
14
35
  def build_response(request)
15
36
  response = super
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foobara-sh-cli-connector
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miles Georgi
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-16 00:00:00.000000000 Z
11
+ date: 2024-09-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: foobara
@@ -24,7 +24,7 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- description:
27
+ description:
28
28
  email:
29
29
  - azimux@gmail.com
30
30
  executables: []
@@ -59,7 +59,7 @@ metadata:
59
59
  source_code_uri: https://github.com/foobara/sh-cli-connector
60
60
  changelog_uri: https://github.com/foobara/sh-cli-connector/blob/main/CHANGELOG.md
61
61
  rubygems_mfa_required: 'true'
62
- post_install_message:
62
+ post_install_message:
63
63
  rdoc_options: []
64
64
  require_paths:
65
65
  - lib
@@ -74,8 +74,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  requirements: []
77
- rubygems_version: 3.4.10
78
- signing_key:
77
+ rubygems_version: 3.5.18
78
+ signing_key:
79
79
  specification_version: 4
80
80
  summary: Command-line connector for Foobara
81
81
  test_files: []