gas2 0.1.7b

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Fredrik Wallgren
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
20
+
@@ -0,0 +1,66 @@
1
+ h1. Notice: This gem has fallen behind walle/gas and is not compatible with it
2
+
3
+ This fork of walle/gas (used for ssh key juggling) is slowly being migrated into an extension style plugin to the gas gem (for changing authors only, has nothing to do with pushing keys online or anything).
4
+
5
+
6
+ h1. gas - manage your git author accounts
7
+
8
+ "!http://travis-ci.org/walle/gas.png!":http://travis-ci.org/walle/gas
9
+
10
+ h2. Description
11
+
12
+ Gas is a mighty utility that helps you keep track of your multiple git authors. Add them to gas and switch between them instantly! Great if you use one author at work and one at home or if you are doing pair programming.
13
+
14
+ h2. Installation
15
+
16
+
17
+ bc. $ git clone git@github.com:TheNotary/gas.git
18
+ $ cd gas
19
+ $ git checkout last_stable_ssh_gas
20
+ $ bundle
21
+ $ bundle exec rake install
22
+
23
+ Alternatively you can try:
24
+
25
+ bc. $ gem install gas2
26
+
27
+
28
+ You can install from source:
29
+
30
+ bc. $ cd gas/
31
+ $ bundle
32
+ $ rake install
33
+
34
+ h2. Running
35
+
36
+ The default task is to list authors
37
+
38
+ bc. $ gas
39
+
40
+ bc. $ gas list
41
+
42
+ This lists the authors that are set up in the ~/.gas/gas.authors file.
43
+
44
+ You can import your current user by giving it a nickname
45
+
46
+ bc. $ gas import current_user
47
+
48
+ To add an author use, add
49
+
50
+ bc. $ gas add walle "Fredrik Wallgren" fredrik.wallgren@gmail.com
51
+
52
+ And the main usage, use
53
+
54
+ bc. $ gas use walle
55
+
56
+ To delete it again use, delete
57
+
58
+ bc. $ gas delete walle
59
+
60
+ Gas can also juggle your id_rsa ssh keys, which is helpful for uploading to github between multiple accounts. Indespensible for teachers who need to instruct their students on how to use github!
61
+
62
+ bc. $ gas add Njax NotarySojac no@mail.com
63
+ Do you want gas to handle switching rsa keys for this user?
64
+ [y/n]
65
+
66
+ View @gas -h@ to see all options.
data/bin/gas ADDED
@@ -0,0 +1,53 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'thor'
4
+ require 'gas'
5
+
6
+ class GasRunner < Thor
7
+
8
+ default_task :list
9
+
10
+ desc "list", "Lists your authors"
11
+ def list
12
+ Gas.list
13
+ end
14
+
15
+ desc "show", "Shows your current user"
16
+ def show
17
+ Gas.show
18
+ end
19
+
20
+ desc "use AUTHOR", "Uses author"
21
+ def use(nickname)
22
+ Gas.use nickname
23
+ end
24
+
25
+ desc "add NICKNAME NAME EMAIL", "Adds author to gasconfig"
26
+ def add(nickname, name, email)
27
+ Gas.add nickname, name, email
28
+ end
29
+
30
+ desc "import NICKNAME", "Imports current user to gasconfig"
31
+ def import(nickname)
32
+ Gas.import nickname
33
+ end
34
+
35
+ desc "delete AUTHOR", "Deletes author"
36
+ def delete(nickname)
37
+ Gas.delete nickname
38
+ end
39
+
40
+ desc "ssh", "Creates a new ssh key for an existing gas author"
41
+ def ssh(nickname=nil)
42
+ Gas.ssh nickname
43
+ end
44
+
45
+ desc "version", "Prints Gas's version"
46
+ def version
47
+ Gas.version
48
+ end
49
+ map %w(-v --version) => :version
50
+
51
+ end
52
+
53
+ GasRunner.start
@@ -0,0 +1,171 @@
1
+ require 'rbconfig'
2
+
3
+ GAS_DIRECTORY = "#{ENV['HOME']}/.gas" # File.expand_path('~/.gas')
4
+ SSH_DIRECTORY = "#{ENV['HOME']}/.ssh" # File.expand_path('~/.ssh')
5
+ GITHUB_SERVER = 'api.github.com'
6
+ IS_WINDOWS = (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/)
7
+
8
+
9
+ require 'sshkey' #external
10
+
11
+ require 'gas/version'
12
+ require 'gas/prompter'
13
+ require 'gas/ssh'
14
+ require 'gas/user'
15
+ require 'gas/config'
16
+ require 'gas/gitconfig'
17
+ require 'gas/settings'
18
+ require 'gas/github_speaker'
19
+
20
+
21
+ module Gas
22
+
23
+ @config = Config.new
24
+ @gitconfig = Gitconfig.new
25
+
26
+ # Lists all authors
27
+ def self.list
28
+ puts
29
+ puts 'Available users:'
30
+ puts
31
+ puts @config
32
+ puts
33
+ end
34
+
35
+ # Shows the current user
36
+ def self.show
37
+ user = @gitconfig.current_user
38
+
39
+ if user
40
+ puts 'Current user:'
41
+ puts "#{user.name} <#{user.email}>"
42
+ else
43
+ puts 'No current user in gitconfig'
44
+ end
45
+ end
46
+
47
+ # Sets _nickname_ as current user
48
+ # @param [String] nickname The nickname to use
49
+ def self.use(nickname)
50
+ return false unless self.no_user?(nickname)
51
+ user = @config[nickname]
52
+
53
+ @gitconfig.change_user user # daring change made here! Heads up Walle
54
+
55
+ self.show
56
+ end
57
+
58
+ # Adds a author to the config
59
+ # @param [String] nickname The nickname of the author
60
+ # @param [String] name The name of the author
61
+ # @param [String] email The email of the author
62
+ def self.add(nickname, name, email, github_speaker = nil)
63
+ return false if self.has_user?(nickname)
64
+ user = User.new name, email, nickname
65
+ @config.add user
66
+ @config.save!
67
+
68
+ using_ssh = Ssh.setup_ssh_keys user
69
+
70
+ Ssh.upload_public_key_to_github(user, github_speaker) if using_ssh
71
+
72
+ puts 'Added new author'
73
+ puts user
74
+ end
75
+
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
+ # Imports current user from .gitconfig to .gas
113
+ # @param [String] nickname The nickname to give to the new user
114
+ def self.import(nickname)
115
+ return false if self.has_user?(nickname)
116
+ user = @gitconfig.current_user
117
+
118
+ if user
119
+ user = User.new user.name, user.email, nickname
120
+
121
+ @config.add user
122
+ @config.save!
123
+
124
+ puts 'Added author'
125
+ puts user
126
+ else
127
+ puts 'No current user to import'
128
+ end
129
+ end
130
+
131
+ # Deletes an author from the config using nickname
132
+ # @param [String] nickname The nickname of the author
133
+ def self.delete(nickname)
134
+
135
+ return false unless self.no_user? nickname # I re-engineered this section so I could use Gas.delete in a test even when that author didn't exist
136
+ # TODO: The name no_user? is now very confusing. It should be changed to something like "user_exists?" now maybe?
137
+ Ssh.delete nickname
138
+
139
+ @config.delete nickname
140
+ @config.save!
141
+
142
+ puts "Deleted author #{nickname}"
143
+ return true
144
+ end
145
+
146
+ # Prints the current version
147
+ def self.version
148
+ puts Gas::VERSION
149
+ 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
@@ -0,0 +1,148 @@
1
+ require 'fileutils'
2
+
3
+ module Gas
4
+
5
+ # Class that keeps track of users
6
+ class Config
7
+ attr_reader :users
8
+
9
+ # This function checks for a ~/.gas FILE and if it exists, it puts it into memory and deletes it from the HDD
10
+ # then it creates the ~/.gas FOLDER and saves the old .gas file as ~/git.conf
11
+ #
12
+ def migrate_to_gas_dir!
13
+ old_config_file = GAS_DIRECTORY
14
+ config_dir = GAS_DIRECTORY
15
+ new_config_file = GAS_DIRECTORY + "/gas.authors"
16
+
17
+ if File.file? old_config_file
18
+ file = File.open(old_config_file, "rb")
19
+ contents = file.read
20
+ file.close
21
+
22
+ File.delete old_config_file
23
+
24
+ Dir::mkdir(config_dir)
25
+
26
+ file = File.new(new_config_file, "w")
27
+ file.puts contents
28
+ file.close
29
+ end
30
+ end
31
+
32
+
33
+ # Initializes the object. If no users are supplied we look for a config file, if none then create it, and parse it to load users
34
+ # @param [Array<User>] users The override users
35
+ # @param [String] config The override config
36
+ def initialize(users = nil, config = nil)
37
+ migrate_to_gas_dir! # Migrates old users to the new configuration file location, how thoughtful of me, I know
38
+ @config_file = "#{GAS_DIRECTORY}/gas.authors"
39
+ @gas_dir = GAS_DIRECTORY
40
+ @config = ''
41
+
42
+ if config.nil?
43
+ if !File.exists? @config_file
44
+ Dir::mkdir(@gas_dir) unless File.exists? @gas_dir
45
+ FileUtils.touch @config_file
46
+ end
47
+
48
+ @config = File.read(@config_file)
49
+ else
50
+ @config = config
51
+ end
52
+
53
+ if users.nil?
54
+ @users = []
55
+ @config.scan(/\[(.+)\]\s+name = (.+)\s+email = (.+)/) do |nickname, name, email|
56
+ @users << User.new(name, email, nickname)
57
+ end
58
+ else
59
+ @users = users
60
+ end
61
+ end
62
+
63
+ # Checks if a user with _nickname_ exists
64
+ # @param [String] nickname
65
+ # @return [Boolean]
66
+ def exists?(nickname)
67
+ @users.each do |user|
68
+ if user.nickname == nickname
69
+ return true;
70
+ end
71
+ end
72
+
73
+ false
74
+ end
75
+
76
+ # Returns the user with nickname nil if no such user exists
77
+ # @param [String|Symbol] nickname
78
+ # @return [User|nil]
79
+ def get(nickname)
80
+ @users.each do |user|
81
+ if user.nickname == nickname.to_s
82
+ return user
83
+ end
84
+ end
85
+
86
+ nil
87
+ end
88
+
89
+ # Override [] to get hash style acces to users
90
+ # @param [String|Symbol] nickname
91
+ # @return [User|nil]
92
+ def [](nickname)
93
+ get nickname
94
+ end
95
+
96
+ # Adds a user
97
+ # @param [User]
98
+ def add(user)
99
+ @users << user
100
+ end
101
+
102
+ # Deletes a user by nickname
103
+ # @param [String] nickname The nickname of the user to delete
104
+ def delete(nickname)
105
+ @users.delete_if do |user|
106
+ user.nickname == nickname
107
+ end
108
+ end
109
+
110
+ # Saves the current users to the config file
111
+ def save!
112
+ File.open @config_file, 'w' do |file|
113
+ file.write self
114
+ end
115
+ end
116
+
117
+
118
+ # Override to_s to output correct format
119
+ def to_s
120
+ gc = Gitconfig.new
121
+
122
+ users = @users.map do |user|
123
+ if is_current_user(gc.current_user_object[:name], gc.current_user_object[:email], user.to_s)
124
+ " ==>" + user.to_s[5,user.to_s.length]
125
+ else
126
+ user.to_s
127
+ end
128
+ end.join("\n")
129
+
130
+ return users
131
+ end
132
+
133
+
134
+ # Scans the @users (a string containing info formatted identical to the gas.author file)
135
+ # ...and checks to see if it's name and email match what you're looking for
136
+ def is_current_user(name, email, object)
137
+ object.scan(/\[(.+)\]\s+name = (.+)\s+email = (.+)/) do |nicknamec, namec, emailc|
138
+ if namec == name and emailc == email
139
+ # check if ssh is active
140
+ # TODO: Check if its SSH key is setup, and indicate SSH ACTIVE
141
+ return true
142
+ end
143
+ end
144
+ return false # could not get a current user's nickname
145
+ end
146
+
147
+ end
148
+ end