git_switch 0.3.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7ef41c6c02e2a0139590b8455ae0f0df5730439e1ebf51bf79552854ce08f18a
4
- data.tar.gz: 711caa5c3378da0e60ceee25d935e564efeee262c27b3cbe74b1d4ada3ba457c
3
+ metadata.gz: 4682e1a21cd0fcf56ba298bc9de39312bc7de40fd2cc7060853c9f452a934b79
4
+ data.tar.gz: 378010b8b024f582759db350d4d6eeb21c15cc8ee7fe3a4807985bb37e335c55
5
5
  SHA512:
6
- metadata.gz: 7b8fab1051a79af43ac94fc40012622c9d70543e0f930f4dcef2dbf3a913423c58726e9d9d2f89399f3548540641ce0cc1d0e190f960a47b0b522de9d6c4d548
7
- data.tar.gz: d1d04826119f0a020e1f8e03231a71c69017f31e0dcdafa4f98d98f801bbb9c7f1dbf1fb01e4d9fc70116fbe4aac89ee3010a116da0c70c11b1b2f592ffee582
6
+ metadata.gz: f825028bb0d5296d3a260b5a6dd142e3c457debf9aee12ddd01d2f069007a85940b464e0366fde6cc1761e81230ab2daa2802ad3e47bb2e7b33ff382d92827c3
7
+ data.tar.gz: 2a0a4755354d88147f41459bae0aeef877a7a145300e77b49d830b972253caa9240776c69d0a85ee48430487ea3516ab78419c330c76cf4bb7e8e80de43da424
@@ -42,6 +42,63 @@ module GitSwitch
42
42
  end
43
43
  end
44
44
 
45
+ def configure!
46
+ @profiles = build_profiles
47
+ write_profiles_to_config_file if @profiles.any?
48
+ end
49
+
50
+ def build_profiles
51
+ puts "How many profiles would you like to create?"
52
+ profile_count = STDIN.gets.chomp.to_i
53
+ profiles = Array.new(profile_count, {})
54
+ current = 1
55
+ profiles.map do |profile|
56
+ puts "\n#{ordinal(current)} profile name (e.g. 'work' or 'personal'):"
57
+ profile[:profile_name] = STDIN.gets.chomp
58
+ puts "Git username for #{profile[:profile_name]}:"
59
+ profile[:git_username] = STDIN.gets.chomp
60
+ puts "Git email for #{profile[:profile_name]}:"
61
+ profile[:git_email] = STDIN.gets.chomp
62
+ puts "Git name for #{profile[:profile_name]}:"
63
+ profile[:git_name] = STDIN.gets.chomp
64
+ puts "Path to ssh key for #{profile[:profile_name]} (e.g. '~/.ssh/id_rsa'):"
65
+ profile[:ssh_key] = STDIN.gets.chomp
66
+
67
+ current +=1
68
+ profile.dup
69
+ end
70
+ rescue Interrupt
71
+ return {}
72
+ end
73
+
74
+ def ordinal(number)
75
+ # Source: https://github.com/infertux/ordinalize_full
76
+ abs_number = number.abs
77
+ suffix = if (11..13).cover?(abs_number % 100)
78
+ "th"
79
+ else
80
+ case abs_number % 10
81
+ when 1 then "st"
82
+ when 2 then "nd"
83
+ when 3 then "rd"
84
+ else "th"
85
+ end
86
+ end
87
+ "#{abs_number}#{suffix}"
88
+ end
89
+
90
+ def write_profiles_to_config_file
91
+ File.open(File.expand_path('~/.gitswitch'), 'w') do |file|
92
+ profiles.each do |profile|
93
+ file.write "#{profile[:profile_name]}:\n"
94
+ file.write " username: #{profile[:git_username]}\n"
95
+ file.write " email: #{profile[:git_email]}\n"
96
+ file.write " name: #{profile[:git_name]}\n"
97
+ file.write " ssh: #{profile[:ssh_key]}\n"
98
+ end
99
+ end
100
+ end
101
+
45
102
  def print_list
46
103
  profiles = @profiles.map do |key, value|
47
104
  prefix = value["username"] == current_git_username ? "=>" : " "
@@ -7,16 +7,19 @@ module GitSwitch
7
7
 
8
8
  def flags
9
9
  @flags ||= args.select do |arg|
10
- arg.match(/\A\-[glv]{1}\z/) ||
11
- arg.match(/\A\-\-(global|list|verbose){1}\z/)
10
+ arg.match(/\A\-[cglv]{1}\z/) ||
11
+ arg.match(/\A\-\-(config|global|list|verbose){1}\z/)
12
12
  end
13
13
  end
14
14
 
15
15
  def valid_args?
16
- if list? && args.count > 1
17
- puts "Invalid args"
18
- return false
19
- elsif no_flags?(args) || one_flag?(args)
16
+ if config_flag? && args.count == 1
17
+ return true
18
+ elsif list_flag? && args.count == 1
19
+ return true
20
+ elsif no_flags?
21
+ return true
22
+ elsif one_flag? && !flag_only?
20
23
  return true
21
24
  elsif usage?
22
25
  return true
