Class: Wordmove::Movefile

Inherits:
Object
  • Object
show all
Defined in:
lib/wordmove/movefile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, start_dir = current_dir) ⇒ Movefile

Returns a new instance of Movefile.



5
6
7
8
9
# File 'lib/wordmove/movefile.rb', line 5

def initialize(name = nil, start_dir = current_dir)
  @logger = Logger.new(STDOUT).tap { |l| l.level = Logger::DEBUG }
  @name = name
  @start_dir = start_dir
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



3
4
5
# File 'lib/wordmove/movefile.rb', line 3

def logger
  @logger
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/wordmove/movefile.rb', line 3

def name
  @name
end

#start_dirObject (readonly)

Returns the value of attribute start_dir.



3
4
5
# File 'lib/wordmove/movefile.rb', line 3

def start_dir
  @start_dir
end

Instance Method Details

#environment(cli_options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/wordmove/movefile.rb', line 45

def environment(cli_options = {})
  options = fetch(false)
  available_enviroments = extract_available_envs(options)
  options.merge!(cli_options).deep_symbolize_keys!

  if options[:environment] != 'local'
    if available_enviroments.size > 1 && options[:environment].nil?
      raise(
        UndefinedEnvironment,
        "You need to specify an environment with --environment parameter"
      )
    end

    if options[:environment].present?
      unless available_enviroments.include?(options[:environment].to_sym)
        raise UndefinedEnvironment, "No environment found for \"#{options[:environment]}\". "\
                                    "Available Environments: #{available_enviroments.join(' ')}"
      end
    end
  end

  (options[:environment] || available_enviroments.first).to_sym
end

#fetch(verbose = true) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/wordmove/movefile.rb', line 11

def fetch(verbose = true)
  entries = if name.nil?
              Dir["#{File.join(start_dir, '{M,m}ovefile')}{,.yml,.yaml}"]
            else
              Dir["#{File.join(start_dir, name)}{,.yml,.yaml}"]
            end

  if entries.empty?
    if last_dir?(start_dir)
      raise MovefileNotFound, "Could not find a valid Movefile. Searched"\
                              " for filename \"#{name}\" in folder \"#{start_dir}\""
    end

    @start_dir = upper_dir(start_dir)
    return fetch(verbose)
  end

  found = entries.first
  logger.task("Using Movefile: #{found}") if verbose == true
  YAML.safe_load(ERB.new(File.read(found)).result, [], [], true).deep_symbolize_keys!
end

#load_dotenv(cli_options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/wordmove/movefile.rb', line 33

def load_dotenv(cli_options = {})
  env = environment(cli_options)
  env_files = Dir[File.join(start_dir, ".env{.#{env},}")]

  found_env = env_files.first

  return false unless found_env.present?

  logger.info("Using .env file: #{found_env}")
  Dotenv.load(found_env)
end

#secretsObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/wordmove/movefile.rb', line 69

def secrets
  options = fetch(false)

  secrets = []
  options.each_key do |env|
    secrets << options.dig(env, :database, :password)
    secrets << options.dig(env, :database, :host)
    secrets << options.dig(env, :vhost)
    secrets << options.dig(env, :ssh, :password)
    secrets << options.dig(env, :ssh, :host)
    secrets << options.dig(env, :ftp, :password)
    secrets << options.dig(env, :ftp, :host)
    secrets << options.dig(env, :wordpress_path)
  end

  secrets.compact.delete_if(&:empty?)
end