BigW Consortium Gitlab

command_line_util.rb 1.42 KB
Newer Older
1
module Gitlab
2 3
  module ImportExport
    module CommandLineUtil
4 5
      DEFAULT_MODE = 0700

6 7
      def tar_czf(archive:, dir:)
        tar_with_options(archive: archive, dir: dir, options: 'czf')
8 9
      end

10 11 12 13
      def untar_zxf(archive:, dir:)
        untar_with_options(archive: archive, dir: dir, options: 'zxf')
      end

14
      def git_bundle(repo_path:, bundle_path:)
15
        execute(%W(#{git_bin_path} --git-dir=#{repo_path} bundle create #{bundle_path} --all))
16
      end
17

18 19 20 21 22
      def mkdir_p(path)
        FileUtils.mkdir_p(path, mode: DEFAULT_MODE)
        FileUtils.chmod(DEFAULT_MODE, path)
      end

James Lopez committed
23 24
      private

25
      def tar_with_options(archive:, dir:, options:)
26
        execute(%W(tar -#{options} #{archive} -C #{dir} .))
27
      end
28 29

      def untar_with_options(archive:, dir:, options:)
30 31 32 33
        execute(%W(tar -#{options} #{archive} -C #{dir}))
      end

      def execute(cmd)
James Lopez committed
34
        output, status = Gitlab::Popen.popen(cmd)
35
        @shared.error(Gitlab::ImportExport::Error.new(output.to_s)) unless status.zero?
36 37
        status.zero?
      end
38 39 40 41

      def git_bin_path
        Gitlab.config.git.bin_path
      end
42 43 44 45 46

      def copy_files(source, destination)
        # if we are copying files, create the destination folder
        destination_folder = File.file?(source) ? File.dirname(destination) : destination

47
        mkdir_p(destination_folder)
48 49 50
        FileUtils.copy_entry(source, destination)
        true
      end
51 52 53
    end
  end
end