Module: Modal::Config

Defined in:
lib/modal/config.rb

Constant Summary collapse

CONFIG_FILE =
File.join(Dir.home, ".modal.toml")

Class Method Summary collapse

Class Method Details

.environment_name(environment = nil) ⇒ Object



56
57
58
# File 'lib/modal/config.rb', line 56

def self.environment_name(environment = nil)
  environment || profile[:environment] || ""
end

.get_profile(profile_name = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/modal/config.rb', line 18

def self.get_profile(profile_name = nil)
  config = read_config_file

  profile_name ||= ENV["MODAL_PROFILE"]
  unless profile_name
    config.each do |name, data|
      if data["active"]
        profile_name = name
        break
      end
    end
  end

  if profile_name && !config.key?(profile_name)
    raise "Profile \"#{profile_name}\" not found in .modal.toml. Please set the MODAL_PROFILE environment variable or specify a valid profile." unless config.empty?
  end

  profile_data = profile_name ? (config[profile_name] || {}) : {}

  server_url = ENV["MODAL_SERVER_URL"] || profile_data["server_url"] || "https://api.modal.com"
  token_id = ENV["MODAL_TOKEN_ID"] || profile_data["token_id"]
  token_secret = ENV["MODAL_TOKEN_SECRET"] || profile_data["token_secret"]
  environment = ENV["MODAL_ENVIRONMENT"] || profile_data["environment"]
  image_builder_version = ENV["MODAL_IMAGE_BUILDER_VERSION"] || profile_data["image_builder_version"] || "2024.10"

  unless token_id && token_secret
    raise "Profile \"#{profile_name}\" is missing token_id or token_secret. Please set them in .modal.toml or as environment variables."
  end

  {
    server_url: server_url,
    token_id: token_id,
    token_secret: token_secret,
    environment: environment,
    image_builder_version: image_builder_version
  }
end

.image_builder_version(version = nil) ⇒ Object



60
61
62
# File 'lib/modal/config.rb', line 60

def self.image_builder_version(version = nil)
  version || profile[:image_builder_version] || "2024.10"
end

.profileObject



65
66
67
# File 'lib/modal/config.rb', line 65

def self.profile
  @profile
end

.read_config_fileObject



8
9
10
11
12
13
14
15
16
# File 'lib/modal/config.rb', line 8

def self.read_config_file
  if File.exist?(CONFIG_FILE)
    TomlRB.parse(File.read(CONFIG_FILE))
  else
    {}
  end
rescue => e
  raise "Failed to read or parse .modal.toml: #{e.message}"
end