Class: GitGo::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/git_go/cli.rb

Instance Method Summary collapse

Instance Method Details

#clip(name) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/git_go/cli.rb', line 118

def clip(name)
  core = GitGo::Core.new
  conn = core.connection

  response     = conn.bash("find ./*.git/ -mindepth 0 -maxdepth 0")
  repositories = response[:stdout].split("\n").map { |r| File.basename(r) }
  repository   = repositories.select { |r| r == name + ".git" }.first

  if repository.nil?
    puts %Q(There is no git repository named "#{name}". ) +
         %Q(Run `gg list` for the list of repositories.)
    return conn.close_and_exit(1)
  else
    url = "#{core.user}@#{core.host}:#{repository}"
    system(%Q(printf "#{url}" | pbcopy))
    puts "#{url} copied to clipboard."
  end
end

#create(name) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/git_go/cli.rb', line 8

def create(name)
  core = GitGo::Core.new
  conn = core.connection

  if conn.directory?("#{name}.git")
    puts %Q("#{name}" already exists.)
    return conn.close_and_exit(1)
  else
    conn.mkdir("#{name}.git")
    conn.cd("#{name}.git")
    response = conn.bash("git init --bare")

    if response[:exit_code] == 0
      puts "Repository created at: #{core.user}@#{core.host}:#{name}.git"
    else
      puts "Failed to create repository."
      puts response[:stderr] + response[:stdout]
    end
  end

  conn.close
end

#destroy(name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/git_go/cli.rb', line 32

def destroy(name)
  core       = GitGo::Core.new
  conn       = core.connection
  repository = "#{name}.git"

  if conn.directory?(repository)
    if yes?(%Q(Are you sure you want to destroy "#{name}"?))
      response = conn.rm(repository)
      if response[:exit_code] == 0
        puts %Q(Repository "#{name}" was destroyed.)
      else
        puts %Q(Repository "#{name}" could not be destroyed.)
        puts response[:stdout] + response[:stderr]
        return conn.close_and_exit(1)
      end
    end
  else
    puts %Q(Repository "#{name}" does not exist.)
    return conn.close_and_exit(1)
  end

  conn.close
end

#instructionsObject



138
139
140
# File 'lib/git_go/cli.rb', line 138

def instructions
  puts Core.instructions
end

#listObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/git_go/cli.rb', line 86

def list
  core = GitGo::Core.new
  conn = core.connection

  response = conn.bash("find ./*.git/ -mindepth 0 -maxdepth 0")

  if response[:exit_code] == 0
    repositories = response[:stdout].split("\n").map { |r| File.basename(r) }
    list = [["Repository", "Git Fetch/Push URL"]]

    repositories.each do |repository|
      list << [
        repository.sub(".git", ""),
        "#{core.user}@#{core.host}:#{repository}"
      ]
    end

    amount = list.count - 1
    out    = Formatter.columns(list, :spacing => 4, :header => true)

    puts "\n"
    puts out
    puts "\nThere #{amount == 1 ? "is" : "are"} #{amount} git " +
      "#{amount == 1 ? "repository" : "repositories"}.\n"
  else
    puts "There are no repositories at #{core.user}@#{core.host}."
  end

  conn.close
end

#rename(name, new_name) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/git_go/cli.rb', line 57

def rename(name, new_name)
  core           = GitGo::Core.new
  conn           = core.connection
  repository     = "#{name}.git"
  new_repository = "#{new_name}.git"

  if not conn.directory?(repository)
    puts %Q(Repository "#{name}" does not exist.)
    return conn.close_and_exit(1)
  end

  if conn.directory?(new_repository)
    puts %Q(Repository "#{new_name}" already exists.)
    return conn.close_and_exit(1)
  end

  response = conn.mv(repository, new_repository)
  if response[:exit_code] == 0
    puts %Q(Repository "#{name}" renamed to "#{new_name}".)
  else
    puts %Q(Could not rename "#{name}" to "#{new_name}".)
    puts response[:stderr] + response[:stdout]
    return conn.close_and_exit(1)
  end

  conn.close
end