Add latest changes from gitlab-org/gitlab@master

This commit is contained in:
GitLab Bot
2024-03-06 15:09:47 +00:00
parent e408c9c787
commit 96dfc10639
90 changed files with 948 additions and 282 deletions

View 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

View 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

View File

@ -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

View File

@ -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

View 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