barkeep-client 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES.markdown ADDED
@@ -0,0 +1,15 @@
1
+ Barkeep Client Changes
2
+ ======================
3
+
4
+ This file summarizes changes between barkeep-client gem versions.
5
+
6
+ 0.1.1
7
+ -----
8
+
9
+ * Make it easier to pass in arbitrary arguments to `git log` from the `unapproved` command using `--`.
10
+
11
+
12
+ 0.1.0
13
+ -----
14
+
15
+ Initial version, including `commit` and `unapproved` commands.
data/README.md CHANGED
@@ -1,4 +1,6 @@
1
1
  Barkeep Client
2
2
  ==============
3
3
 
4
- ## Coming soon!
4
+ This is a commandline tool to make use of the Barkeep REST API.
5
+
6
+ See usage instructions in the Barkeep readme.
@@ -1,9 +1,11 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
+ require "barkeep/version"
5
+
4
6
  Gem::Specification.new do |s|
5
7
  s.name = "barkeep-client"
6
- s.version = "0.1.1"
8
+ s.version = BarkeepClient::VERSION
7
9
  s.authors = ["Caleb Spare"]
8
10
  s.email = ["caleb@ooyala.com"]
9
11
  s.homepage = "https://github.com/ooyala/barkeep"
data/bin/barkeep CHANGED
@@ -5,20 +5,17 @@ require "dedent"
5
5
  require "yaml"
6
6
 
7
7
  require "barkeep/commit"
8
+ require "barkeep/configuration"
8
9
  require "barkeep/unapproved"
9
-
10
- CONFIG_FILE = File.join(ENV["HOME"], ".barkeeprc")
10
+ require "barkeep/version"
11
11
 
12
12
  COMMANDS = {
13
13
  "commit" => "Get information about a particular commit.",
14
14
  "unapproved" => "Find unapproved commits from a list or commit range."
15
15
  }
16
16
 
17
- sub_command = ARGV.shift
18
- unless COMMANDS.include? sub_command
19
- puts sub_command ? "Error: unrecognized command '#{sub_command}'" : "Error: must provide command."
17
+ def usage(exit_status)
20
18
  puts <<-EOS.dedent
21
-
22
19
  Usage:
23
20
  $ barkeep <command> [args]
24
21
  where <command> is one of:
@@ -28,26 +25,28 @@ unless COMMANDS.include? sub_command
28
25
 
29
26
  Type 'barkeep <command> --help' for more information about a particular command.
30
27
  EOS
31
- exit 1
28
+ exit exit_status
32
29
  end
33
30
 
34
- # Load in configuration from ~/.barkeeprc.
35
- begin
36
- configuration = YAML.load_file CONFIG_FILE
37
- rescue
38
- puts <<-EOS.dedent
39
- Error: #{CONFIG_FILE} must exist to specify barkeep server information, and it must be a valid YAML file.
40
- EOS
41
- exit 1
31
+ sub_command = ARGV.shift
32
+
33
+ usage(0) if ["--help", "help", "-h"].include? sub_command
34
+ if ["-v", "--version"].include? sub_command
35
+ puts "barkeep-client version #{BarkeepClient::VERSION}"
36
+ exit
37
+ end
38
+ unless COMMANDS.include? sub_command
39
+ puts sub_command ? "Error: unrecognized command '#{sub_command}'" : "Error: must provide command."
40
+ puts
41
+ usage(1)
42
42
  end
43
43
 
44
- # Check the configuration
45
- REQUIRED_KEYS = %w[barkeep_server]
46
- unless REQUIRED_KEYS.each { |key| configuration.include? key }
47
- puts "Error: each of the following configuration keys are required in your #{CONFIG_FILE}:"
48
- REQUIRED_KEYS.each { |key| puts " #{key}" }
44
+ begin
45
+ configuration = BarkeepClient.get_configuration
46
+ rescue RuntimeError => e
47
+ puts e.message
49
48
  exit 1
50
49
  end
51
50
 
52
51
  # Delegate to the trollop parsing + client logic that lives in the appropriate subcommand file.
53
- BarkeepClient.send sub_command.to_sym, configuration
52
+ BarkeepClient::Commands.send sub_command.to_sym, configuration
@@ -7,45 +7,66 @@ require "json"
7
7
  require "barkeep/constants"
8
8
 
9
9
  module BarkeepClient
