module LeapCli::Commands
Constants
- COMMON_CONTENT
- DEFAULT_REPO
- GITIGNORE_CONTENT
Public Instance Methods
agree(question, options={})
click to toggle source
# File lib/leap_cli/commands/common.rb, line 81 def agree(question, options={}) while true response = ask(question, options) if response.nil? say('Please enter "yes" or "no".') elsif ["y","yes", "ye"].include?(response.downcase) return true elsif ["n", "no"].include?(response.downcase) return false else say('Please enter "yes" or "no".') end end end
ask(question, options={})
click to toggle source
# File lib/leap_cli/commands/common.rb, line 59 def ask(question, options={}) default = options[:default] if default if ends_in_whitespace?(question) question = question + "|" + default + "| " else question = question + "|" + default + "|" end end response = Readline.readline(question, true) # set to false if ever reading passwords. if response response = response.strip if response.empty? return default else return response end else return default end end
parse_node_list(nodes)
click to toggle source
# File lib/leap_cli/commands/common.rb, line 38 def parse_node_list(nodes) if nodes.is_a? Config::Object Config::ObjectList.new(nodes) elsif nodes.is_a? Config::ObjectList nodes elsif nodes.is_a? String manager.filter!(nodes) else bail! "argument error" end end
path(name)
click to toggle source
# File lib/leap_cli/commands/common.rb, line 8 def path(name) Path.named_path(name) end
say(statement)
click to toggle source
# File lib/leap_cli/commands/common.rb, line 50 def say(statement) if ends_in_whitespace?(statement) $stdout.print(statement) $stdout.flush else $stdout.puts(statement) end end
Private Instance Methods
ask_string(str, options={})
click to toggle source
don't let the user specify any of the following: y, yes, n, no they must actually input a real string
# File lib/leap_cli/commands/new.rb, line 46 def ask_string(str, options={}) while true value = ask(str, options) if value =~ /^(y|yes|n|no)$/i say "`#{value}` is not a valid value. Try again" else return value end end end
create_initial_provider_files(directory, global, options)
click to toggle source
see provider with initial files
# File lib/leap_cli/commands/new.rb, line 77 def create_initial_provider_files(directory, global, options) Dir.chdir(directory) do assert_files_missing! 'provider.json', 'common.json', 'Leapfile', :base => directory platform_dir = File.expand_path(options[:platform], "./") unless File.symlink?(platform_dir) || File.directory?(platform_dir) if global[:yes] || agree("The platform directory \"#{platform_dir}\" does not exist.\nDo you want me to create it by cloning from the\ngit repository #{DEFAULT_REPO}? ") assert_bin! 'git' ensure_dir platform_dir Dir.chdir(platform_dir) do log :cloning, "leap_platform:stable into #{platform_dir}" pty_run "git clone --branch stable #{DEFAULT_REPO} ." end else bail! end end write_file! '.gitignore', GITIGNORE_CONTENT write_file! 'provider.json', provider_content(options) write_file! 'common.json', COMMON_CONTENT write_file! 'Leapfile', leapfile_content(options) ["nodes", "services", "tags"].each do |dir| ensure_dir dir end log :completed, 'initialization' end end
create_provider_directory(global, directory)
click to toggle source
creates a new provider directory
# File lib/leap_cli/commands/new.rb, line 60 def create_provider_directory(global, directory) unless directory && directory.any? help! "Directory name is required." end unless File.exist?(directory) if global[:yes] || agree("Create directory #{directory}? ") ensure_dir directory else bail! { log :missing, "directory #{directory}" } end end Path.set_provider_path(directory) end
ends_in_whitespace?(str)
click to toggle source
true if str ends in whitespace before a color escape code.
# File lib/leap_cli/commands/common.rb, line 99 def ends_in_whitespace?(str) /[ \t](\e\[\d+(;\d+)*m)?\Z/ =~ str end
leapfile_content(options)
click to toggle source
# File lib/leap_cli/commands/new.rb, line 109 def leapfile_content(options) %[@platform_directory_path = "#{options[:platform]}"\n# see https://leap.se/en/docs/platform/config for more options] end
new_provider_action(global, options, args)
click to toggle source
# File lib/leap_cli/commands/new.rb, line 22 def new_provider_action(global, options, args) unless args.first # this should not be needed, but GLI is not making it required. bail! "Argument DIRECTORY is required." end directory = File.expand_path(args.first) create_provider_directory(global, directory) options[:domain] ||= ask_string("The primary domain of the provider: ", default: 'example.org') options[:name] ||= ask_string("The name of the provider: ", default: 'Example') options[:platform] ||= ask_string("File path of the leap_platform directory: ", default: File.expand_path('../leap_platform', directory)) options[:platform] = "./" + options[:platform] unless options[:platform] =~ /^\// options[:contacts] ||= ask_string("Default email address contacts: ", default: 'root@' + options[:domain]) options[:platform] = relative_path(options[:platform]) create_initial_provider_files(directory, global, options) end
provider_content(options)
click to toggle source
# File lib/leap_cli/commands/new.rb, line 120 def provider_content(options) %[// // General service provider configuration. // { "domain": "#{options[:domain]}", "name": { "en": "#{options[:name]}" }, "description": { "en": "You really should change this text" }, "contacts": { "default": "#{options[:contacts]}" }, "languages": ["en"], "default_language": "en", "enrollment_policy": "open" } ] end
relative_path(path)
click to toggle source
# File lib/leap_cli/commands/new.rb, line 105 def relative_path(path) Pathname.new(File.expand_path(path)).relative_path_from(Pathname.new(Path.provider)).to_s end