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 CHANGED
@@ -1,53 +1,88 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'thor'
4
- require 'gas'
3
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)) + '/../lib/')
5
4
 
6
- class GasRunner < Thor
5
+ require 'fileutils'
7
6
 
8
- default_task :list
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
- desc "list", "Lists your authors"
11
- def list
12
- Gas.list
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
- desc "show", "Shows your current user"
16
- def show
17
- Gas.show
18
- end
18
+ # Check for first structure
19
+ if File.file? oldest_config_file
20
+ contents = File.read oldest_config_file
19
21
 
20
- desc "use AUTHOR", "Uses author"
21
- def use(nickname)
22
- Gas.use nickname
23
- end
22
+ File.delete oldest_config_file
23
+ Dir::mkdir(gas_directory)
24
24
 
25
- desc "add NICKNAME NAME EMAIL", "Adds author to gasconfig"
26
- def add(nickname, name, email)
27
- Gas.add nickname, name, email
25
+ file = File.new(old_config_file, "w")
26
+ file.puts contents
27
+ file.close
28
28
  end
29
29
 
30
- desc "import NICKNAME", "Imports current user to gasconfig"
31
- def import(nickname)
32
- Gas.import nickname
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
- 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
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
- desc "version", "Prints Gas's version"
46
- def version
47
- Gas.version
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
- map %w(-v --version) => :version
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
- GasRunner.start
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
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)) + '/../lib/')
4
+ require 'gas'
5
+
6
+ Gas.check_parameters 3, 'Usage: gas add NICKNAME NAME EMAIL'
7
+ Gas.add ARGV[0], ARGV[1], ARGV[2]
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)) + '/../lib/')
4
+ require 'gas'
5
+
6
+ Gas.check_parameters 1, 'Usage: gas delete NICKNAME'
7
+ Gas.delete ARGV[0]
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)) + '/../lib/')
4
+ require 'gas'
5
+
6
+ Gas.check_parameters 1, 'Usage: gas import NICKNAME'
7
+ Gas.import ARGV[0]
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)) + '/../lib/')
4
+ require 'gas'
5
+
6
+ Gas.list
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)) + '/../lib/')
4
+ require 'gas'
5
+
6
+ Gas.show
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)) + '/../lib/')
4
+ require 'gas'
5
+
6
+ Gas.check_parameters 1, 'Usage: gas use NICKNAME'
7
+ Gas.use ARGV[0]
data/lib/gas.rb CHANGED
@@ -1,40 +1,66 @@
1
- require 'rbconfig'
1
+ require 'gas/version'
2
+ require 'gas/user'
3
+ require 'gas/users'
4
+ require 'gas/git_config'
2
5
 
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/)
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
- require 'sshkey' #external
12
+ @users = Users.new(File.join GAS_DIRECTORY, GAS_USERS_FILENAME)
10
13
 
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'
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
- module Gas
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
- @config = Config.new
24
- @gitconfig = Gitconfig.new
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 @config
57
+ puts users
32
58
  puts
33
59
  end
34
60
 
35
61
  # Shows the current user
36
62
  def self.show
37
- user = @gitconfig.current_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
- return false unless self.no_user?(nickname)
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, 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!
87
+ def self.add(nickname, name, email)
88
+ check_if_user_already_exists(nickname)
67
89
 
68
- using_ssh = Ssh.setup_ssh_keys user
69
-
70
- Ssh.upload_public_key_to_github(user, github_speaker) if using_ssh
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
- return false if self.has_user?(nickname)
116
- user = @gitconfig.current_user
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
- @config.add user
122
- @config.save!
108
+ users.add user
109
+ users.save!
123
110
 
124
- puts 'Added author'
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
- 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!
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
- # Prints the current version
147
- def self.version
148
- puts Gas::VERSION
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
@@ -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