10
- def self.commit(configuration)
11
- options = Trollop::options do
12
- banner <<-EOS.dedent
13
- Barkeep's 'commit' command shows information about a particular commit given its SHA.
10
+ module Commands
11
+ def self.commit(configuration)
12
+ options = Trollop::options do
13
+ banner <<-EOS.dedent
14
+ Barkeep's 'commit' command shows information about a particular commit given its SHA.
14
15
 
15
- Usage:
16
- $ barkeep commit [options] <commit>
17
- where <commit> is specified as a (partial) SHA-1 hash (for the current repo) or as, for example,
18
- myrepo/d29a4a0fa
19
- to specify a particular repository, and [options] can include:
20
- EOS
21
- end
22
- Trollop::die "must provide a commit sha" unless ARGV.size == 1
16
+ Usage:
17
+ $ barkeep commit [options] <commit>
18
+ where <commit> is specified as a (partial) SHA-1 hash (for the current repo) or as, for example,
19
+ myrepo/d29a4a0fa
20
+ to specify a particular repository, and [options] can include:
21
+ EOS
22
+ end
23
+ Trollop::die "must provide a commit sha" unless ARGV.size == 1
23
24
 
24
- commit = ARGV[0]
25
- repo, sha = case commit
26
- when %r{^[^/]+/#{SHA_REGEX}$} # foo/abc123
27
- commit.split "/"
28
- when /^#{SHA_REGEX}$/ # abc123
29
- repo = File.basename(`git rev-parse --show-toplevel`).strip
30
- if repo.empty?
31
- Trollop::die "need to be in a git repo or specify a repository (e.g. myrepo/abc123)"
25
+ commit = ARGV[0]
26
+ repo, sha = case commit
27
+ when %r{^[^/]+/#{SHA_REGEX}$} # foo/abc123
28
+ commit.split "/"
29
+ when /^#{SHA_REGEX}$/ # abc123
30
+ repo = File.basename(`git rev-parse --show-toplevel`).strip
31
+ if repo.empty?
32
+ Trollop::die "need to be in a git repo or specify a repository (e.g. myrepo/abc123)"
33
+ end
34
+ [repo, commit]
35
+ else
36
+ Trollop::die "#{commit} is an invalid commit specification"
32
37
  end
33
- [repo, commit]
34
- else
35
- Trollop::die "#{commit} is an invalid commit specification"
36
- end
37
- uri = URI.parse(File.join(configuration["barkeep_server"], "/api/commits/#{repo}/#{sha}"))
38
- result = Net::HTTP.get_response uri
39
- if result.code.to_i != 200
40
- error = JSON.parse(result.body)["message"] rescue nil
41
- puts error ? "Error: #{error}" : "Unspecified server error."
42
- exit 1
38
+
39
+ begin
40
+ result = BarkeepClient.commits(configuration, repo, [sha])[sha]
41
+ rescue RuntimeError => e
42
+ puts e.message
43
+ exit 1
44
+ end
45
+
46
+ result.each { |key, value| puts " #{key.rjust(result.keys.map(&:size).max)}: #{value}" }
43
47
  end
44
- info = JSON.parse(result.body)
45
- info.each do |key, value|
46
- next if value.nil?
47
- value = Time.at(value).strftime("%m/%d/%Y %I:%M%p") if key == "approved_at"
48
- puts " #{key.rjust(info.keys.map(&:size).max)}: #{value}"
48
+ end
49
+
50
+ # Core method for calling Barkeep's commit API call.
51
+ # TODO: Support querying lots of commits at once using the altered API call.
52
+ def self.commits(configuration, repo, shas)
53
+ result = {}
54
+ shas.each do |sha|
55
+ uri = URI.parse(File.join(configuration["barkeep_server"], "/api/commits/#{repo}/#{sha}"))
56
+ response = Net::HTTP.get_response uri
57
+ if response.code.to_i != 200
58
+ error = JSON.parse(response.body)["message"] rescue nil
59
+ raise error ? "Error: #{error}" : "Unspecified server error."
60
+ end
61
+ info = JSON.parse(response.body)
62
+ commit_data = {}
63
+ info.each do |key, value|
64
+ next if value.nil?
65
+ value = Time.at(value).strftime("%m/%d/%Y %I:%M%p") if key == "approved_at"
66
+ commit_data[key] = value
67
+ end
68
+ result[sha] = commit_data
49
69
  end
70
+ result
50
71
  end
51
72
  end
@@ -0,0 +1,28 @@
1
+ # Load a user's configuration
2
+
3
+ require "yaml"
4
+ require "dedent"
5
+
6
+ require "barkeep/constants"
7
+
8
+ module BarkeepClient
9
+ def self.get_configuration
10
+ begin
11
+ configuration = YAML.load_file CONFIG_FILE
12
+ raise "Bad file" if configuration == false # On empty yaml files or ones with only comments. Lame API
13
+ rescue
14
+ raise <<-EOS.dedent
15
+ Error: #{CONFIG_FILE} must exist to specify barkeep server information,
16
+ and it must be a valid YAML file.
17
+ EOS
18
+ end
19
+
20
+ # Check the configuration
21
+ unless REQUIRED_CONFIG_KEYS.all? { |key| configuration.include? key }
22
+ error = "Error: the following configuration keys are required in your #{CONFIG_FILE}: " <<
23
+ REQUIRED_CONFIG_KEYS.join(', ')
24
+ raise error
25
+ end
26
+ configuration
27
+ end
28
+ end
@@ -1,3 +1,5 @@
1
1
  module BarkeepClient
2
2
  SHA_REGEX = /[0-9a-fA-F]+/
3
+ CONFIG_FILE = File.join(ENV["HOME"], ".barkeeprc")
4
+ REQUIRED_CONFIG_KEYS = %w[barkeep_server]
3
5
  end
@@ -7,75 +7,72 @@ require "json"
7
7
  require "barkeep/constants"
8
8
 
9
9
  module BarkeepClient
10
- def self.unapproved(configuration)
11
- options = Trollop::options do
12
- banner <<-EOS.dedent
13
- Barkeep's 'unapproved' command shows information about a particular commit. It MUST be run from a git
14
- repository of the same name as the repository on the server.
10
+ module Commands
11
+ def self.unapproved(configuration)
12
+ options = Trollop::options do
13
+ banner <<-EOS.dedent
14
+ Barkeep's 'unapproved' command shows information about a particular commit. It MUST be run from a git
15
+ repository of the same name as the repository on the server.
15
16
 
16
- Usage:
17
- $ barkeep unapproved [options] <commit-range>
18
- where <commit-range> is a commit range specified using git's range syntax (see `man gitrevisions`).
19
- For example:
17
+ Usage:
18
+ $ barkeep unapproved [options] <commit-range>
19
+ where <commit-range> is a commit range specified using git's range syntax (see `man gitrevisions`).
20
+ For example:
20
21
 
21
- $ barkeep unapproved abc123
22
- $ barkeep unapproved ^abc123 def456
23
- $ barkeep unapproved abc123..def456
22
+ $ barkeep unapproved abc123
23
+ $ barkeep unapproved ^abc123 def456
24
+ $ barkeep unapproved abc123..def456
24
25
 
25
- [options] can include:
26
- EOS
27
- opt :stop_on_unapproved, "Stop and print a message on the first unapproved commit."
28
- end
29
- Trollop::die "must provide a commit range" if ARGV.empty?
26
+ [options] can include:
27
+ EOS
28
+ end
29
+ Trollop::die "must provide a commit range" if ARGV.empty?
30
30
 
31
- repo = File.basename(`git rev-parse --show-toplevel`).strip
32
- if repo.empty?
33
- Trollop::die "need to be in a git repo"
34
- end
31
+ repo = File.basename(`git rev-parse --show-toplevel`).strip
32
+ if repo.empty?
33
+ Trollop::die "need to be in a git repo"
34
+ end
35
35
 
36
- commit_range = ARGV.map { |arg| "'#{arg}'" }.join(" ")
37
- commits_string = `git log --format='%H' #{commit_range}`
38
- exit(1) unless $?.to_i.zero?
39
- commits = commits_string.split("\n").map(&:strip)
36
+ commit_range = ARGV.map { |arg| "'#{arg}'" }.join(" ")
37
+ commits_string = `git log --format='%H' #{commit_range}`
38
+ exit(1) unless $?.to_i.zero?
39
+ commits = commits_string.split("\n").map(&:strip)
40
40
 
41
- if commits.empty?
42
- puts "No commits in range."
43
- exit 0
44
- elsif commits.size > 100
45
- puts "Warning: #{commits.size} commits in range. Lookup could be very slow. Proceed? [yN]"
46
- unless STDIN.gets.downcase.strip =~ /^y(es)?/
47
- puts "Aborting."
41
+ if commits.empty?
42
+ puts "No commits in range."
48
43
  exit 0
44
+ elsif commits.size > 100
45
+ puts "Warning: #{commits.size} commits in range. Lookup could be very slow. Proceed? [yN]"
46
+ unless STDIN.gets.downcase.strip =~ /^y(es)?/
47
+ puts "Aborting."
48
+ exit 0
49
+ end
49
50
  end
50
- end
51
51
 
52
- unapproved_commits = {}
53
- commits.each do |sha|
54
- uri = URI.parse(File.join(configuration["barkeep_server"], "/api/commits/#{repo}/#{sha}"))
55
- result = Net::HTTP.get_response uri
56
- if result.code.to_i != 200
57
- error = JSON.parse(result.body)["message"] rescue nil
58
- puts error ? "Error: #{error}" : "Unspecified server error."
52
+ begin
53
+ commit_data = BarkeepClient.commits(configuration, repo, commits)
54
+ rescue RuntimeError => e
55
+ puts e.message
59
56
  exit 1
60
57
  end
61
- info = JSON.parse(result.body)
62
- next if info["approved"]
63
- author_name = `git log --format='%an' #{sha} -n 1`
64
- if options[:stop_on_unapproved]
65
- puts "Found unapproved commit #{sha} by #{author_name}"
66
- exit 1
58
+
59
+ unapproved_commits = {}
60
+
61
+ commit_data.each do |sha, commit|
62
+ next if commit["approved"]
63
+ author_name = `git log --format='%an' #{sha} -n 1`
64
+ unapproved_commits[sha] = author_name
67
65
  end
68
- unapproved_commits[sha] = author_name
69
- end
70
66
 
71
- if unapproved_commits.empty?
72
- puts "#{commits.size} approved commit(s) and no unapproved commits in the given range."
73
- else
74
- puts "#{commits.size - unapproved_commits.size} approved commit(s) and " <<
75
- "#{unapproved_commits.size} unapproved commit(s) in the given range."
76
- puts "Unapproved:"
77
- unapproved_commits.each { |sha, author| puts "#{sha} #{author}" }
78
- exit 1
67
+ if unapproved_commits.empty?
68
+ puts "#{commits.size} approved commit(s) and no unapproved commits in the given range."
69
+ else
70
+ puts "#{commits.size - unapproved_commits.size} approved commit(s) and " <<
71
+ "#{unapproved_commits.size} unapproved commit(s) in the given range."
72
+ puts "Unapproved:"
73
+ unapproved_commits.each { |sha, author| puts "#{sha} #{author}" }
74
+ exit 1
75
+ end
79
76
  end
80
77
  end
81
78
  end
@@ -0,0 +1,3 @@
1
+ module BarkeepClient
2
+ VERSION = "0.1.2"
3
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: barkeep-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-15 00:00:00.000000000Z
12
+ date: 2011-12-19 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: trollop
16
- requirement: &70154030714680 !ruby/object:Gem::Requirement
16
+ requirement: &70356271100060 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.16.2
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70154030714680
24
+ version_requirements: *70356271100060
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: dedent
27
- requirement: &70154030714020 !ruby/object:Gem::Requirement
27
+ requirement: &70356271098120 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 0.0.2
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70154030714020
35
+ version_requirements: *70356271098120
36
36
  description: A command-line client for Barkeep's REST API.
37
37
  email:
38
38
  - caleb@ooyala.com
@@ -43,6 +43,7 @@ extra_rdoc_files: []
43
43
  files:
44
44
  - .gitignore
45
45
  - .rbenv-version
46
+ - CHANGES.markdown
46
47
  - Gemfile
47
48
  - Gemfile.lock
48
49
  - README.md
@@ -50,8 +51,10 @@ files:
50
51
  - barkeep_client.gemspec
51
52
  - bin/barkeep
52
53
  - lib/barkeep/commit.rb
54
+ - lib/barkeep/configuration.rb
53
55
  - lib/barkeep/constants.rb
54
56
  - lib/barkeep/unapproved.rb
57
+ - lib/barkeep/version.rb
55
58
  homepage: https://github.com/ooyala/barkeep
56
59
  licenses: []
57
60
  post_install_message: