From a54eaeef363795f86fdb4cdb0f7f1fa648c0e02b Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Thu, 8 Feb 2024 14:21:39 -0500 Subject: [PATCH 1/6] Get basic CLI demo working --- Gemfile | 1 + Gemfile.lock | 2 ++ app/controllers/cli_controller.rb | 7 ++++ app/models/web_cli.rb | 33 +++++++++++++++++++ app/views/cli/execute.html.erb | 2 ++ app/views/cli/show.html.erb | 45 ++++++++++++++++++++++++++ app/views/layouts/application.html.erb | 10 +++--- config/routes.rb | 2 ++ 8 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 app/controllers/cli_controller.rb create mode 100644 app/models/web_cli.rb create mode 100644 app/views/cli/execute.html.erb create mode 100644 app/views/cli/show.html.erb diff --git a/Gemfile b/Gemfile index 40171e5..cfa4db6 100644 --- a/Gemfile +++ b/Gemfile @@ -10,6 +10,7 @@ flipper_version = '~> 1.2.2' gem 'flipper-cloud', flipper_version gem 'flipper-ui', flipper_version gem 'flipper-active_record', flipper_version +gem 'ansi-to-html' group :development, :test do gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] diff --git a/Gemfile.lock b/Gemfile.lock index c19e14f..a70c09c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -77,6 +77,7 @@ GEM tzinfo (~> 2.0) addressable (2.8.6) public_suffix (>= 2.0.2, < 6.0) + ansi-to-html (0.0.3) base64 (0.2.0) bcrypt (3.1.20) bigdecimal (3.1.6) @@ -264,6 +265,7 @@ PLATFORMS x86_64-linux DEPENDENCIES + ansi-to-html bcrypt byebug capybara diff --git a/app/controllers/cli_controller.rb b/app/controllers/cli_controller.rb new file mode 100644 index 0000000..cef6a4e --- /dev/null +++ b/app/controllers/cli_controller.rb @@ -0,0 +1,7 @@ +class CliController < ApplicationController + def execute + args = Shellwords.split(params[:prompt].to_s) + @status, @output = WebCli.run(args) + render layout: false ## FIXME: render full page if not xhr + end +end diff --git a/app/models/web_cli.rb b/app/models/web_cli.rb new file mode 100644 index 0000000..2eebb6a --- /dev/null +++ b/app/models/web_cli.rb @@ -0,0 +1,33 @@ +require 'flipper/cli' + +class WebCli < Flipper::CLI + def load_environment! + # already loaded, so no need + end + + def run(...) + original_stdout = $stdout + original_stderr = $stderr + + output = StringIO.new + # Prentend this a TTY so we get colorization + def output.tty? + true + end + + $stdout = output + $stderr = output + status = 0 + + begin + super(...) + rescue SystemExit => e + status = e.status + end + + [status, output.string] + ensure + $stdout = original_stdout + $stderr = original_stderr + end +end diff --git a/app/views/cli/execute.html.erb b/app/views/cli/execute.html.erb new file mode 100644 index 0000000..621b811 --- /dev/null +++ b/app/views/cli/execute.html.erb @@ -0,0 +1,2 @@ +$ flipper <%= params[:prompt] %> +<%=raw Ansi::To::Html.new(h(@output)).to_html(:xterm) %> diff --git a/app/views/cli/show.html.erb b/app/views/cli/show.html.erb new file mode 100644 index 0000000..8b8087f --- /dev/null +++ b/app/views/cli/show.html.erb @@ -0,0 +1,45 @@ + + + diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index cbe939b..f85e5cc 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -42,13 +42,15 @@
-
+
<%= yield %>
- + <% if content_for?(:aside) %> + + <% end %>
diff --git a/config/routes.rb b/config/routes.rb index 22d979c..9ffae38 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -18,6 +18,8 @@ get 'example/index', as: :examples get 'example/:slug', to: "example#show", as: :example + get "cli", to: "cli#show", as: :cli + post "cli", to: "cli#execute" get 'connect/:token', to: "cloud#connect", as: :cloud_connect From be45568fcbccc26bdda3f3342b402d1370b72010 Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Thu, 8 Feb 2024 14:40:24 -0500 Subject: [PATCH 2/6] Tweak text styles --- app/views/cli/show.html.erb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/views/cli/show.html.erb b/app/views/cli/show.html.erb index 8b8087f..aed4fed 100644 --- a/app/views/cli/show.html.erb +++ b/app/views/cli/show.html.erb @@ -1,5 +1,5 @@ -