Class: Kitsune::Kit::Workflows::EnvironmentSelection

Inherits:
Object
  • Object
show all
Defined in:
lib/kitsune/kit/workflows/environment_selection.rb

Constant Summary collapse

NAME =
/\A[a-z0-9][a-z0-9_-]*\z/

Instance Method Summary collapse

Constructor Details

#initialize(root: Dir.pwd) ⇒ EnvironmentSelection

Returns a new instance of EnvironmentSelection.



14
15
16
# File 'lib/kitsune/kit/workflows/environment_selection.rb', line 14

def initialize(root: Dir.pwd)
  @root = Pathname(root).expand_path
end

Instance Method Details

#current(env: ENV) ⇒ Object



22
23
24
# File 'lib/kitsune/kit/workflows/environment_selection.rb', line 22

def current(env: ENV)
  env["KITSUNE_ENV"] || selected_from_file || "development"
end

#listObject



18
19
20
# File 'lib/kitsune/kit/workflows/environment_selection.rb', line 18

def list
  environments_directory.glob("*.yml").map { |path| path.basename(".yml").to_s }.sort
end

#use(name) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/kitsune/kit/workflows/environment_selection.rb', line 26

def use(name)
  validate_name!(name)
  unless list.include?(name)
    raise Errors::ConfigurationError.new(
      "environment does not exist: #{name}",
      hint: "Create .kitsune/environments/#{name}.yml, then retry."
    )
  end

  FileUtils.mkdir_p(base_directory, mode: 0o700)
  Tempfile.create(["environment", ".tmp"], base_directory.to_s, mode: 0o600) do |file|
    file.write("#{name}\n")
    file.flush
    file.fsync
    File.rename(file.path, selection_path)
  end
  File.chmod(0o600, selection_path)
  name
end