-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Plugin framework #51
Merged
Merged
Plugin framework #51
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5a9ce20
removed gem from managed source code
eguzki 7ee2591
plugin framework
eguzki 1be791a
plugin framework: cri dependency added
eguzki 2d7e98b
.gitignore: add vendor path usually used by bundler
eguzki bfb8724
plugin framework: copy_command imported
eguzki bcf04e5
plugin framework: copy_command refactored as command
eguzki 28a52e6
plugin framework: import command imported
eguzki e430778
plugin framework: import_command refactored as command
eguzki 44a8bdc
plugin framework: update command imported
eguzki c751f29
plugin framework: update_command refactored as command
eguzki b674c05
plugin framework: help command
eguzki 446e7c3
plugin framework: builtin commands
eguzki 19d0867
plugin framework: doc about building plugins
eguzki 92d6da5
.travis.yml: update build script
eguzki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
/coverage/ | ||
/doc/ | ||
/pkg/ | ||
/vendor/ | ||
/spec/reports/ | ||
/tmp/ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,8 +7,8 @@ Gem::Specification.new do |spec| | |
spec.name = '3scale_toolbox' | ||
spec.version = ThreeScaleToolbox::VERSION | ||
spec.licenses = ['MIT'] | ||
spec.authors = ['Michal Cichra'] | ||
spec.email = ['[email protected]'] | ||
spec.authors = ['Michal Cichra', 'Eguzki Astiz Lezaun'] | ||
spec.email = ['[email protected]', '[email protected]'] | ||
|
||
spec.summary = %q{3scale CLI Toolbox.} | ||
spec.description = %q{3scale CLI tools to manage your API from the terminal.} | ||
|
@@ -23,5 +23,6 @@ Gem::Specification.new do |spec| | |
spec.add_development_dependency 'rake', '~> 10.0' | ||
spec.required_ruby_version = '>= 2.1' | ||
|
||
spec.add_dependency '3scale-api', '~> 0.1.2' | ||
spec.add_dependency '3scale-api', '~> 0.1' | ||
spec.add_dependency 'cri', '~> 2.10' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
# Developing 3scale Toolbox CLI plugins | ||
|
||
3scale Toolbox CLI is based on [cri](https://github.com/ddfreyne/cri) library for building command line tools. | ||
Plugin system also uses [cri](https://github.com/ddfreyne/cri) to leverage easy to develop, flexible and extensible plugin system. | ||
|
||
3scale Toolbox will load plugins installed in gems or $LOAD_PATH. Plugins are discovered via *Gem::find_files*, then loaded. | ||
Plugins must be named ‘3scale_toolbox_plugin’ (.rb, .so, etc) and placed at the root of your gem’s #require_path. | ||
|
||
Plugins may add commands to *3scale* CLI or may add *subcommands* to any existing command. | ||
Subcommands may be added to main commands or other subcommands as children. | ||
|
||
Nothing better than few examples to illustrate . | ||
|
||
Let's create a plugin to add a main `simple hello world` command. | ||
|
||
``` | ||
$ cat lib/3scale_toolbox_plugin.rb | ||
require '3scale_toolbox/cli' | ||
|
||
module FooCommand | ||
extend ThreeScaleToolbox::Command | ||
|
||
def self.command | ||
Cri::Command.define do | ||
name 'foo' | ||
usage 'foo [options]' | ||
summary '3scale foo' | ||
description '3scale foo command' | ||
flag :h, :help, 'show help for this command' do |_, cmd| | ||
puts cmd.help | ||
exit 0 | ||
end | ||
run do |opts, args, _| | ||
puts "foo done" | ||
end | ||
end | ||
end | ||
end | ||
ThreeScaleToolbox::CLI.add_command(FooCommand) | ||
|
||
$ RUBYOPT=-Ilib 3scale foo | ||
Hello World | ||
``` | ||
Few things worth to highlight. | ||
- Your module must be extended by *ThreeScaleToolbox::Command* module. It allows your command to be added to CLI command tree. | ||
- Must implement `command` module function and return instance of `Cri::Command` from [cri](https://github.com/ddfreyne/cri) | ||
- Add your command to `3scale` CLI command tree by calling `ThreeScaleToolbox::CLI.add_command` | ||
|
||
Your plugin help is also available using builtin *help* command | ||
|
||
``` | ||
$ RUBYOPT=-Ilib 3scale help foo | ||
NAME | ||
foo - foo command | ||
|
||
USAGE | ||
3scale foo [options] | ||
|
||
DESCRIPTION | ||
This command does a lot of stuff. | ||
|
||
OPTIONS | ||
-h --help show help for this command | ||
|
||
OPTIONS FOR 3SCALE | ||
-v --version Prints the version of this command | ||
``` | ||
|
||
Let's create a plugin to add a `simple hello world` subcommand for the builtin *copy* command. | ||
|
||
``` | ||
$ cat lib/3scale_toolbox_plugin.rb | ||
require '3scale_toolbox/base_command' | ||
require '3scale_toolbox/commands/copy_command' | ||
|
||
module FooCommand | ||
extend ThreeScaleToolbox::Command | ||
|
||
def self.command | ||
Cri::Command.define do | ||
name 'foo' | ||
usage 'foo [options]' | ||
summary '3scale copy foo' | ||
description '3scale copy foo subcommand' | ||
flag :h, :help, 'show help for this command' do |_, cmd| | ||
puts cmd.help | ||
exit 0 | ||
end | ||
run do |opts, args, _| | ||
puts "foo done" | ||
end | ||
end | ||
end | ||
end | ||
|
||
ThreeScaleToolbox::Commands::CopyCommand.add_subcommand(FooCommand) | ||
|
||
$ RUBYOPT=-Ilib 3scale copy foo | ||
foo done | ||
``` | ||
|
||
Few things worth to highlight. | ||
- Your module must be extended by *ThreeScaleToolbox::Command* module. It allows your command to be added to CLI command tree. | ||
- Must implement `command` module function and return instance of `Cri::Command` from [cri](https://github.com/ddfreyne/cri) | ||
- Add your subcommand to `3scale` CLI command tree by calling parent command's module's `add_subcommand` method. | ||
|
||
Checking `copy` command help, it can be verified the new subcommand `foo` is added. | ||
|
||
``` | ||
$ RUBYOPT=-Ilib 3scale help copy | ||
NAME | ||
copy - 3scale CLI copy | ||
|
||
USAGE | ||
3scale copy <command> [options] | ||
|
||
DESCRIPTION | ||
3scale CLI copy tools to manage your API from the terminal. | ||
|
||
SUBCOMMANDS | ||
foo 3scale copy foo | ||
service 3scale CLI copy service | ||
|
||
OPTIONS | ||
-h --help show help for this command | ||
|
||
OPTIONS FOR 3SCALE | ||
-v --version Prints the version of this command | ||
``` | ||
|
||
Now, package your plugin as a [gem](https://guides.rubygems.org/make-your-own-gem/) and let us know about it. | ||
|
||
## Existing Plugins |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,8 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require '3scale_toolbox' | ||
require '3scale_toolbox/cli' | ||
|
||
options, argv = ThreeScaleToolbox::CLI.parse | ||
args = ARGV.clone | ||
|
||
unless options.command | ||
puts 'Available subcommands: ' | ||
puts ThreeScaleToolbox::CLI.subcommands | ||
puts | ||
ThreeScaleToolbox::CLI.print_help! | ||
end | ||
|
||
exec options.command.full_path, *ARGV | ||
ThreeScaleToolbox::CLI.run args |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this part is the most fragile. Gems have a flat namespace where you basically would need to name your plugin as
3scale_toolbox-myplugin
to avoid polluting this global shared namespace. I'd advocate having the code installed in something such as/usr/share/3scale_toolbox/plugins
(or.local/share
...) or having a config file setting to specify it.At the very least, here we should suggest using a specific prefix for the gem names, and possibly enforce it when looking up plugin code.
Update: I see we are using a specific path to look up plugins, but I guess that is still not enforcing things like the above, and it is not clear how to install such plugins from this doc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is a common practice in Ruby ecosystem. https://guides.rubygems.org/name-your-gem/
We just require gems to have
3scale_toolbox_plugin.rb
to be automatically loaded by the3scale
executable.I don't really see any need to enforce any rules. People are free to do whatever they want to do.
It is common practice to name extensions prefixed with the gem name. Also it is common practice to namespace all your code with the correct namespace according to your gem name.