Skip to content

Commit

Permalink
Bring back the YAML pasting dialog for stack commands (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
kke authored and jakolehm committed Nov 9, 2017
1 parent c42ca33 commit 059d82b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 32 deletions.
31 changes: 0 additions & 31 deletions lib/kontena/plugin/shell/callbacks/stack_file.rb

This file was deleted.

27 changes: 27 additions & 0 deletions lib/kontena/plugin/shell/prompt_loader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'kontena/cli/stacks/yaml/stack_file_loader'

module Kontena
module Plugin
module Shell
class PromptLoader < Kontena::Cli::Stacks::YAML::StackFileLoader
def self.match?(source, parent = nil)
source.end_with?('kontena.yml') && !Kontena::Cli::Stacks::YAML::FileLoader.match?(source, parent)
end

def read_content
content = Kontena.prompt.multiline("Enter or paste a stack YAML").join
raise "Invalid stack YAML" unless YAML.safe_load(content).kind_of?(Hash)
content
end

def origin
"prompt"
end

def registry
"prompt"
end
end
end
end
end
12 changes: 11 additions & 1 deletion lib/kontena/plugin/shell/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,17 @@ def run
end

# Hook stack command kontena.yml content prompting
require 'kontena/plugin/shell/callbacks/stack_file'
if Gem::Version.new(Kontena::Cli::VERSION) >= Gem::Version.new('1.4.0')
require 'kontena/plugin/shell/prompt_loader'
else
require 'kontena/cli/stacks/validate_command'
require 'kontena/cli/stacks/install_command'
require 'kontena/cli/stacks/upgrade_command'
require 'kontena/plugin/shell/stacks_common_ext'
Kontena::Cli::Stacks::ValidateCommand.send(:include, Kontena::Plugin::Shell::StacksCommonExt)
Kontena::Cli::Stacks::InstallCommand.send(:include, Kontena::Plugin::Shell::StacksCommonExt)
Kontena::Cli::Stacks::UpgradeCommand.send(:include, Kontena::Plugin::Shell::StacksCommonExt)
end

Readline.completion_proc = Proc.new do |word|
line = Readline.line_buffer
Expand Down
21 changes: 21 additions & 0 deletions lib/kontena/plugin/shell/stacks_common_ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'tempfile'

module Kontena
module Plugin
module Shell
module StacksCommonExt
def reader_from_yaml(*args)
if args.first == 'kontena.yml' && !File.exist?('kontena.yml')
tempfile = Tempfile.new('kontena.yml')
tempfile.write(Kontena.prompt.multiline("Enter or paste a stack YAML").join)
tempfile.close
args[0] = tempfile.path
end
reader = super(*args)
File.unlink(args[0]) if File.exist?(args[0])
reader
end
end
end
end
end

0 comments on commit 059d82b

Please sign in to comment.