omnicli 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 991d1b77559f0dd2950f7e139720ad30d43f122134d9a1606d77d4669d68739c
4
+ data.tar.gz: 0b165c2c86f3e0d7ae9348e9e80021bb810eb8a185d0e74e157b7b456653db4d
5
+ SHA512:
6
+ metadata.gz: 18e70e6dc8accc71a24d789713d893b56d8bd6bf75fe4c7b2520c2259a58a89456992f67e7ddc13dd21bfd2b711045cfff353f15ee4d00b6708b97252b934139
7
+ data.tar.gz: 612df92b0e41b5479a8c7aad8366772436ab94a56e7c4d91a22147c2deba87747aa9623a5bcae3b42dabaaf9637bbed90bdd8e84cb1d7b942fe1a0fbc3fb01ce
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Raphaël Beamonte <raphael.beamonte@gmail.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,110 @@
1
+ # omnicli (sdk-ruby)
2
+
3
+ Ruby SDK for building Omni commands.
4
+
5
+ ## Overview
6
+
7
+ `omnicli` is a Ruby gem that provides functionality to help build commands that will be executed by Omni. It offers various utilities and helpers that make it easier to work with Omni's features from within Ruby.
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ gem install omnicli
13
+ ```
14
+
15
+ Or add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'omnicli'
19
+ ```
20
+
21
+ And then execute:
22
+ ```bash
23
+ bundle install
24
+ ```
25
+
26
+ ## Features
27
+
28
+ ### Argument Parsing
29
+
30
+ The SDK can read omni-parsed arguments from environment variables into a familiar Ruby Hash format:
31
+
32
+ ```ruby
33
+ require 'omnicli'
34
+
35
+ begin
36
+ args = OmniCli.parse!
37
+ # Access your command's arguments as hash keys
38
+ if args[:verbose]
39
+ puts "Verbose mode enabled"
40
+ end
41
+ if args[:input_file]
42
+ puts "Processing file: #{args[:input_file]}"
43
+ end
44
+ rescue OmniCli::ArgListMissingError
45
+ puts "No Omni CLI arguments found. Make sure 'argparser: true' is set for your command."
46
+ end
47
+ ```
48
+
49
+ The arguments are returned as a Hash with symbol keys, with their values in the expected types (strings, integers, floats, booleans or arrays of these types).
50
+
51
+ ### Integration with omni
52
+
53
+ The argument parser of omni needs to be enabled for your command. This can be done as part of the [metadata](https://omnicli.dev/reference/custom-commands/path/metadata-headers) of your command, which can either be provided as a separate file:
54
+
55
+ ```
56
+ your-repo
57
+ └── commands
58
+ ├── your-command.rb
59
+ └── your-command.metadata.yaml
60
+ ```
61
+
62
+ ```yaml
63
+ # your-command.metadata.yaml
64
+ argparser: true
65
+ ```
66
+
67
+ Or as part of your Ruby command headers:
68
+
69
+ ```ruby
70
+ # your-command.rb
71
+ #
72
+ # argparser: true
73
+ require 'omnicli'
74
+ ...
75
+ ```
76
+
77
+ ## Requirements
78
+
79
+ - Ruby 2.6 or higher
80
+ - No additional dependencies required
81
+
82
+ ## Development
83
+
84
+ After checking out the repo, run:
85
+
86
+ ```bash
87
+ # Install dependencies
88
+ bundle install
89
+
90
+ # Run tests
91
+ bundle exec rspec
92
+ ```
93
+
94
+ For local development, you can also run:
95
+ ```bash
96
+ # Clone the repository
97
+ omni clone https://github.com/omnicli/sdk-ruby.git
98
+ # Install dependencies
99
+ omni up
100
+ # Run tests
101
+ omni test
102
+ ```
103
+
104
+ ## Contributing
105
+
106
+ Bug reports and pull requests are welcome on GitHub at https://github.com/omnicli/sdk-ruby.
107
+
108
+ ## License
109
+
110
+ The gem is available as open source under the terms of the MIT License.
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OmniCli
4
+ # Base exception for omnicli-related errors
5
+ class Error < StandardError; end
6
+
7
+ # Raised when the OMNI_ARG_LIST environment variable is missing
8
+ class ArgListMissingError < Error
9
+ def initialize(msg = "OMNI_ARG_LIST environment variable is not set. " \
10
+ 'Are you sure "argparser: true" is set for this command?')
11
+ super
12
+ end
13
+ end
14
+
15
+ # Base class for invalid value errors
16
+ class InvalidValueError < Error; end
17
+
18
+ # Raised when an invalid boolean value is encountered
19
+ class InvalidBooleanValueError < InvalidValueError
20
+ def initialize(value)
21
+ super("expected 'true' or 'false', got '#{value}'")
22
+ end
23
+ end
24
+
25
+ # Raised when an invalid integer value is encountered
26
+ class InvalidIntegerValueError < InvalidValueError
27
+ def initialize(value)
28
+ super("expected integer, got '#{value}'")
29
+ end
30
+ end
31
+
32
+ # Raised when an invalid float value is encountered
33
+ class InvalidFloatValueError < InvalidValueError
34
+ def initialize(value)
35
+ super("expected float, got '#{value}'")
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "errors"
4
+
5
+ module OmniCli
6
+ # Parser for Omni CLI arguments from environment variables
7
+ class Parser
8
+ # Parse arguments from environment variables into a Hash with symbol keys
9
+ #
10
+ # @return [Hash{Symbol => Object}] parsed arguments with proper types
11
+ # @raise [ArgListMissingError] if OMNI_ARG_LIST is not set
12
+ # @raise [InvalidBooleanValueError] if a boolean value is invalid
13
+ # @raise [InvalidIntegerValueError] if an integer value is invalid
14
+ # @raise [InvalidFloatValueError] if a float value is invalid
15
+ # @example
16
+ # parser = OmniCli::Parser.new
17
+ # args = parser.parse!
18
+ # puts args[:verbose] if args[:verbose]
19
+ def parse!
20
+ list = arg_list
21
+ return {} if list.empty?
22
+
23
+ list.each_with_object({}) do |name, args|
24
+ args[name.downcase.to_sym] = parse_argument(name)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def arg_list
31
+ ENV["OMNI_ARG_LIST"]&.split || raise(ArgListMissingError)
32
+ end
33
+
34
+ def parse_argument(name)
35
+ type_info = arg_type(name)
36
+ return nil unless type_info
37
+
38
+ base_type, size = type_info
39
+
40
+ if size
41
+ Array.new(size) { |i| arg_value(name, base_type, i) }
42
+ else
43
+ arg_value(name, base_type)
44
+ end
45
+ end
46
+
47
+ def arg_type(name)
48
+ return nil unless (type_str = ENV.fetch("OMNI_ARG_#{name.upcase}_TYPE", nil))
49
+
50
+ if type_str.include?("/")
51
+ base_type, size = type_str.split("/")
52
+ [base_type, size.to_i]
53
+ else
54
+ [type_str, nil]
55
+ end
56
+ end
57
+
58
+ def arg_value(name, type, index = nil)
59
+ key = ["OMNI_ARG", name.upcase, "VALUE", index].compact.join("_")
60
+ value = ENV.fetch(key, nil)
61
+
62
+ return default_value(type) if value.nil?
63
+
64
+ convert_value(value, type)
65
+ end
66
+
67
+ def default_value(type)
68
+ type == "str" ? "" : nil
69
+ end
70
+
71
+ def convert_value(value, type)
72
+ case type
73
+ when "bool"
74
+ parse_boolean(value)
75
+ when "int"
76
+ parse_integer(value)
77
+ when "float"
78
+ parse_float(value)
79
+ else
80
+ value
81
+ end
82
+ end
83
+
84
+ def parse_boolean(value)
85
+ case value.downcase
86
+ when "true" then true
87
+ when "false" then false
88
+ else raise InvalidBooleanValueError, value
89
+ end
90
+ end
91
+
92
+ def parse_integer(value)
93
+ Integer(value)
94
+ rescue ArgumentError, TypeError
95
+ raise InvalidIntegerValueError, value
96
+ end
97
+
98
+ def parse_float(value)
99
+ Float(value)
100
+ rescue ArgumentError, TypeError
101
+ raise InvalidFloatValueError, value
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OmniCli
4
+ # Read version from VERSION file, or default to 0.0.0-unreleased
5
+ VERSION = if File.exist?(File.expand_path("VERSION", __dir__))
6
+ File.read(File.expand_path("VERSION", __dir__)).strip
7
+ else
8
+ "0.0.0-unreleased"
9
+ end
10
+ end
data/lib/omnicli.rb ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "omnicli/version"
4
+ require_relative "omnicli/errors"
5
+ require_relative "omnicli/parser"
6
+
7
+ # The OmniCli module provides functionality to build Omni commands in Ruby.
8
+ module OmniCli
9
+ # Create a new parser instance and parse arguments
10
+ #
11
+ # @return [OpenStruct] parsed arguments
12
+ # @raise [ArgListMissingError] if OMNI_ARG_LIST is not set
13
+ # @raise [InvalidBooleanValueError] if a boolean value is invalid
14
+ # @raise [InvalidIntegerValueError] if an integer value is invalid
15
+ # @raise [InvalidFloatValueError] if a float value is invalid
16
+ def self.parse!
17
+ Parser.new.parse!
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omnicli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Raphaël Beamonte
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-10-23 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This package provides functionality to build Omni commands in Ruby
14
+ email:
15
+ - raphael.beamonte@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE
21
+ - README.md
22
+ - lib/omnicli.rb
23
+ - lib/omnicli/VERSION
24
+ - lib/omnicli/errors.rb
25
+ - lib/omnicli/parser.rb
26
+ - lib/omnicli/version.rb
27
+ homepage: https://omnicli.dev
28
+ licenses:
29
+ - MIT
30
+ metadata:
31
+ homepage_uri: https://omnicli.dev
32
+ source_code_uri: https://github.com/omnicli/sdk-ruby
33
+ rubygems_mfa_required: 'true'
34
+ allowed_push_host: https://rubygems.org
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 2.6.0
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 3.5.16
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Ruby SDK for building Omni commands
54
+ test_files: []