Skip to content
This repository has been archived by the owner on May 22, 2018. It is now read-only.

Commit

Permalink
Merge pull request #81 from grosser/autocomplete
Browse files Browse the repository at this point in the history
add boxen autocomplete
  • Loading branch information
dgoodlad committed Jan 14, 2014
2 parents f02d861 + 47bea3f commit 535cc65
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lib/boxen/autocomplete.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env ruby

module Boxen
module Autocomplete
# refresh options via: ruby -e 'puts `boxen -h 2>/dev/null`.scan(/\-+[a-z\?-]+/).inspect'
OPTIONS = ["--debug", "--pretend", "--noop", "--report", "--env", "-h", "-?", "--help", "--disable-service", "--enable-service", "--restart-service", "--disable-services", "--enable-services", "--restart-services", "--list-services", "--homedir", "--logfile", "--login", "--no-fde", "--no-pull", "--no-issue", "--stealth", "--token", "--profile", "--future-parser", "--projects", "--srcdir", "--user", "--no-color"]
SERVICE_OPTIONS = OPTIONS.select { |o| o.end_with?("-service") }
DIR_OPTIONS = ["--logfile", "--homedir"]

class << self
def complete(typed)
if part = after(typed, SERVICE_OPTIONS)
available_services.select { |s| s.start_with?(part) }
elsif after(typed, DIR_OPTIONS)
[]
else
OPTIONS.select { |o| o.start_with?(typed[/([a-z-]*)$/,1].to_s) }
end
end

private

def after(typed, kind)
typed[/(#{kind.join("|")})\s+([^\s]*)?$/, 2]
end

# keep in sync with boxen/service.rb
def available_services
Dir["/Library/LaunchDaemons/dev.*.plist"].map { |f| f[/dev\.(.*)\.plist/, 1] }
end
end
end
end

if $0 == __FILE__
puts Boxen::Autocomplete.complete(ENV["COMP_LINE"])
end
3 changes: 3 additions & 0 deletions manifests/environment.pp
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,8 @@
ensure => $relative_bin_on_path_ensure,
source => 'puppet:///modules/boxen/relative_bin_on_path.sh',
priority => 'lowest' ;
'boxen_autocomplete':
content => template('boxen/boxen_autocomplete.sh.erb'),
priority => 'lowest' ;
}
}
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
fixture_path = File.expand_path(File.join(__FILE__, '..', 'fixtures'))

RSpec.configure do |c|
c.mock_framework = :rspec
c.module_path = File.join(fixture_path, 'modules')
c.manifest_dir = File.join(fixture_path, 'manifests')
end
45 changes: 45 additions & 0 deletions spec/unit/boxen/autocomplete_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'spec_helper'
require 'boxen/autocomplete'

describe Boxen::Autocomplete do
describe ".complete" do
def call(string)
Boxen::Autocomplete.complete(string)
end

let(:available_services) { ["mysql", "myother", "nginx"] }

before do
Boxen::Autocomplete.stub(:available_services).and_return(available_services)
end

it "completes from nothing" do
call("boxen ").should include "-h"
end

it "completes from partial option" do
call("boxen --res").should == ["--restart-service", "--restart-services"]
end

it "completes from end of service" do
call("boxen --restart-service").should == ["--restart-service", "--restart-services"]
end

it "completes from after service" do
call("boxen --restart-service ").should == available_services
end

it "completes from partial service name" do
call("boxen --restart-service my").should == (available_services - ["nginx"])
end

it "completes from after service name" do
call("boxen --restart-service nginx ").should include "-h"
end

it "completes files after file options" do
call("boxen --logfile ").should == []
call("boxen --homedir ").should == []
end
end
end
4 changes: 4 additions & 0 deletions templates/boxen_autocomplete.sh.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Autocomplete for boxen

<% script = File.expand_path("../../lib/boxen/autocomplete.rb", __FILE__) %>
type complete >/dev/null && complete -C <%= script %> -o default boxen

0 comments on commit 535cc65

Please sign in to comment.