Skip to content
This repository has been archived by the owner on Sep 13, 2021. It is now read-only.

Commit

Permalink
Merge pull request #49 from randallreedjr/feature/initial-config-v2
Browse files Browse the repository at this point in the history
Add guided setup for initial config
  • Loading branch information
randallreedjr committed Jan 22, 2019
2 parents 72c4d64 + 4d2a367 commit 1eadf36
Show file tree
Hide file tree
Showing 12 changed files with 355 additions and 114 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Expand Up @@ -29,7 +29,7 @@ jobs:
- run:
name: install dependencies
command: |
gem install bundler
gem install bundler -v '~> 1.17'
bundle install --jobs=4 --retry=3 --path vendor/bundle
# - save_cache:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -11,3 +11,4 @@
.rspec_status

*.gem
Gemfile.lock
38 changes: 0 additions & 38 deletions Gemfile.lock

This file was deleted.

6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -21,7 +21,9 @@ $ rvm @global do gem install git_switch

## Configuration

Although a guided setup is planned, for now you must manually create your configuration at `~/.gitswitch`. The config file is in YAML format. Here is an example.
To run the guided setup, use `git switch --config`.

You may also manually create or edit your configuration at `~/.gitswitch`. The config file is in YAML format. Here is an example.

```
personal:
Expand All @@ -42,7 +44,7 @@ The root keys can be any nickname you want. It should be memorable to make it ea

## Usage

Git Switch follows the convention to create a custom git command. It can be invoked as follows, to either set your git profile locally or globally:
Git Switch follows the convention to create a custom git command. It can be invoked as follows, to either set your git profile locally (no flag) or globally (`-g`):

```
$ git switch personal
Expand Down
5 changes: 4 additions & 1 deletion git_switch.gemspec
Expand Up @@ -6,6 +6,7 @@ require "git_switch/version"
Gem::Specification.new do |spec|
spec.name = "git_switch"
spec.version = GitSwitch::VERSION
spec.platform = Gem::Platform::RUBY
spec.authors = ["Randall Reed, Jr."]
spec.email = ["randallreedjr@gmail.com"]

Expand All @@ -27,7 +28,9 @@ Gem::Specification.new do |spec|
spec.executables << 'git-switch'
spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.16"
spec.add_runtime_dependency "activesupport", "~> 5.2"

spec.add_development_dependency "bundler", "~> 1.17"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "rspec_junit_formatter", "~> 0.4.1"
Expand Down
57 changes: 57 additions & 0 deletions lib/git_switch/config.rb
Expand Up @@ -42,6 +42,63 @@ def valid_profile?
end
end

def configure!
@profiles = build_profiles
write_profiles_to_config_file if @profiles.any?
end

def build_profiles
puts "How many profiles would you like to create?"
profile_count = STDIN.gets.chomp.to_i
profiles = Array.new(profile_count, {})
current = 1
profiles.map do |profile|
puts "\n#{ordinal(current)} profile name (e.g. 'work' or 'personal'):"
profile[:profile_name] = STDIN.gets.chomp
puts "Git username for #{profile[:profile_name]}:"
profile[:git_username] = STDIN.gets.chomp
puts "Git email for #{profile[:profile_name]}:"
profile[:git_email] = STDIN.gets.chomp
puts "Git name for #{profile[:profile_name]}:"
profile[:git_name] = STDIN.gets.chomp
puts "Path to ssh key for #{profile[:profile_name]} (e.g. '~/.ssh/id_rsa'):"
profile[:ssh_key] = STDIN.gets.chomp

current +=1
profile.dup
end
rescue Interrupt
return {}
end

def ordinal(number)
# Source: https://github.com/infertux/ordinalize_full
abs_number = number.abs
suffix = if (11..13).cover?(abs_number % 100)
"th"
else
case abs_number % 10
when 1 then "st"
when 2 then "nd"
when 3 then "rd"
else "th"
end
end
"#{abs_number}#{suffix}"
end

