Class: Spectre::Git::GitAccess

Inherits:
DslClass
  • Object
show all
Defined in:
lib/spectre/git.rb

Instance Method Summary collapse

Constructor Details

#initialize(cfg, logger) ⇒ GitAccess

Returns a new instance of GitAccess.



10
11
12
13
14
15
16
17
# File 'lib/spectre/git.rb', line 10

def initialize cfg, logger
  @__logger = logger
  @__cfg = cfg
  @__repo_path = nil

  url(@__cfg['url'])
  @__cfg['branch'] = 'master' unless @__cfg['branch']
end

Instance Method Details

#add(path) ⇒ Object



83
84
85
# File 'lib/spectre/git.rb', line 83

def add path
  run("git add \"#{path}\"")
end

#add_allObject



87
88
89
# File 'lib/spectre/git.rb', line 87

def add_all
  run("git add --all")
end

#branch(name) ⇒ Object



49
50
51
# File 'lib/spectre/git.rb', line 49

def branch name
  @__cfg['branch'] = name
end

#cert(file_path) ⇒ Object



45
46
47
# File 'lib/spectre/git.rb', line 45

def cert file_path
  @__cfg['cert'] = file_path
end

#cleanupObject



126
127
128
# File 'lib/spectre/git.rb', line 126

def cleanup
  FileUtils.rm_rf(@__repo_path)
end

#cloneObject



53
54
55
56
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
# File 'lib/spectre/git.rb', line 53

def clone
  @__repo_path = File.absolute_path File.join(@__cfg['working_dir'], @__cfg['name'])

  if File.exist? @__repo_path
    FileUtils.rm_rf(@__repo_path)
    @__logger.debug("repo path '#{@__repo_path}' removed")
  end

  FileUtils.mkpath(@__repo_path)
  @__logger.debug("repo path '#{@__repo_path}' created")

  clone_cmd = ['git', 'clone']
  clone_cmd << '--branch'
  clone_cmd << @__cfg['branch']

  if @__cfg['cert']
    clone_cmd << '--config'
    clone_cmd << "http.sslCAInfo=#{@__cfg['cert']}"
  end

  clone_cmd << get_url
  clone_cmd << @__repo_path

  clone_cmd = clone_cmd.join(' ')

  @__logger.info("#{clone_cmd.gsub /:([^\:\@\/\/]*)@/, ':*****@'}")

  run(clone_cmd, log: false)
end

#commit(message) ⇒ Object



95
96
97
# File 'lib/spectre/git.rb', line 95

def commit message
  run("git commit -m \"#{message}\"")
end

#password(pass) ⇒ Object



36
37
38
# File 'lib/spectre/git.rb', line 36

def password pass
  @__cfg['password'] = pass
end

#pullObject



103
104
105
# File 'lib/spectre/git.rb', line 103

def pull
  run("git pull")
end

#pushObject



99
100
101
# File 'lib/spectre/git.rb', line 99

def push
  run("git push")
end

#read_file(path) ⇒ Object



121
122
123
124
# File 'lib/spectre/git.rb', line 121

def read_file path
  full_path = File.join(@__repo_path, path)
  File.read(full_path)
end

#run(cmd, log: true) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/spectre/git.rb', line 130

def run cmd, log: true
  stdin, stdout, stderr, wait_thr = Open3.popen3(cmd, chdir: @__repo_path)

  @__logger.info(cmd) if log

  output = stdout.gets(nil)
  stdout.close

  error = stderr.gets(nil)
  stderr.close

  raise error unless wait_thr.value.exitstatus == 0
end

#tag(name, message: nil) ⇒ Object



91
92
93
# File 'lib/spectre/git.rb', line 91

def tag name, message: nil
  run("git tag -a -m \"#{message}\"")
end

#url(git_url) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/spectre/git.rb', line 19

def url git_url
  url_match = git_url.match /^(?<scheme>http(?:s)?):\/\/(?:(?<user>[^\/:]*):(?<pass>.*)@)?(?<url>.*\/(?<name>[^\/]*)\.git)$/

  raise "invalid git url: '#{git_url}'" unless url_match

  @__cfg['url_path'] = url_match[:url]
  @__cfg['scheme'] = url_match[:scheme]
  @__cfg['username'] = url_match[:user] unless @__cfg['username']
  @__cfg['password'] = url_match[:pass] unless @__cfg['password']
  @__cfg['name'] = url_match[:name] unless @__cfg['name']
  @__cfg['working_dir'] = './tmp' unless @__cfg['working_dir']
end

#username(user) ⇒ Object



32
33
34
# File 'lib/spectre/git.rb', line 32

def username user
  @__cfg['username'] = user
end

#working_dir(path) ⇒ Object



40
41
42
43
# File 'lib/spectre/git.rb', line 40

def working_dir path
  @__cfg['working_dir'] = path if path
  @__cfg['working_dir']
end

#write_file(path, content) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/spectre/git.rb', line 111

def write_file path, content
  full_path = File.join(@__repo_path, path)

  file = File.open(full_path, 'w')
  file.write(content)
  file.close

  full_path
end