@@ -30,8 +33,12 @@ module GitSwitch
30
33
  args.count == 0
31
34
  end
32
35
 
36
+ def config?
37
+ config_flag? && args.count == 1
38
+ end
39
+
33
40
  def list?
34
- (flags.include? '-l') || (flags.include? '--list')
41
+ list_flag? && args.count == 1
35
42
  end
36
43
 
37
44
  def global?
@@ -44,14 +51,26 @@ module GitSwitch
44
51
 
45
52
  private
46
53
 
47
- def no_flags?(args)
54
+ def list_flag?
55
+ (flags.include? '-l') || (flags.include? '--list')
56
+ end
57
+
58
+ def config_flag?
59
+ (flags.include? '-c') || (flags.include? '--config')
60
+ end
61
+
62
+ def no_flags?
48
63
  args.length == 1 && flag_count(args) == 0
49
64
  end
50
65
 
51
- def one_flag?(args)
66
+ def one_flag?
52
67
  args.length == 2 && flag_count(args) == 1
53
68
  end
54
69
 
70
+ def flag_only?
71
+ list_flag? || config_flag?
72
+ end
73
+
55
74
  def flag_count(args)
56
75
  args.count {|a| a.start_with? '-'}
57
76
  end
@@ -1,8 +1,11 @@
1
1
  require_relative './version'
2
+ require 'active_support/core_ext/module/delegation'
2
3
 
3
4
  module GitSwitch
4
5
  class Switcher
5
6
  attr_reader :config, :options
7
+ delegate :usage?, :config?, :list?, :global?, to: :options
8
+ delegate :profile, :name, :username, :email, :ssh, :print_list, :configure!, :valid_profile?, to: :config
6
9
 
7
10
  def initialize(args)
8
11
  raise ArgumentError unless args.is_a? Array
@@ -11,8 +14,11 @@ module GitSwitch
11
14
  end
12
15
 
13
16
  def run
17
+ return unless options.valid_args?
14
18
  if usage?
15
19
  print_usage
20
+ elsif config?
21
+ configure!
16
22
  elsif list?
17
23
  print_list
18
24
  else
@@ -20,38 +26,6 @@ module GitSwitch
20
26
  end
21
27
  end
22
28
 
23
- def profile
24
- config.profile
25
- end
26
-
27
- def name
28
- config.name
29
- end
30
-
31
- def username
32
- config.username
33
- end
34
-
35
- def email
36
- config.email
37
- end
38
-
39
- def ssh
40
- config.ssh
41
- end
42
-
43
- def usage?
44
- options.usage?
45
- end
46
-
47
- def list?
48
- options.list?
49
- end
50
-
51
- def global?
52
- options.global?
53
- end
54
-
55
29
  def git_repo?
56
30
  if GitHelper.git_repo? || global?
57
31
  return true
@@ -61,13 +35,8 @@ module GitSwitch
61
35
  end
62
36
  end
63
37
 
64
- def valid_profile?
65
- config.valid_profile?
66
- end
67
-
68
38
  def set!
69
- return unless options.valid_args? && valid_profile?
70
- return unless git_repo?
39
+ return unless valid_profile? && git_repo?
71
40
 
72
41
  flag = global? ? '--global' : ''
73
42
 
@@ -80,10 +49,6 @@ module GitSwitch
80
49
  puts usage
81
50
  end
82
51
 
83
- def print_list
84
- config.print_list
85
- end
86
-
87
52
  def print_settings(flag = '')
88
53
  if options.verbose?
89
54
  puts "\nGit Config:"
@@ -110,19 +75,23 @@ module GitSwitch
110
75
 
111
76
  def usage
112
77
  <<~USAGE
113
- usage: git switch [-l | --list] <profile> [-v | --verbose] [-g | --global]
78
+ usage: git switch [-l | --list] [-c | --config]
79
+ <profile> [-v | --verbose] [-g | --global]
80
+
81
+ configure profiles
82
+ git switch -c
114
83
 
115
84
  switch to a profile for local development only
116
- git switch <profile>
85
+ git switch <profile>
117
86
 
118
87
  switch to a profile globally
119
- git switch -g <profile>
88
+ git switch -g <profile>
120
89
 
121
90
  switch to a profile and see all output
122
- git switch -v <profile>
91
+ git switch -v <profile>
123
92
 
124
93
  see available profiles
125
- git switch -l
94
+ git switch -l
126
95
  USAGE
127
96
  end
128
97
  end
@@ -1,3 +1,3 @@
1
1
  module GitSwitch
2
- VERSION = "0.3.2"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,29 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: git_switch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Randall Reed, Jr.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-15 00:00:00.000000000 Z
11
+ date: 2019-01-22 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.2'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
31
  - - "~>"
18
32
  - !ruby/object:Gem::Version
19
- version: '1.16'
33
+ version: '1.17'
20
34
  type: :development
21
35
  prerelease: false
22
36
  version_requirements: !ruby/object:Gem::Requirement
23
37
  requirements:
24
38
  - - "~>"
25
39
  - !ruby/object:Gem::Version
26
- version: '1.16'
40
+ version: '1.17'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rake
29
43
  requirement: !ruby/object:Gem::Requirement