Skip to content

Commit b465096

Browse files
authored
Merge pull request #391 from DannyBen/add/interactive-shell
Add bashly interactive shell
2 parents 9f10e76 + 1a55dcb commit b465096

File tree

10 files changed

+103
-7
lines changed

10 files changed

+103
-7
lines changed

lib/bashly/cli.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def self.runner
1515
runner.route 'generate', to: Commands::Generate
1616
runner.route 'add', to: Commands::Add
1717
runner.route 'doc', to: Commands::Doc
18+
runner.route 'shell', to: Commands::Shell unless ENV['BASHLY_SHELL']
1819

1920
runner
2021
end

lib/bashly/commands/doc.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ module Bashly
22
module Commands
33
class Doc < Base
44
summary 'Show bashly reference documentation'
5-
help 'This command displays bite-sized help for all the bashly configuration options in the terminal.'
65

76
usage 'bashly doc [SEARCH] [--index]'
87
usage 'bashly doc (-h|--help)'

lib/bashly/commands/init.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Bashly
22
module Commands
33
class Init < Base
44
summary 'Initialize a new workspace'
5-
help 'This command will create the source folder, and place a template configuration file in it.'
5+
help 'Create the bashly source folder, and place a template configuration file in it'
66

77
usage 'bashly init [--minimal]'
88
usage 'bashly init (-h|--help)'

lib/bashly/commands/shell.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
module Bashly
2+
module Commands
3+
class Shell < Base
4+
summary 'Start an interactive bashly shell'
5+
help 'Start an interactive shell where you can run bashly commands'
6+
7+
usage 'bashly shell'
8+
usage 'bashly shell (-h|--help)'
9+
10+
def run
11+
ENV['BASHLY_SHELL'] = '1'
12+
terminal.start
13+
end
14+
15+
private
16+
17+
def terminal
18+
@terminal ||= begin
19+
terminal = MisterBin::Terminal.new runner, {
20+
autocomplete: autocomplete,
21+
show_usage: true,
22+
prompt: "\n\e[33m\e[1mbashly\e[0m > ",
23+
}
24+
25+
terminal.on('help') { runner.run %w[--help] }
26+
terminal.on('version') { runner.run %w[--version] }
27+
terminal
28+
end
29+
end
30+
31+
def runner
32+
@runner ||= Bashly::CLI.runner
33+
end
34+
35+
def autocomplete
36+
@autocomplete ||= %w[help version] + runner.commands.keys
37+
end
38+
end
39+
end
40+
end

spec/approvals/cli/commands

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Commands:
77
generate Generate the bash script and required files
88
add Add extra features and customization to your script
99
doc Show bashly reference documentation
10+
shell Start an interactive bashly shell
1011

1112
Help: bashly COMMAND --help
1213
Docs: https://bashly.dannyb.co

spec/approvals/cli/doc/help

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
Show bashly reference documentation
22

3-
This command displays bite-sized help for all the bashly configuration options
4-
in the terminal.
5-
63
Usage:
74
bashly doc [SEARCH] [--index]
85
bashly doc (-h|--help)

spec/approvals/cli/init/help

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
Initialize a new workspace
22

3-
This command will create the source folder, and place a template configuration
4-
file in it.
3+
Create the bashly source folder, and place a template configuration file in it
54

65
Usage:
76
bashly init [--minimal]

spec/approvals/cli/shell/boot

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Bashly - Bash CLI Generator
2+
3+
Commands:
4+
init Initialize a new workspace
5+
preview Generate the bash script to STDOUT
6+
validate Scan the configuration file for errors
7+
generate Generate the bash script and required files
8+
add Add extra features and customization to your script
9+
doc Show bashly reference documentation
10+
11+
Help: bashly COMMAND --help
12+
Docs: https://bashly.dannyb.co

spec/approvals/cli/shell/help

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Start an interactive bashly shell
2+
3+
Start an interactive shell where you can run bashly commands
4+
5+
Usage:
6+
bashly shell
7+
bashly shell (-h|--help)
8+
9+
Options:
10+
-h --help
11+
Show this help

spec/bashly/commands/shell_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require 'spec_helper'
2+
3+
describe Commands::Shell do
4+
subject { described_class.new }
5+
6+
context 'with --help' do
7+
it 'shows long usage' do
8+
expect { subject.execute %w[terminal --help] }.to output_approval('cli/shell/help')
9+
end
10+
end
11+
12+
context 'without parameters' do
13+
let(:terminal_double) { instance_double MisterBin::Terminal, start: nil, on: nil }
14+
15+
it 'runs MisterBin::Terminal' do
16+
allow(MisterBin::Terminal).to receive(:new).and_return(terminal_double)
17+
expect(terminal_double).to receive(:start)
18+
subject.execute %w[shell]
19+
end
20+
end
21+
22+
context 'with in-terminal commands' do
23+
before do
24+
ENV['BASHLY_SHELL'] = nil
25+
allow(Readline).to receive(:readline).and_return(*input)
26+
end
27+
28+
context 'with exit command' do
29+
let(:input) { ['exit'] }
30+
31+
it 'starts a terminal and shows CLI usage' do
32+
expect { subject.execute %w[shell] }.to output_approval 'cli/shell/boot'
33+
end
34+
end
35+
end
36+
end

0 commit comments

Comments
 (0)