git_switch 0.3.0 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: ac816f86573cb7e58720cb8fc173a5f2a219a617
4
- data.tar.gz: b629eb101ce2484cd3d66506ef321d3050ee4c99
2
+ SHA256:
3
+ metadata.gz: 7ef41c6c02e2a0139590b8455ae0f0df5730439e1ebf51bf79552854ce08f18a
4
+ data.tar.gz: 711caa5c3378da0e60ceee25d935e564efeee262c27b3cbe74b1d4ada3ba457c
5
5
  SHA512:
6
- metadata.gz: 39403b7ef421fd699f8062daf91cdacc9be0fef010d6260996ea519de8da7ae361099f25d0f6af5cdc43a452f5280d5932aae57c3b5d561d1ee581e31173f8bc
7
- data.tar.gz: bdd9422b04f7fc3ca856bc102f8f5b097217270dd0c5363a9d44bf1f5207a9e08198dd05e5a1ece83516378318b4b29ae6a17cc4418d8279232049a80d8b1cdc
6
+ metadata.gz: 7b8fab1051a79af43ac94fc40012622c9d70543e0f930f4dcef2dbf3a913423c58726e9d9d2f89399f3548540641ce0cc1d0e190f960a47b0b522de9d6c4d548
7
+ data.tar.gz: d1d04826119f0a020e1f8e03231a71c69017f31e0dcdafa4f98d98f801bbb9c7f1dbf1fb01e4d9fc70116fbe4aac89ee3010a116da0c70c11b1b2f592ffee582
@@ -0,0 +1,65 @@
1
+ require 'yaml'
2
+
3
+ module GitSwitch
4
+ class Config
5
+ attr_reader :profiles, :selected_profile
6
+ def initialize(args)
7
+ @profiles = load!
8
+ @args = args
9
+ @selected_profile = get_profile(args)
10
+ end
11
+
12
+ def get_profile(args)
13
+ args.detect {|a| !a.start_with? '-'}
14
+ end
15
+
16
+ def name
17
+ profiles[selected_profile]["name"]
18
+ end
19
+
20
+ def username
21
+ profiles[selected_profile]["username"]
22
+ end
23
+
24
+ def email
25
+ profiles[selected_profile]["email"]
26
+ end
27
+
28
+ def ssh
29
+ profiles[selected_profile]["ssh"]
30
+ end
31
+
32
+ def profile
33
+ @selected_profile
34
+ end
35
+
36
+ def valid_profile?
37
+ if profiles.has_key?(selected_profile)
38
+ return true
39
+ else
40
+ puts "Profile '#{selected_profile}' not found!"
41
+ return false
42
+ end
43
+ end
44
+
45
+ def print_list
46
+ profiles = @profiles.map do |key, value|
47
+ prefix = value["username"] == current_git_username ? "=>" : " "
48
+ "#{prefix} #{key}"
49
+ end
50
+ puts profiles
51
+ puts "\n# => - current" if @profiles.any? {|key, value| value["username"] == current_git_username}
52
+ end
53
+
54
+ private
55
+
56
+ def load!
57
+ # TODO: RCR - Handle missing or empty config file
58
+ YAML.load_file(File.expand_path('~/.gitswitch')) || {}
59
+ end
60
+
61
+ def current_git_username
62
+ `git config user.username`.chomp
63
+ end
64
+ end
65
+ end
@@ -18,12 +18,18 @@ module GitSwitch
18
18
  return false
19
19
  elsif no_flags?(args) || one_flag?(args)
20
20
  return true
21
+ elsif usage?
22
+ return true
21
23
  else
22
24
  puts "Invalid args"
23
25
  return false
24
26
  end
25
27
  end
26
28
 
29
+ def usage?
30
+ args.count == 0
31
+ end
32
+
27
33
  def list?
28
34
  (flags.include? '-l') || (flags.include? '--list')
29
35
  end
@@ -1,40 +1,55 @@
1
- require 'yaml'
2
1
  require_relative './version'
3
2
 
4
3
  module GitSwitch
5
4
  class Switcher
6
- attr_reader :config, :profile, :options
5
+ attr_reader :config, :options
7
6
 
8
7
  def initialize(args)
9
8
  raise ArgumentError unless args.is_a? Array
10
- @config = load_config
11
9
  @options = GitSwitch::Options.new(args)
12
- @profile = get_profile(args)
10
+ @config = GitSwitch::Config.new(args)
13
11
  end
