lib/branch/name/cli.rb in branch-name-3.10.2 vs lib/branch/name/cli.rb in branch-name-3.10.3

- old
+ new

@@ -36,85 +36,81 @@ desc 'create [OPTIONS] DESCRIPTION [TICKET]', 'Formulate a branch name based on a ticket description and optional ticket' long_desc <<-LONG_DESC NAME - \x5 + `branch-name create` -- will formulate a Git branch name based on the DESCRIPTION and optional TICKET provided. SYNOPSIS - \x5 + branch-name create [-i|-l|-f|-d|-s|-p|-x] DESCRIPTION [TICKET] - \x5 The following options are available: - \x5 NOTE: Default option values will be overidden if .branch-name config files + NOTE: Default option values will be overidden if .branch-name config files are present. Run `branch-name config info` to determine what config files are present. - \x5 -d: Forces the branch name to lower case. + -d: Forces the branch name to lower case. The default is: #{DEFAULT_BRANCH_NAME_OPTIONS['create']['downcase']}. - \x5 -f: Used with the -p option. If -f is specified, project files + -f: Used with the -p option. If -f is specified, project files will be created in the PROJECT_LOCATION specified by the -l option. The default is: #{DEFAULT_BRANCH_NAME_OPTIONS['create']['project_files']}. - \x5 -i: Interactive. Used with the -p option. If -i is specified, you will + -i: Interactive. Used only with the -p option. If -i is specified, you will be prompted when creating project folders. If -i is not specified, you will NOT be prompted when creating project folders. - \x5\x5 -l PROJECT_LOCATION: Indicates where the project should be created. + -l PROJECT_LOCATION: Indicates where the project should be created. A "project" is a folder that is created in the PROJECT_LOCATION specified, whose name is equivalent to the branch name that is formulated. The default is: "#{Locatable.project_folder(options: options)}". - \x5\x5 -p: Indicates that a project should be created. + -p: Indicates that a project should be created. The default is: #{DEFAULT_BRANCH_NAME_OPTIONS['create']['project']}. - \x5\x5 -s SEPARATOR: Indicates the SEPARATOR that will be used to delimit tokens in the branch name. + -s SEPARATOR: Indicates the SEPARATOR that will be used to delimit tokens in the branch name. The default SEPARATOR is: '#{DEFAULT_BRANCH_NAME_OPTIONS['create']['separator']}'. - \x5 -x FORMAT_STRING: This is a string that determines the format of the branch name + -x FORMAT_STRING: This is a string that determines the format of the branch name that is formulated. The following is a list of required placeholders you must put in your format string to format the branch name: [%t, %d]. - \x5Where %t will be replaced by the ticket. - \x5Where %d will be replaced by the ticket description. - \x5The following is a list of optional placeholders you may put + Where %t will be replaced by the ticket. + Where %d will be replaced by the ticket description. + The following is a list of optional placeholders you may put in your format string to format the branch name: [%u]. - \x5Where %u will be replaced with your username (`Etc.getlogin`, https://rubygems.org/gems/etc). - \x5The default format string is: "#{DEFAULT_BRANCH_NAME_OPTIONS['create']['format_string']}". + Where %u will be replaced with your username (`Etc.getlogin`, https://rubygems.org/gems/etc). + The default format string is: "#{DEFAULT_BRANCH_NAME_OPTIONS['create']['format_string']}". LONG_DESC method_option :downcase, type: :boolean, aliases: '-d' method_option :separator, type: :string, aliases: '-s' method_option :format_string, type: :string, aliases: '-x' method_option :project, type: :boolean, aliases: '-p' method_option :project_location, type: :string, aliases: '-l' method_option :project_files, type: :array, aliases: '-f' - method_option :interactive, type: :boolean, aliases: '-i' + method_option :interactive, type: :boolean, optional: true, aliases: '-i' def create(ticket_description, ticket = nil) - if ticket_description.blank? - say_error 'description is required', ERROR - exit 1 - end + validate_ticket_description! ticket_description + original_options, altered_options = init_options_for! command: :create + self.options = altered_options - init_options_for! command: :create - - branch_name = normalize_branch_name(ticket_description, ticket) do |error| - say_error error.message - exit 1 - end + branch_name = validate_and_normalize_branch_name(ticket_description, ticket) say "Branch name: \"#{branch_name}\"", :cyan say "Branch name \"#{branch_name}\" has been copied to the clipboard!", SUCCESS if copy_to_clipboard branch_name + if original_options[:interactive] && !options[:project] + say 'Ignored: -i is only used with projects (-p).', + WARNING + end + interactive_default = options['interactive'] + options[:interactive] = interactive_default if original_options[:interactive].nil? if options[:project] - project_folder_name = project_folder_name_from(branch_name) do |error| - say_error error.message - exit 1 - end + project_folder_name = validate_and_create_project_folder_name_from! branch_name if options[:interactive] project_folder = project_folder_for branch_name unless yes? "Create project for branch \"#{branch_name}\" " \ "in folder \"#{project_folder}\" (y/n)?", :cyan say 'Aborted.', ABORTED @@ -135,18 +131,45 @@ say Branch::Name::VERSION end private + def validate_ticket_description!(ticket_description) + return unless ticket_description.blank? + + say_error 'description is required', ERROR + exit 1 + end + + def validate_and_normalize_branch_name(ticket_description, ticket) + normalize_branch_name(ticket_description, ticket) do |error| + say_error error.message + exit 1 + end + end + + def validate_and_create_project_folder_name_from!(branch_name) + project_folder_name_from(branch_name) do |error| + say_error error.message + exit 1 + end + end + def init_options_for!(command:) + options_array = [] + options_array << options.dup + say "Options before config file merge: #{options}" if options[:debug] - load_options = load_options(defaults: DEFAULT_BRANCH_NAME_OPTIONS)[command.to_s] || {} - say "No options loaded from config file(s): #{load_options}" if options[:debug] && load_options.blank? - say "Options loaded from config file(s): #{load_options}" if options[:debug] + (load_options(defaults: DEFAULT_BRANCH_NAME_OPTIONS)[command.to_s] || {}).tap do |load_options| + say "No options loaded from config file(s): #{load_options}" if options[:debug] && load_options.blank? + say "Options loaded from config file(s): #{load_options}" if options[:debug] - self.options = Thor::CoreExt::HashWithIndifferentAccess.new(load_options.merge(options)) - say "Options after config file merge: #{options}" if options[:debug] + options_array << Thor::CoreExt::HashWithIndifferentAccess.new(load_options.merge(options)) + say "Options after config file merge: #{options_array.last}" if options_array.last[:debug] + end + + options_array end end end end