Class: Bub::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/bub/command.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Command

Returns a new instance of Command.



13
14
15
# File 'lib/bub/command.rb', line 13

def initialize(argv)
  @argv = argv
end

Class Method Details

.call(*args) ⇒ Object



9
10
11
# File 'lib/bub/command.rb', line 9

def self.call(*args)
  new(*args).call
end

Instance Method Details

#callObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bub/command.rb', line 17

def call
  case action_name
  when nil
    warn usage
    raise Errors::MissingActionNameArgumentError
  when "create", "delete"
    raise Errors::NonExistentGitRepositoryError, action_name unless is_repo?
    self.send(action_name)
  else
    raise Errors::UnknownActionNameError, action_name
  end
rescue Errors::Base => exception
  abort "Error: #{exception}"
end

#createObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bub/command.rb', line 32

def create
  response = HTTP.basic_auth(user: "#{username}", pass: "#{config['password']}").
    post(endpoint, body: "name=#{repo}&scm=git&is_private=true&fork_policy=no_forks")
  if response.code.to_i == 200
    puts "Updating origin"
    puts "git remote add origin [email protected]:#{username}/#{repo}.git"
    system "git remote add origin [email protected]:#{username}/#{repo}.git"
    puts "created repository: #{username}/#{repo}"
  else
    puts "Error #{action_name}: #{response} (HTTP #{response.status})"
  end
end

#deleteObject



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bub/command.rb', line 45

def delete
  if deletable?
    response = HTTP.basic_auth(user: "#{username}", pass: "#{config['password']}").
      delete(endpoint)
    if response.code.to_i == 204
      puts "Your repository #{repo} was successfully deleted."
    else
      puts "Error #{action_name}: #{response} (HTTP #{response.status})"
    end
  else
    puts "Canceled."
  end
end