14
12
 
15
13
  def run
16
- list? ? print_list : set!
14
+ if usage?
15
+ print_usage
16
+ elsif list?
17
+ print_list
18
+ else
19
+ set!
20
+ end
17
21
  end
18
22
 
19
- def list?
20
- options.list?
23
+ def profile
24
+ config.profile
21
25
  end
22
26
 
23
- def global?
24
- options.global?
27
+ def name
28
+ config.name
25
29
  end
26
30
 
27
- def get_profile(args)
28
- args.detect {|a| !a.start_with? '-'}
31
+ def username
32
+ config.username
29
33
  end
30
34
 
31
- def valid_profile?
32
- if config.has_key?(profile)
33
- return true
34
- else
35
- puts "Profile '#{profile}' not found!"
36
- return false
37
- end
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?
38
53
  end
39
54
 
40
55
  def git_repo?
@@ -46,6 +61,10 @@ module GitSwitch
46
61
  end
47
62
  end
48
63
 
64
+ def valid_profile?
65
+ config.valid_profile?
66
+ end
67
+
49
68
  def set!
50
69
  return unless options.valid_args? && valid_profile?
51
70
  return unless git_repo?
@@ -57,13 +76,12 @@ module GitSwitch
57
76
  print_settings(flag)
58
77
  end
59
78
 
79
+ def print_usage
80
+ puts usage
81
+ end
82
+
60
83
  def print_list
61
- profiles = config.map do |key, value|
62
- prefix = value["username"] == current_git_username ? "=>" : " "
63
- "#{prefix} #{key}"
64
- end
65
- puts profiles
66
- puts "\n# => - current" if config.any? {|key, value| value["username"] == current_git_username}
84
+ config.print_list
67
85
  end
68
86
 
69
87
  def print_settings(flag = '')
@@ -79,31 +97,6 @@ module GitSwitch
79
97
 
80
98
  private
81
99
 
82
- def load_config
83
- # TODO: RCR - Handle missing or empty config file
84
- YAML.load_file(File.expand_path('~/.gitswitch')) || {}
85
- end
86
-
87
- def current_git_username
88
- `git config user.username`.chomp
89
- end
90
-
91
- def name
92
- config[profile]["name"]
93
- end
94
-
95
- def username
96
- config[profile]["username"]
97
- end
98
-
99
- def email
100
- config[profile]["email"]
101
- end
102
-
103
- def ssh
104
- config[profile]["ssh"]
105
- end
106
-
107
100
  def set_git_config(flag)
108
101
  `git config #{flag} user.name "#{name}"`
109
102
  `git config #{flag} user.username "#{username}"`
@@ -114,5 +107,23 @@ module GitSwitch
114
107
  `ssh-add -D`
115
108
  `ssh-add #{ssh}`
116
109
  end
110
+
111
+ def usage
112
+ <<~USAGE
113
+ usage: git switch [-l | --list] <profile> [-v | --verbose] [-g | --global]
114
+
115
+ switch to a profile for local development only
116
+ git switch <profile>
117
+
118
+ switch to a profile globally
119
+ git switch -g <profile>
120
+
121
+ switch to a profile and see all output
122
+ git switch -v <profile>
123
+
124
+ see available profiles
125
+ git switch -l
126
+ USAGE
127
+ end
117
128
  end
118
129
  end
@@ -1,3 +1,3 @@
1
1
  module GitSwitch
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.2"
3
3
  end
data/lib/git_switch.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  require_relative 'git_switch/switcher'
2
2
  require_relative 'git_switch/git_helper'
3
3
  require_relative 'git_switch/options'
4
+ require_relative 'git_switch/config'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: git_switch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.2
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: 2018-09-20 00:00:00.000000000 Z
11
+ date: 2019-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -76,6 +76,7 @@ extra_rdoc_files: []
76
76
  files:
77
77
  - bin/git-switch
78
78
  - lib/git_switch.rb
79
+ - lib/git_switch/config.rb
79
80
  - lib/git_switch/git_helper.rb
80
81
  - lib/git_switch/options.rb
81
82
  - lib/git_switch/switcher.rb
@@ -100,7 +101,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
101
  version: '0'
101
102
  requirements: []
102
103
  rubyforge_project:
103
- rubygems_version: 2.5.2.3
104
+ rubygems_version: 2.7.7
104
105
  signing_key:
105
106
  specification_version: 4
106
107
  summary: Switch between git profiles