]> gitweb.fluxo.info Git - leap/leap_cli.git/commitdiff
changed `leap init` to `leap new`, made more foolproof.
authorelijah <elijah@riseup.net>
Sat, 9 Feb 2013 07:09:47 +0000 (23:09 -0800)
committerelijah <elijah@riseup.net>
Sat, 9 Feb 2013 07:09:47 +0000 (23:09 -0800)
lib/leap_cli/commands/new.rb [moved from lib/leap_cli/commands/project.rb with 51% similarity]
lib/leap_cli/path.rb

similarity index 51%
rename from lib/leap_cli/commands/project.rb
rename to lib/leap_cli/commands/new.rb
index fc3972fec8cc603afafa73ebba0fbe4301bfa5a3..c4a067e5f2d2fd7b93ddb6bdb3563204264978aa 100644 (file)
@@ -2,22 +2,23 @@ require 'fileutils'
 
 module LeapCli; module Commands
 
-  desc 'Initializes a new LEAP provider in the specified directory.'
-  arg_name 'directory-path'
+  desc 'Creates a new provider instance in the specified directory, creating it if necessary.'
+  arg_name 'DIRECTORY'
   skips_pre
-  command :init do |c|
-    c.flag 'name', :desc => "The name of the provider", :default_value => 'Example'
-    c.flag 'domain', :desc => "The primary domain of the provider", :default_value => 'example.org'
-    c.flag 'platform', :desc => "File path of the leap_platform directory", :default_value => '../leap_platform'
+  command :new do |c|
+    c.flag 'name', :desc => "The name of the provider" #, :default_value => 'Example'
+    c.flag 'domain', :desc => "The primary domain of the provider" #, :default_value => 'example.org'
+    c.flag 'platform', :desc => "File path of the leap_platform directory" #, :default_value => '../leap_platform'
+    c.flag 'contacts', :desc => "Default email address contacts" #, :default_value => 'root'
+
     c.action do |global, options, args|
-      directory = args.first
-      unless directory && directory.any?
-        help! "Directory name is required."
-      end
-      directory = File.expand_path(directory)
-      unless File.exists?(directory)
-        bail! { log :missing, "directory #{directory}" }
-      end
+      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[:platform] = relative_path(options[:platform])
       create_initial_provider_files(directory, global, options)
     end
   end
@@ -27,22 +28,37 @@ module LeapCli; module Commands
   DEFAULT_REPO = 'git://leap.se/leap_platform' # TODO: use https
 
   #
-  # creates new provider directory
+  # creates new provider directory
   #
-  def create_initial_provider_files(directory, global, options)
+  def create_provider_directory(global, directory)
+    unless directory && directory.any?
+      help! "Directory name is required."
+    end
+    unless File.exists?(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
+
+  #
+  # see provider with initial files
+  #
+  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])
-
+      platform_dir = File.expand_path(options[:platform], "./")
       unless File.symlink?(platform_dir) || File.directory?(platform_dir)
-        if global[:yes] || agree("The platform directory \"#{options[:platform]}\" does not exist.\nDo you want me to create it by cloning from the\ngit repository #{DEFAULT_REPO}? ")
+        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 into #{platform_dir}"
-            pty_run "git clone --branch develop #{DEFAULT_REPO} ."
+            pty_run "git clone --branch master #{DEFAULT_REPO} ."
             pty_run 'git submodule update --init'
           end
         else
@@ -60,13 +76,18 @@ module LeapCli; module Commands
     end
   end
 
+  def relative_path(path)
+    Pathname.new(path).relative_path_from(Pathname.new(Path.provider)).to_s
+  end
+
   def leapfile_content(options)
+    # @leap_version = "#{LeapCli::VERSION}"
     %[## Required:
 @platform_directory_path = "#{options[:platform]}"
-@leap_version = "#{LeapCli::VERSION}"
 
 ## Optional:
 # @custom_vagrant_vm_line = "config.vm.boot_mode = :gui"
+# @log = "/tmp/leap.log"
 ]
   end
 
@@ -89,6 +110,9 @@ EOS
   "description": {
     "en": "You really should change this text"
   },
+  "contacts": {
+    "default": "#{options[:contacts]}"
+  },
   "languages": ["en"],
   "default_language": "en",
   "enrollment_policy": "open"
index 7628628c8eccaa276114ea1e78a14e50aeaf676b..634284dd5083633b28bf6207203d82b4ba2606f8 100644 (file)
@@ -135,8 +135,12 @@ module LeapCli; module Path
   end
 
   def self.relative_path(path, provider_dir=Path.provider)
-    path = named_path(path, provider_dir)
-    path.sub(/^#{Regexp.escape(provider_dir)}\//,'')
+    if provider_dir
+      path = named_path(path, provider_dir)
+      path.sub(/^#{Regexp.escape(provider_dir)}\//,'')
+    else
+      path
+    end
   end
 
 end; end