gas 0.1.0

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
+
data/README.textile ADDED
@@ -0,0 +1,43 @@
1
+ h1. gas - manage your git author accounts
2
+
3
+ "!http://travis-ci.org/walle/gas.png!":http://travis-ci.org/walle/gas
4
+
5
+ h2. Description
6
+
7
+ Gas is a utility to keep track of your git authors. Add them to gas and switch at any time. Great if you use one author at work and one at home for instance.
8
+
9
+ h2. Installation
10
+
11
+ The best way to install gas is with RubyGems:
12
+
13
+ bc. $ [sudo] gem install gas
14
+
15
+ You can install from source:
16
+
17
+ bc. $ cd gas/
18
+ $ bundle
19
+ $ rake install
20
+
21
+ h2. Running
22
+
23
+ Default task is to list authors
24
+
25
+ bc. $ gas
26
+
27
+ bc. $ gas list
28
+
29
+ This lists the authors that are set up.
30
+
31
+ To add a author use, add
32
+
33
+ bc. $ gas add walle "Fredrik Wallgren" fredrik.wallgren@gmail.com
34
+
35
+ And the main usage, use
36
+
37
+ bc. $ gas use walle
38
+
39
+ To delete it again use, delete
40
+
41
+ bc. $ gas delete walle
42
+
43
+ View @gas -h@ to see all options.
data/bin/gas ADDED
@@ -0,0 +1,43 @@
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 "delete AUTHOR", "Deletes author"
31
+ def delete(nickname)
32
+ Gas.delete nickname
33
+ end
34
+
35
+ desc "version", "Prints Gas's version"
36
+ def version
37
+ Gas.version
38
+ end
39
+ map %w(-v --version) => :version
40
+
41
+ end
42
+
43
+ GasRunner.start
data/lib/gas.rb ADDED
@@ -0,0 +1,110 @@
1
+ require 'fileutils'
2
+
3
+ require 'gas/version'
4
+ require 'gas/user'
5
+ require 'gas/configuration'
6
+
7
+ # TODO: Refactor this file
8
+
9
+ module Gas
10
+
11
+ @config = File.expand_path('~/.gas')
12
+ @gitconfig = File.expand_path('~/.gitconfig')
13
+
14
+ # Lists all authors
15
+ def self.list
16
+ if !File.exists? @config
17
+ FileUtils.touch @config
18
+ end
19
+
20
+ config = File.read(@config)
21
+ configuration = Configuration.parse config
22
+ puts 'Available users:'
23
+ puts configuration
24
+
25
+ self.show
26
+ end
27
+
28
+ # Shows the current user
29
+ def self.show
30
+ gitconfig = File.read(@gitconfig)
31
+ user = Gas::Configuration.current_user gitconfig
32
+ puts 'Current user:'
33
+ puts "#{user.name} <#{user.email}>"
34
+ end
35
+
36
+ # Sets _nickname_ as current user
37
+ # @param [String] nickname The nickname to use
38
+ def self.use(nickname)
39
+ if !File.exists? @config
40
+ FileUtils.touch @config
41
+ end
42
+
43
+ config = File.read(@config)
44
+ configuration = Configuration.parse config
45
+
46
+ if !configuration.exists? nickname
47
+ puts "Nickname #{nickname} does not exist"
48
+ return
49
+ end
50
+
51
+ user = configuration[nickname]
52
+
53
+ gitconfig = File.read(@gitconfig)
54
+ gitconfig.gsub! /name\s?=\s?.+/, "name = #{user.name}"
55
+ gitconfig.gsub! /email\s?=\s?.+/, "email = #{user.email}"
56
+ File.open @gitconfig, 'w' do |file|
57
+ file.write gitconfig
58
+ end
59
+
60
+ self.show
61
+ end
62
+
63
+ # Adds a author to the config
64
+ # @param [String] nickname The nickname of the author
65
+ # @param [String] name The name of the author
66
+ # @param [String] email The email of the author
67
+ def self.add(nickname, name, email)
68
+ config = File.read(@config)
69
+ configuration = Configuration.parse config
70
+
71
+ if configuration.exists? nickname
72
+ puts "Nickname #{nickname} does already exist"
73
+ return
74
+ end
75
+
76
+ user = User.new name, email, nickname
77
+ configuration.add user
78
+ File.open @config, 'w' do |file|
79
+ file.write configuration
80
+ end
81
+
82
+ puts 'Added author'
83
+ puts user
84
+ end
85
+
86
+ # Deletes a author from the config using nickname
87
+ # @param [String] nickname The nickname of the author
88
+ def self.delete(nickname)
89
+ config = File.read(@config)
90
+ configuration = Configuration.parse config
91
+
92
+ if !configuration.exists? nickname
93
+ puts "Nickname #{nickname} does not exist"
94
+ return
95
+ end
96
+
97
+ configuration.delete nickname
98
+ File.open @config, 'w' do |file|
99
+ file.write configuration
100
+ end
101
+
102
+ puts "Deleted author #{nickname}"
103
+ end
104
+
105
+ # Prints the current version
106
+ def self.version
107
+ puts Gas::Version
108
+ end
109
+
110
+ end
@@ -0,0 +1,84 @@
1
+ module Gas
2
+
3
+ # Class that keeps track of users
4
+ class Configuration
5
+ attr_reader :users
6
+
7
+ # Parses [User]s from a string of the config file
8
+ # @param [String] config The configuration file
9
+ # @return [Configuration]
10
+ def self.parse(config)
11
+ users = []
12
+ config.scan(/\[(.+)\]\s+name = (.+)\s+email = (.+)/) do |nickname, name, email|
13
+ users << User.new(name, email, nickname)
14
+ end
15
+
16
+ Configuration.new users
17
+ end
18
+
19
+ # Can parse out the current user from the gitconfig
20
+ # @param [String] gitconfig The git configuration
21
+ # @return [User] The current user
22
+ def self.current_user(gitconfig)
23
+ User.parse gitconfig
24
+ end
25
+
26
+ # Checks if a user with _nickname_ exists
27
+ # @param [String] nickname
28
+ # @return [Boolean]
29
+ def exists?(nickname)
30
+ @users.each do |user|
31
+ if user.nickname == nickname
32
+ return true;
33
+ end
34
+ end
35
+
36
+ false
37
+ end
38
+
39
+ # Returns the user with nickname nil if no such user exists
40
+ # @param [String|Symbol] nickname
41
+ # @return [User|nil]
42
+ def get(nickname)
43
+ @users.each do |user|
44
+ if user.nickname == nickname.to_s
45
+ return user
46
+ end
47
+ end
48
+
49
+ nil
50
+ end
51
+
52
+ # Override [] to get hash style acces to users
53
+ # @param [String|Symbol] nickname
54
+ # @return [User|nil]
55
+ def [](nickname)
56
+ get nickname
57
+ end
58
+
59
+ # Adds a user
60
+ # @param [User]
61
+ def add(user)
62
+ @users << user
63
+ end
64
+
65
+ # Deletes a user by nickname
66
+ # @param [String] nickname The nickname of the user to delete
67
+ def delete(nickname)
68
+ @users.delete_if do |user|
69
+ user.nickname == nickname
70
+ end
71
+ end
72
+
73
+ # @param [Array<User>] users
74
+ def initialize(users)
75
+ @users = users
76
+ end
77
+
78
+ # Override to_s to output correct format
79
+ def to_s
80
+ @users.join("\n")
81
+ end
82
+
83
+ end
84
+ end
data/lib/gas/user.rb ADDED
@@ -0,0 +1,40 @@
1
+ module Gas
2
+
3
+ # Class that contains data for a git user
4
+ class User
5
+ attr_reader :name, :email, :nickname
6
+
7
+ # Static function to parse out the data from a git config string
8
+ # @param [String] gitconfig
9
+ # @return [User] a new user
10
+ def self.parse(gitconfig)
11
+ regex = /\[user\]\s+name = (.+)\s+email = (.+)/
12
+ matches = regex.match gitconfig
13
+
14
+ User.new matches[1], matches[2]
15
+ end
16
+
17
+ # @param [String] name The name of the user
18
+ # @param [String] email The email of the user
19
+ # @param [String] nickname A nickname for the user, not used when parsing from gitconfig
20
+ def initialize(name, email, nickname = '')
21
+ @name = name
22
+ @email = email
23
+ @nickname = nickname
24
+ end
25
+
26
+ # Returns the git format of user
27
+ # @return [String]
28
+ def git_user
29
+ to_s false
30
+ end
31
+
32
+ # Overides to_s to output in the correct format
33
+ # @param [Boolean] use_nickname Defaults to true
34
+ # @return [String]
35
+ def to_s(use_nickname = true)
36
+ "[#{use_nickname ? @nickname : 'user'}]\n name = #{@name}\n email = #{@email}"
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,6 @@
1
+ module Gas
2
+
3
+ Version = '0.1.0'
4
+
5
+ end
6
+
@@ -0,0 +1,80 @@
1
+ require './spec/spec_helper'
2
+
3
+ require './lib/gas'
4
+
5
+ describe Gas::Configuration do
6
+ it 'should be able to parse users from config format' do
7
+ name = 'Fredrik Wallgren'
8
+ email = 'fredrik.wallgren@gmail.com'
9
+ nickname = 'walle'
10
+ config = "[#{nickname}]\n name = #{name}\n email = #{email}\n\n[user2]\n name = foo\n email = bar"
11
+ config = Gas::Configuration.parse config
12
+ config.users.count.should == 2
13
+ config.users[0].name.should == name
14
+ config.users[0].email.should == email
15
+ config.users[0].nickname.should == nickname
16
+ end
17
+
18
+ it 'should be able to get current user from gitconfig' do
19
+ name = 'Fredrik Wallgren'
20
+ email = 'fredrik.wallgren@gmail.com'
21
+ gitconfig = "[other stuff]\n foo = bar\n\n[user]\n name = #{name}\n email = #{email}\n\n[foo]\n bar = foo"
22
+ user = Gas::Configuration.current_user gitconfig
23
+ user.name.should == name
24
+ user.email.should == email
25
+ end
26
+
27
+ it 'should output the users in the correct format' do
28
+ user1 = Gas::User.new 'Fredrik Wallgren', 'fredrik.wallgren@gmail.com', 'walle'
29
+ user2 = Gas::User.new 'foo', 'bar', 'user2'
30
+ users = [user1, user2]
31
+ config = Gas::Configuration.new users
32
+ config.to_s.should == "[walle]\n name = Fredrik Wallgren\n email = fredrik.wallgren@gmail.com\n[user2]\n name = foo\n email = bar"
33
+ end
34
+
35
+ it 'should be able to tell if a nickname exists' do
36
+ user1 = Gas::User.new 'Fredrik Wallgren', 'fredrik.wallgren@gmail.com', 'walle'
37
+ user2 = Gas::User.new 'foo', 'bar', 'user2'
38
+ users = [user1, user2]
39
+ config = Gas::Configuration.new users
40
+ config.exists?('walle').should be_true
41
+ config.exists?('foo').should be_false
42
+ config.exists?('user2').should be_true
43
+ end
44
+
45
+ it 'should be able to get a user from a nickname' do
46
+ user1 = Gas::User.new 'Fredrik Wallgren', 'fredrik.wallgren@gmail.com', 'walle'
47
+ user2 = Gas::User.new 'foo', 'bar', 'user2'
48
+ users = [user1, user2]
49
+ config = Gas::Configuration.new users
50
+ config.get('walle').should == user1
51
+ config.get('user2').should == user2
52
+ config['walle'].should == user1
53
+ config['user2'].should == user2
54
+ config[:walle].should == user1
55
+ config[:user2].should == user2
56
+ end
57
+
58
+ it 'should be able to add users' do
59
+ user1 = Gas::User.new 'Fredrik Wallgren', 'fredrik.wallgren@gmail.com', 'walle'
60
+ user2 = Gas::User.new 'foo', 'bar', 'user2'
61
+ users = [user1]
62
+ config = Gas::Configuration.new users
63
+ config.users.count.should == 1
64
+ config.add user2
65
+ config.users.count.should == 2
66
+ end
67
+
68
+ it 'should be able to delete users by nickname' do
69
+ user1 = Gas::User.new 'Fredrik Wallgren', 'fredrik.wallgren@gmail.com', 'walle'
70
+ user2 = Gas::User.new 'foo', 'bar', 'user2'
71
+ users = [user1, user2]
72
+ config = Gas::Configuration.new users
73
+ config.users.count.should == 2
74
+ config.delete 'walle'
75
+ config.users.count.should == 1
76
+ config.delete 'user2'
77
+ config.users.count.should == 0
78
+ end
79
+ end
80
+
@@ -0,0 +1,37 @@
1
+ require './spec/spec_helper'
2
+
3
+ require './lib/gas'
4
+
5
+ describe Gas::User do
6
+ it 'should be able to parse name and email from gitconfig format' do
7
+ name = 'Fredrik Wallgren'
8
+ email = 'fredrik.wallgren@gmail.com'
9
+ gitconfig = "[other stuff]\n foo = bar\n\n[user]\n name = #{name}\n email = #{email}\n\n[foo]\n bar = foo"
10
+ user = Gas::User.parse gitconfig
11
+ user.name.should == name
12
+ user.email.should == email
13
+ end
14
+
15
+ it 'should output in the right format' do
16
+ name = 'Fredrik Wallgren'
17
+ email = 'fredrik.wallgren@gmail.com'
18
+ nickname = 'walle'
19
+ user = Gas::User.new name, email, nickname
20
+ user.to_s.should == "[#{nickname}]\n name = #{name}\n email = #{email}"
21
+ end
22
+
23
+ it 'should output a git user in the right format' do
24
+ name = 'Fredrik Wallgren'
25
+ email = 'fredrik.wallgren@gmail.com'
26
+ user = Gas::User.new name, email
27
+ user.git_user.should == "[user]\n name = #{name}\n email = #{email}"
28
+ end
29
+
30
+ it 'should be able to have a nickname' do
31
+ name = 'Fredrik Wallgren'
32
+ email = 'fredrik.wallgren@gmail.com'
33
+ nickname = 'walle'
34
+ user = Gas::User.new name, email, nickname
35
+ user.nickname.should == nickname
36
+ end
37
+ end
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+
3
+ RSpec.configure do |config|
4
+ config.mock_with :rr
5
+ end
6
+
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gas
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Fredrik Wallgren
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-07-20 00:00:00.000000000 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thor
17
+ requirement: &19173520 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 0.14.6
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *19173520
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ requirement: &19210280 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *19210280
37
+ - !ruby/object:Gem::Dependency
38
+ name: rr
39
+ requirement: &19209820 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *19209820
48
+ - !ruby/object:Gem::Dependency
49
+ name: bundler
50
+ requirement: &19209400 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *19209400
59
+ description: Gas is a utility to keep track of your git authors. Add them to gas and
60
+ switch at any time. Great if you use one author at work and one at home for instance.
61
+ email: fredrik.wallgren@gmail.com
62
+ executables:
63
+ - gas
64
+ extensions: []
65
+ extra_rdoc_files:
66
+ - README.textile
67
+ - LICENSE
68
+ files:
69
+ - bin/gas
70
+ - lib/gas.rb
71
+ - lib/gas/version.rb
72
+ - lib/gas/user.rb
73
+ - lib/gas/configuration.rb
74
+ - spec/spec_helper.rb
75
+ - spec/gas/user_spec.rb
76
+ - spec/gas/configuration_spec.rb
77
+ - LICENSE
78
+ - README.textile
79
+ has_rdoc: true
80
+ homepage: https://github.com/walle/gas
81
+ licenses: []
82
+ post_install_message:
83
+ rdoc_options:
84
+ - --charset=UTF-8
85
+ require_paths:
86
+ - - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project: gas
101
+ rubygems_version: 1.6.2
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: Manage your git author accounts
105
+ test_files: []