gas 0.1.8 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/gas +70 -35
- data/bin/gas-add +7 -0
- data/bin/gas-delete +7 -0
- data/bin/gas-import +7 -0
- data/bin/gas-list +6 -0
- data/bin/gas-show +6 -0
- data/bin/gas-use +7 -0
- data/lib/gas.rb +67 -103
- data/lib/gas/git_config.rb +25 -0
- data/lib/gas/user.rb +11 -4
- data/lib/gas/users.rb +110 -0
- data/lib/gas/version.rb +2 -3
- data/spec/integration/gas_spec.rb +118 -0
- data/spec/integration/users_spec.rb +40 -0
- data/spec/spec_helper.rb +10 -141
- data/spec/unit/git_config_spec.rb +82 -0
- data/spec/unit/user_spec.rb +18 -0
- data/spec/unit/users_spec.rb +71 -0
- metadata +23 -112
- data/lib/gas/config.rb +0 -148
- data/lib/gas/gitconfig.rb +0 -46
- data/lib/gas/github_speaker.rb +0 -219
- data/lib/gas/prompter.rb +0 -169
- data/lib/gas/settings.rb +0 -28
- data/lib/gas/ssh.rb +0 -305
- data/spec/integration/ssh_spec.rb +0 -338
- data/spec/unit/config_spec.rb +0 -83
- data/spec/unit/gitconfig_spec.rb +0 -85
- data/spec/unit/github_speaker_spec.rb +0 -107
- data/spec/unit/settings_spec.rb +0 -56
data/bin/gas
CHANGED
@@ -1,53 +1,88 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
|
4
|
-
require 'gas'
|
3
|
+
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)) + '/../lib/')
|
5
4
|
|
6
|
-
|
5
|
+
require 'fileutils'
|
7
6
|
|
8
|
-
|
7
|
+
# Migrates old files to the new file structure
|
8
|
+
# @depricated in version 2.0.0
|
9
|
+
def migrate_to_new_file_structure!
|
10
|
+
gas_directory = File.expand_path '~/.gas'
|
11
|
+
old_gas_users_filename = 'gas.authors'
|
12
|
+
gas_users_filename = 'users'
|
9
13
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
end
|
14
|
+
oldest_config_file = File.expand_path '~/.gas'
|
15
|
+
old_config_file = File.join gas_directory, old_gas_users_filename
|
16
|
+
new_config_file = File.join gas_directory, gas_users_filename
|
14
17
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
end
|
18
|
+
# Check for first structure
|
19
|
+
if File.file? oldest_config_file
|
20
|
+
contents = File.read oldest_config_file
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
-
Gas.use nickname
|
23
|
-
end
|
22
|
+
File.delete oldest_config_file
|
23
|
+
Dir::mkdir(gas_directory)
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
file = File.new(old_config_file, "w")
|
26
|
+
file.puts contents
|
27
|
+
file.close
|
28
28
|
end
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
|
30
|
+
# Check for second structure
|
31
|
+
if File.file? old_config_file
|
32
|
+
FileUtils.mv old_config_file, File.join(gas_directory, gas_users_filename)
|
33
33
|
end
|
34
|
+
end
|
34
35
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
36
|
+
migrate_to_new_file_structure! # Run migrate to ensure the latest file structure is used
|
37
|
+
|
38
|
+
require 'gas'
|
39
|
+
|
40
|
+
# Cross-platform way of finding an executable in the $PATH.
|
41
|
+
def which(cmd)
|
42
|
+
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
|
43
|
+
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
|
44
|
+
exts.each { |ext|
|
45
|
+
exe = "#{path}/#{cmd}#{ext}"
|
46
|
+
return exe if File.executable? exe
|
47
|
+
}
|
43
48
|
end
|
49
|
+
return nil
|
50
|
+
end
|
44
51
|
|
45
|
-
|
46
|
-
|
47
|
-
|
52
|
+
# List all gas commands available
|
53
|
+
def plugins
|
54
|
+
plugins = []
|
55
|
+
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
|
56
|
+
Dir.glob("#{path}/gas-*").each do |cmd|
|
57
|
+
plugins << cmd.split('/').last.scan(/gas-([^-]+).*/).flatten.first
|
58
|
+
end
|
48
59
|
end
|
49
|
-
|
60
|
+
return plugins
|
61
|
+
end
|
62
|
+
|
63
|
+
arguments = ARGV
|
64
|
+
command = arguments.shift
|
65
|
+
command = 'list' if command.nil?
|
50
66
|
|
67
|
+
if command == 'version' || command == '-v' || command == '--version'
|
68
|
+
Gas.print_version
|
69
|
+
exit 0
|
70
|
+
elsif command == 'plugins'
|
71
|
+
puts plugins
|
72
|
+
exit 0
|
51
73
|
end
|
52
74
|
|
53
|
-
|
75
|
+
bin = "gas-#{command}"
|
76
|
+
|
77
|
+
if which(bin)
|
78
|
+
system "#{bin} #{arguments.join(' ')}"
|
79
|
+
|
80
|
+
plugins.each do |plugin|
|
81
|
+
plugin_bin = "gas-#{plugin}-#{command}"
|
82
|
+
if which(plugin_bin)
|
83
|
+
system "#{plugin_bin} #{arguments.join(' ')}"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
else
|
87
|
+
Gas.print_usage
|
88
|
+
end
|
data/bin/gas-add
ADDED
data/bin/gas-delete
ADDED
data/bin/gas-import
ADDED
data/bin/gas-list
ADDED
data/bin/gas-show
ADDED
data/bin/gas-use
ADDED
data/lib/gas.rb
CHANGED
@@ -1,40 +1,66 @@
|
|
1
|
-
require '
|
1
|
+
require 'gas/version'
|
2
|
+
require 'gas/user'
|
3
|
+
require 'gas/users'
|
4
|
+
require 'gas/git_config'
|
2
5
|
|
3
|
-
GAS_DIRECTORY =
|
4
|
-
|
5
|
-
|
6
|
-
IS_WINDOWS = (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/)
|
6
|
+
GAS_DIRECTORY = File.expand_path '~/.gas'
|
7
|
+
OLD_GAS_USERS_FILENAME = 'gas.authors'
|
8
|
+
GAS_USERS_FILENAME = 'users'
|
7
9
|
|
10
|
+
module Gas
|
8
11
|
|
9
|
-
|
12
|
+
@users = Users.new(File.join GAS_DIRECTORY, GAS_USERS_FILENAME)
|
10
13
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
require 'gas/config'
|
16
|
-
require 'gas/gitconfig'
|
17
|
-
require 'gas/settings'
|
18
|
-
require 'gas/github_speaker'
|
14
|
+
# Print version information to stdout
|
15
|
+
def self.print_version
|
16
|
+
puts Gas::VERSION
|
17
|
+
end
|
19
18
|
|
19
|
+
# Print usage information to stdout
|
20
|
+
def self.print_usage
|
21
|
+
puts "Usage: command [parameters]\n\nBuilt-in commands:\n add NICKNAME NAME EMAIL - adds a new user to gas\n delete NICKNAME - deletes a user from gas\n import NICKNAME - imports the user from .gitconfig into NICKNAME\n list - lists all users\n show - shows the current user\n use NICKNAME - sets the user with NICKNAME as the current user"
|
22
|
+
end
|
20
23
|
|
21
|
-
|
24
|
+
# Checks the number of parameters and exits with a message if wrong number of parameters is supplied
|
25
|
+
# @param [Integer] number_of_parameters_required
|
26
|
+
# @param [String] message
|
27
|
+
def self.check_parameters(number_of_parameters_required, message)
|
28
|
+
unless ARGV.length == number_of_parameters_required
|
29
|
+
puts message
|
30
|
+
exit
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# Checks if the user exists and gives error and exit if not
|
35
|
+
# @param [String] nickname
|
36
|
+
def self.check_if_user_does_not_exist(nickname)
|
37
|
+
if !users.exists? nickname
|
38
|
+
puts "Nickname #{nickname} does not exist"
|
39
|
+
exit
|
40
|
+
end
|
41
|
+
end
|
22
42
|
|
23
|
-
|
24
|
-
@
|
43
|
+
# Checks if the user exists and gives error and exit if so
|
44
|
+
# @param [String] nickname
|
45
|
+
def self.check_if_user_already_exists(nickname)
|
46
|
+
if users.exists? nickname
|
47
|
+
puts "Nickname #{nickname} already exists"
|
48
|
+
exit
|
49
|
+
end
|
50
|
+
end
|
25
51
|
|
26
52
|
# Lists all authors
|
27
53
|
def self.list
|
28
54
|
puts
|
29
55
|
puts 'Available users:'
|
30
56
|
puts
|
31
|
-
puts
|
57
|
+
puts users
|
32
58
|
puts
|
33
59
|
end
|
34
60
|
|
35
61
|
# Shows the current user
|
36
62
|
def self.show
|
37
|
-
user =
|
63
|
+
user = GitConfig.current_user
|
38
64
|
|
39
65
|
if user
|
40
66
|
puts 'Current user:'
|
@@ -47,11 +73,10 @@ module Gas
|
|
47
73
|
# Sets _nickname_ as current user
|
48
74
|
# @param [String] nickname The nickname to use
|
49
75
|
def self.use(nickname)
|
50
|
-
|
51
|
-
user = @config[nickname]
|
52
|
-
|
53
|
-
@gitconfig.change_user user # daring change made here! Heads up Walle
|
76
|
+
check_if_user_does_not_exist(nickname)
|
54
77
|
|
78
|
+
user = users.get nickname
|
79
|
+
GitConfig.change_user user
|
55
80
|
self.show
|
56
81
|
end
|
57
82
|
|
@@ -59,69 +84,31 @@ module Gas
|
|
59
84
|
# @param [String] nickname The nickname of the author
|
60
85
|
# @param [String] name The name of the author
|
61
86
|
# @param [String] email The email of the author
|
62
|
-
def self.add(nickname, name, email
|
63
|
-
|
64
|
-
user = User.new name, email, nickname
|
65
|
-
@config.add user
|
66
|
-
@config.save!
|
87
|
+
def self.add(nickname, name, email)
|
88
|
+
check_if_user_already_exists(nickname)
|
67
89
|
|
68
|
-
|
69
|
-
|
70
|
-
|
90
|
+
user = User.new name, email, nickname
|
91
|
+
users.add user
|
92
|
+
users.save!
|
71
93
|
|
72
94
|
puts 'Added new author'
|
73
95
|
puts user
|
74
96
|
end
|
75
97
|
|
76
|
-
|
77
|
-
# Adds an ssh key for the specified user
|
78
|
-
def self.ssh(nickname)
|
79
|
-
if nickname.nil?
|
80
|
-
puts "Oh, so you'd like an elaborate explanation on how ssh key juggling works? Well pull up a chair!"
|
81
|
-
puts
|
82
|
-
puts "Gas can juggle ssh keys for you. It works best in a unix based environment (so at least use git bash or cygwin on a windows platform)."
|
83
|
-
puts "You will be prompted if you would like to handle SSH keys when you create a new user."
|
84
|
-
puts "If you are a long time user of gas, you can add ssh to an author by the command..."
|
85
|
-
puts "\$ gas ssh NICKNAME"
|
86
|
-
puts
|
87
|
-
puts "Your ssh keys will be stored in ~/.gas/NICKNAME_id_rsa and automatically copied to ~/.ssh/id_rsa when you use the command..."
|
88
|
-
puts "\$ gas use NICKNAME"
|
89
|
-
puts "If ~/.ssh/id_rsa already exists, you will be prompted UNLESS that rsa file is already backed up in the .gas directory (I'm so sneaky, huh?)"
|
90
|
-
puts
|
91
|
-
puts "The unix command ssh-add is used in order to link up your rsa keys when you attempt to make an ssh connection (git push uses ssh keys of course)"
|
92
|
-
puts
|
93
|
-
puts "The ssh feature of gas offers you and the world ease of use, and even marginally enhanced privacy against corporate databases. Did you know that IBM built one of the first automated database systems? These ancient database machines (called tabulators) were used to facilitate the holocaust =("
|
94
|
-
else
|
95
|
-
user = @config[nickname]
|
96
|
-
|
97
|
-
|
98
|
-
# Prompt Remake this user's ssh keys?
|
99
|
-
|
100
|
-
# check for ssh keys
|
101
|
-
if !Ssh.corresponding_rsa_files_exist?(nickname)
|
102
|
-
Ssh.setup_ssh_keys user
|
103
|
-
Ssh.upload_public_key_to_github user
|
104
|
-
else
|
105
|
-
Ssh.setup_ssh_keys user
|
106
|
-
Ssh.upload_public_key_to_github user
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
|
112
98
|
# Imports current user from .gitconfig to .gas
|
113
99
|
# @param [String] nickname The nickname to give to the new user
|
114
100
|
def self.import(nickname)
|
115
|
-
|
116
|
-
|
101
|
+
check_if_user_already_exists(nickname)
|
102
|
+
|
103
|
+
user = GitConfig.current_user
|
117
104
|
|
118
105
|
if user
|
119
106
|
user = User.new user.name, user.email, nickname
|
120
107
|
|
121
|
-
|
122
|
-
|
108
|
+
users.add user
|
109
|
+
users.save!
|
123
110
|
|
124
|
-
puts '
|
111
|
+
puts 'Imported author'
|
125
112
|
puts user
|
126
113
|
else
|
127
114
|
puts 'No current user to import'
|
@@ -131,41 +118,18 @@ module Gas
|
|
131
118
|
# Deletes an author from the config using nickname
|
132
119
|
# @param [String] nickname The nickname of the author
|
133
120
|
def self.delete(nickname)
|
121
|
+
check_if_user_does_not_exist(nickname)
|
134
122
|
|
135
|
-
|
136
|
-
|
137
|
-
Ssh.delete nickname
|
138
|
-
|
139
|
-
@config.delete nickname
|
140
|
-
@config.save!
|
123
|
+
users.delete nickname
|
124
|
+
users.save!
|
141
125
|
|
142
126
|
puts "Deleted author #{nickname}"
|
143
127
|
return true
|
144
128
|
end
|
145
129
|
|
146
|
-
#
|
147
|
-
|
148
|
-
|
130
|
+
# Returns the users object so we don't use it directly
|
131
|
+
# @return [Gas::Users]
|
132
|
+
def self.users
|
133
|
+
@users
|
149
134
|
end
|
150
|
-
|
151
|
-
# Checks if the user exists and gives error and exit if not
|
152
|
-
# @param [String] nickname
|
153
|
-
def self.no_user?(nickname)
|
154
|
-
if !@config.exists? nickname
|
155
|
-
puts "Nickname #{nickname} does not exist"
|
156
|
-
return false
|
157
|
-
end
|
158
|
-
return true
|
159
|
-
end
|
160
|
-
|
161
|
-
# Checks if the user exists and gives error and exit if so
|
162
|
-
# @param [String] nickname
|
163
|
-
def self.has_user?(nickname)
|
164
|
-
if @config.exists? nickname
|
165
|
-
puts "Nickname #{nickname} already exists"
|
166
|
-
return true
|
167
|
-
end
|
168
|
-
return false
|
169
|
-
end
|
170
|
-
|
171
|
-
end
|
135
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Gas
|
2
|
+
|
3
|
+
# Module that class that interacts with the git config
|
4
|
+
module GitConfig
|
5
|
+
|
6
|
+
# Parse out the current user from the gitconfig
|
7
|
+
# @param [String] gitconfig The git configuration
|
8
|
+
# @return [Gas::User] The current user or nil if not present
|
9
|
+
def self.current_user
|
10
|
+
name = `git config --global --get user.name`
|
11
|
+
email = `git config --global --get user.email`
|
12
|
+
|
13
|
+
return nil if name.nil? && email.nil?
|
14
|
+
|
15
|
+
User.new name.delete("\n"), email.delete("\n") # git cli returns the name and email with \n at the end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Changes the user
|
19
|
+
# @param [Gas::User] user The new user
|
20
|
+
def self.change_user(user)
|
21
|
+
`git config --global user.name "#{user.name}"`
|
22
|
+
`git config --global user.email "#{user.email}"`
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/gas/user.rb
CHANGED
@@ -4,7 +4,6 @@ module Gas
|
|
4
4
|
class User
|
5
5
|
attr_reader :name, :email, :nickname
|
6
6
|
|
7
|
-
|
8
7
|
# @param [String] name The name of the user
|
9
8
|
# @param [String] email The email of the user
|
10
9
|
# @param [String] nickname A nickname for the user, not used when parsing from gitconfig
|
@@ -12,7 +11,6 @@ module Gas
|
|
12
11
|
@name = name
|
13
12
|
@email = email
|
14
13
|
@nickname = nickname
|
15
|
-
|
16
14
|
end
|
17
15
|
|
18
16
|
# Returns the git format of user
|
@@ -28,6 +26,15 @@ module Gas
|
|
28
26
|
" [#{use_nickname ? @nickname : 'user'}]\n name = #{@name}\n email = #{@email}"
|
29
27
|
end
|
30
28
|
|
31
|
-
|
29
|
+
# Define the equality operator to test if two user objects are the same
|
30
|
+
# @param [Object] the object to test against
|
31
|
+
# @return [boolean]
|
32
|
+
def ==(user)
|
33
|
+
return false unless user.is_a? User
|
34
|
+
unless nickname.empty? || user.nickname.empty? # Don't test equallity in nickname if any of the nicknames is not used
|
35
|
+
return false unless nickname == user.nickname
|
36
|
+
end
|
37
|
+
return (name == user.name && email == user.email)
|
38
|
+
end
|
32
39
|
end
|
33
|
-
end
|
40
|
+
end
|