Sha256: 52436ec1fdaf5d697c4817e7fbbca9ab41122b8829497b1d680fc5a738257ae2

Contents?: true

Size: 943 Bytes

Versions: 1

Compression:

Stored size: 943 Bytes

Contents

class KuberKit::Tools::BuildDirCleaner
  include KuberKit::Import[
    "shell.bash_commands",
  ]

  KEEP_DIRS_COUNT = 10

  Contract KuberKit::Shell::AbstractShell, KeywordArgs[parent_dir: String] => Any
  def call(shell, parent_dir:)
    dirs_to_delete = get_ancient_builds_dirs(shell, parent_dir: parent_dir)

    dirs_to_delete.each do |dir|
      bash_commands.rm_rf(shell, dir)
    rescue KuberKit::Shell::AbstractShell::ShellError => e
      ui.print_debug "BuildDirCleaner", "Error while removing directory: #{e.inspect}"
    end
  rescue KuberKit::Shell::AbstractShell::DirNotFoundError
    ui.print_debug "BuildDirCleaner", "Build directory not found"
  end

  private
    def get_ancient_builds_dirs(shell, parent_dir:)
      all_dirs  = shell.list_dirs("#{parent_dir}/*")

      skip_dirs = all_dirs
        .sort_by{ |f| bash_commands.ctime(shell, f) }
        .reverse[0...KEEP_DIRS_COUNT]

      all_dirs - skip_dirs
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kuber_kit-0.9.7 lib/kuber_kit/tools/build_dir_cleaner.rb