]> gitweb.fluxo.info Git - leap/leap_cli.git/commitdiff
improved robustness of `leap new`
authorelijah <elijah@riseup.net>
Mon, 24 Mar 2014 15:32:44 +0000 (08:32 -0700)
committerelijah <elijah@riseup.net>
Mon, 24 Mar 2014 15:32:44 +0000 (08:32 -0700)
lib/leap_cli/commands/new.rb

index 286fde70357481e3cb1fdf5257e1358643dc47c7..038e6e4773a74549f98e843558e1670829e9ff68 100644 (file)
@@ -14,10 +14,11 @@ module LeapCli; module Commands
     c.action do |global, options, args|
       directory = File.expand_path(args.first)
       create_provider_directory(global, directory)
-      options[:domain]   ||= ask("The primary domain of the provider: ") {|q| q.default = 'example.org'}
-      options[:name]     ||= ask("The name of the provider: ") {|q| q.default = 'Example'}
-      options[:platform] ||= ask("File path of the leap_platform directory: ") {|q| q.default = File.expand_path('../leap_platform', directory)}
-      options[:contacts] ||= ask("Default email address contacts: ") {|q| q.default = 'root@' + options[:domain]}
+      options[:domain]   ||= ask_string("The primary domain of the provider: ") {|q| q.default = 'example.org'}
+      options[:name]     ||= ask_string("The name of the provider: ") {|q| q.default = 'Example'}
+      options[:platform] ||= ask_string("File path of the leap_platform directory: ") {|q| q.default = File.expand_path('../leap_platform', directory)}
+      options[:platform] = "./" + options[:platform] unless options[:platform] =~ /^\//
+      options[:contacts] ||= ask_string("Default email address contacts: ") {|q| q.default = 'root@' + options[:domain]}
       options[:platform] = relative_path(options[:platform])
       create_initial_provider_files(directory, global, options)
     end
@@ -27,6 +28,21 @@ module LeapCli; module Commands
 
   DEFAULT_REPO = 'https://leap.se/git/leap_platform.git'
 
+  #
+  # don't let the user specify any of the following: y, yes, n, no
+  # they must actually input a real string
+  #
+  def ask_string(str, &block)
+    while true
+      value = ask(str, &block)
+      if value =~ /^(y|yes|n|no)$/i
+        say "`#{value}` is not a valid value. Try again"
+      else
+        return value
+      end
+    end
+  end
+
   #
   # creates a new provider directory
   #
@@ -77,7 +93,7 @@ module LeapCli; module Commands
   end
 
   def relative_path(path)
-    Pathname.new(path).relative_path_from(Pathname.new(Path.provider)).to_s
+    Pathname.new(File.expand_path(path)).relative_path_from(Pathname.new(Path.provider)).to_s
   end
 
   def leapfile_content(options)