mirror of
https://gitlab.com/gitlab-org/gitlab-foss.git
synced 2025-08-10 01:31:45 +00:00
Add latest changes from gitlab-org/gitlab@master
This commit is contained in:
37
gems/gitlab-backup-cli/lib/gitlab/backup/cli/dependencies.rb
Normal file
37
gems/gitlab-backup-cli/lib/gitlab/backup/cli/dependencies.rb
Normal file
@ -0,0 +1,37 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Gitlab
|
||||
module Backup
|
||||
module Cli
|
||||
module Dependencies
|
||||
# Search on PATH or default locations for provided binary and return its fullpath
|
||||
#
|
||||
# @param [String] binary name
|
||||
# @return [String|False] full path to the binary file
|
||||
def self.find_executable(binary)
|
||||
executable_file = proc { |name| next name if File.file?(name) && File.executable?(name) }
|
||||
|
||||
# Retrieve PATH from ENV or use a fallback
|
||||
path = ENV['PATH']&.split(File::PATH_SEPARATOR) || %w[/usr/local/bin /usr/bin /bin]
|
||||
|
||||
# check binary against each PATH
|
||||
path.each do |dir|
|
||||
file = File.expand_path(binary, dir)
|
||||
|
||||
return file if executable_file.call(file)
|
||||
end
|
||||
|
||||
nil
|
||||
end
|
||||
|
||||
# Check whether provided binary name exists on PATH or default locations
|
||||
#
|
||||
# @param [String] binary name
|
||||
# @return [Boolean] whether binary exists
|
||||
def self.executable_exist?(name)
|
||||
!!find_executable(name)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
13
gems/gitlab-backup-cli/lib/gitlab/backup/cli/shell.rb
Normal file
13
gems/gitlab-backup-cli/lib/gitlab/backup/cli/shell.rb
Normal file
@ -0,0 +1,13 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
autoload :Open3, 'open3'
|
||||
|
||||
module Gitlab
|
||||
module Backup
|
||||
module Cli
|
||||
module Shell
|
||||
autoload :Command, 'gitlab/backup/cli/shell/command'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,43 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Gitlab
|
||||
module Backup
|
||||
module Cli
|
||||
module Shell
|
||||
# Abstraction to control shell command execution
|
||||
# It provides an easier API to common usages
|
||||
class Command
|
||||
attr_reader :cmd_args, :env
|
||||
|
||||
# Result data structure from running a command
|
||||
#
|
||||
# @attr [String] stdout
|
||||
# @attr [String] stderr
|
||||
# @attr [Process::Status] status
|
||||
# @attr [Float] duration
|
||||
Result = Struct.new(:stdout, :stderr, :status, :duration, keyword_init: true)
|
||||
|
||||
# @example Usage
|
||||
# Shell.new('echo', 'Some amazing output').capture
|
||||
# @param [Array<String>] cmd_args
|
||||
# @param [Hash<String,String>] env
|
||||
def initialize(*cmd_args, env: {})
|
||||
@cmd_args = cmd_args
|
||||
@env = env
|
||||
end
|
||||
|
||||
# Execute a process and return its output and status
|
||||
#
|
||||
# @return [Command::Result] Captured output from executing a process
|
||||
def capture
|
||||
start = Time.now
|
||||
stdout, stderr, status = Open3.capture3(env, *cmd_args)
|
||||
duration = Time.now - start
|
||||
|
||||
Result.new(stdout: stdout, stderr: stderr, status: status, duration: duration)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -5,6 +5,7 @@ module Gitlab
|
||||
module Cli
|
||||
module Utils
|
||||
autoload :PgDump, 'gitlab/backup/cli/utils/pg_dump'
|
||||
autoload :Tar, 'gitlab/backup/cli/utils/tar'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
35
gems/gitlab-backup-cli/lib/gitlab/backup/cli/utils/tar.rb
Normal file
35
gems/gitlab-backup-cli/lib/gitlab/backup/cli/utils/tar.rb
Normal file
@ -0,0 +1,35 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Gitlab
|
||||
module Backup
|
||||
module Cli
|
||||
module Utils
|
||||
# Run tar command to create or extract content from an archive
|
||||
class Tar
|
||||
# Returns the version of tar command available
|
||||
#
|
||||
# @return [String] the first line of `--version` output
|
||||
def version
|
||||
version = Shell::Command.new(cmd, '--version').capture.stdout.dup
|
||||
version.force_encoding('locale').split("\n").first
|
||||
end
|
||||
|
||||
def cmd
|
||||
@cmd ||= if gtar_available?
|
||||
# In BSD/Darwin we can get GNU tar by running 'gtar' instead
|
||||
'gtar'
|
||||
else
|
||||
'tar'
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def gtar_available?
|
||||
Dependencies.executable_exist?('gtar')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user