def write_profiles_to_config_file
File.open(File.expand_path('~/.gitswitch'), 'w') do |file|
profiles.each do |profile|
file.write "#{profile[:profile_name]}:\n"
file.write " username: #{profile[:git_username]}\n"
file.write " email: #{profile[:git_email]}\n"
file.write " name: #{profile[:git_name]}\n"
file.write " ssh: #{profile[:ssh_key]}\n"
end
end
end

def print_list
profiles = @profiles.map do |key, value|
prefix = value["username"] == current_git_username ? "=>" : " "
Expand Down
37 changes: 28 additions & 9 deletions lib/git_switch/options.rb
Expand Up @@ -7,16 +7,19 @@ def initialize(args)

def flags
@flags ||= args.select do |arg|
arg.match(/\A\-[glv]{1}\z/) ||
arg.match(/\A\-\-(global|list|verbose){1}\z/)
arg.match(/\A\-[cglv]{1}\z/) ||
arg.match(/\A\-\-(config|global|list|verbose){1}\z/)
end
end

def valid_args?
if list? && args.count > 1
puts "Invalid args"
return false
elsif no_flags?(args) || one_flag?(args)
if config_flag? && args.count == 1
return true
elsif list_flag? && args.count == 1
return true
elsif no_flags?
return true
elsif one_flag? && !flag_only?
return true
elsif usage?
return true
Expand All @@ -30,8 +33,12 @@ def usage?
args.count == 0
end

def config?
config_flag? && args.count == 1
end

def list?
(flags.include? '-l') || (flags.include? '--list')
list_flag? && args.count == 1
end

def global?
Expand All @@ -44,14 +51,26 @@ def verbose?

private

def no_flags?(args)
def list_flag?
(flags.include? '-l') || (flags.include? '--list')
end

def config_flag?
(flags.include? '-c') || (flags.include? '--config')
end

def no_flags?
args.length == 1 && flag_count(args) == 0
end

def one_flag?(args)
def one_flag?
args.length == 2 && flag_count(args) == 1
end

def flag_only?
list_flag? || config_flag?
end

def flag_count(args)
args.count {|a| a.start_with? '-'}
end
Expand Down
54 changes: 11 additions & 43 deletions lib/git_switch/switcher.rb
@@ -1,8 +1,11 @@
require_relative './version'
require 'active_support/core_ext/module/delegation'

module GitSwitch
class Switcher
attr_reader :config, :options
delegate :usage?, :config?, :list?, :global?, to: :options
delegate :profile, :name, :username, :email, :ssh, :print_list, :configure!, :valid_profile?, to: :config

def initialize(args)
raise ArgumentError unless args.is_a? Array
Expand All @@ -11,47 +14,18 @@ def initialize(args)
end

def run
return unless options.valid_args?
if usage?
print_usage
elsif config?
configure!
elsif list?
print_list
else
set!
end
end

def profile
config.profile
end

def name
config.name
end

def username
config.username
end

def email
config.email
end

def ssh
config.ssh
end

def usage?
options.usage?
end

def list?
options.list?
end

def global?
options.global?
end

def git_repo?
if GitHelper.git_repo? || global?
return true
Expand All @@ -61,13 +35,8 @@ def git_repo?
end
end

def valid_profile?
config.valid_profile?
end

def set!
return unless options.valid_args? && valid_profile?
return unless git_repo?
return unless valid_profile? && git_repo?

flag = global? ? '--global' : ''

Expand All @@ -80,10 +49,6 @@ def print_usage
puts usage
end

def print_list
config.print_list
end

def print_settings(flag = '')
if options.verbose?
puts "\nGit Config:"
Expand All @@ -110,9 +75,12 @@ def set_ssh

def usage
<<~USAGE
usage: git switch [-l | --list]
usage: git switch [-l | --list] [-c | --config]
<profile> [-v | --verbose] [-g | --global]
configure profiles
git switch -c
switch to a profile for local development only
git switch <profile>
Expand Down
2 changes: 1 addition & 1 deletion lib/git_switch/version.rb
@@ -1,3 +1,3 @@
module GitSwitch
VERSION = "0.3.2"
VERSION = "0.4.0"
end

0 comments on commit 1eadf36

Please sign in to comment.