Sha256: d73a8df2a742f60bf13dd5e0dff436cb8773893265dbbd56334e61ade57be4c5

Contents?: true

Size: 1.35 KB

Versions: 5

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

require 'rake'
require 'yaml'

module KubsCLI
  # Used for copying files within kubs_cli
  class FileHelper
    # Copies a file or directory from one spot to another
    # @param from [Dir, File] Where to copy from
    # @param to [Dir, File] Where to copy to
    # @return void
    def copy(from:, to:)
      return if file_does_not_exist(from)

      to_dir = File.dirname(File.expand_path(to))
      Rake.mkdir_p(to_dir) unless Dir.exist?(to_dir)
      return Rake.cp(from, to) unless File.directory?(from)

      Rake.mkdir_p(to) unless Dir.exist?(to)
      Rake.cp_r(Dir["#{from}/*"], to)
    end

    # Creates dirs using Rake.mkdir_p if it does not exist
    # @param dirs [Array<String>] The names of the dirs to create
    # @return void
    def mkdirs(*dirs)
      dirs.flatten.each { |dir| Rake.mkdir_p(dir) unless Dir.exist?(dir) }
    end

    # Loads a YAML file
    # @param file [File] Yaml formatted file
    # @return [Hash] Returns a hash from a yaml document
    def load_yaml(file)
      YAML.load_file(file)
    rescue StandardError => e
      KubsCLI.add_error(e: e, msg: "Unable to parse your YAML file - #{file}")
    end

    private

    #:nodoc:
    def file_does_not_exist(file)
      return false if File.exist?(file)

      KubsCLI.add_error(e: KubsCLI::Error, msg: "#{file} does not exist")
      true
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
kubs_cli-0.1.7 lib/kubs_cli/file_helper.rb
kubs_cli-0.1.6 lib/kubs_cli/file_helper.rb
kubs_cli-0.1.5 lib/kubs_cli/file_helper.rb
kubs_cli-0.1.4 lib/kubs_cli/file_helper.rb
kubs_cli-0.1.2 lib/kubs_cli/file_helper.rb