Skip to content

Instantly share code, notes, and snippets.

@johnallen3d
Last active January 23, 2018 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnallen3d/10d2d5d5423364cad21e8afe5cfa5d7d to your computer and use it in GitHub Desktop.
Save johnallen3d/10d2d5d5423364cad21e8afe5cfa5d7d to your computer and use it in GitHub Desktop.
nib plugins talk

nib plugins

nib

  • nib is a convenience wrapper for docker-compose
  • originally designed with Ruby/Rails developers in mind but has features that would be helpful for any developer

Plugin Architecture

nib is pluggable via additional gems. The plugin system is loosely based on that of minitest extensions. There are three requirements for a nib plugin:

  1. A Ruby gem that relies on gli for defining it's CLI interface following the naming convention bin/nib-*
  2. It puts a file on the LOAD_PATH that ends with _plugin.rb following the naming convention ./lib/nib_*_plugin.rb
  3. Implements #applies?, which allows the plugin to "self-select" (ie. was this command run in what appears to be an applicable project)

Plugin Example

As an example let's define a plugin for nib that caters to Ruby developers.

# ./bin/nib-ruby

#!/usr/bin/env ruby

desc 'Say hello nib-ruby'
command :console do |c|
  c.action do |_global_options, _options, _args|
    puts 'from nib-ruby'
  end
end
# ./lib/nib_ruby_plugin.rb

module Nib
  module Ruby
    def self.applies?
      !Dir.glob('Gemfile').empty?
    end
  end
end
# ./nib-ruby.gemspec

lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
...

If nib-ruby has been installed and a nib command is run from a directory containing a Gemfile nib will append gli commands from bin/nib-ruby.

Existing Plugins

nib-crypt

Opinionated secrets management.

  • Create an encryption key for a project (directory) and push it to an S3 bucket
  • Pull an encryption key from an S3 bucket for an existing project
  • Convenience wrappers around encryption and decryption (uses openssl and before mentioned key)
COMMANDS
    ...
    crypt-init  - Initialize a project (create or pull secret key)
    ...
    decrypt     - Decrypt a file
    encrypt     - Encrypt a file
    ...

nib-heroku

Integrates commands with Heroku

  • Uses the directory name to infer a Heroku application name (following Technekes convention)
  • Accepts an environment flag
  • If non-development environment flag is specified will shell out to Heroku CLI to integrate a shell session or logs
COMMANDS
    ...
    logs        - Access logs for the speified service or Heroku application
    ...
    shell       - Start a shell session in a one-off service container
    ...

Live Demo

Let's create nib-ruby!

# fire up a Ruby container
docker run --rm -it -v $PWD:$PWD -w $PWD ruby:alpine ash

# scaffold a new gem
bundle gem --test=none --mit --no-coc --bin nib-ruby
exit # container
cd nib-ruby

# edit the executable
mv exe/nib-ruby bin/
rm nib-ruby.gemspec
cp ../nib-ruby.gemspec .
vi bin/nib-ruby

# create plugin detection file
vi lib/nib_ruby_plugin.rb

# build the gem
docker run --rm -it -v $PWD:$PWD -w $PWD ruby:alpine rake build

# install the gem
gem install pkg/nib-ruby-0.1.0.gem

# does it work?! 🤞
nib help
nib hello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment