Class: Aias::EnvFile

Inherits:
Object
  • Object
show all
Includes:
BlockParser
Defined in:
lib/aias/env_file.rb

Overview

Manages a shell env file (~/.config/aia/schedule/env.sh) using a BEGIN/END block so user content above or below the block is preserved.

The file is sourced by every cron entry before the aia command, giving scheduled jobs a controlled PATH and all necessary env vars (API keys, AIA_PROMPTS__DIR, etc.) without relying on crontab env vars or a login shell that would reset PATH via path_helper.

Constant Summary collapse

BLOCK_OPEN =
"# BEGIN aias-env"
BLOCK_CLOSE =
"# END aias-env"

Instance Method Summary collapse

Constructor Details

#initialize(path: Paths::SCHEDULE_ENV) ⇒ EnvFile

Returns a new instance of EnvFile.



18
19
20
# File 'lib/aias/env_file.rb', line 18

def initialize(path: Paths::SCHEDULE_ENV)
  @path = path
end

Instance Method Details

#current_blockObject

Returns the raw content of the managed block (markers excluded). Returns an empty string when no block exists.



45
46
47
# File 'lib/aias/env_file.rb', line 45

def current_block
  extract_block(read, BLOCK_OPEN, BLOCK_CLOSE)
end

#install(env_vars) ⇒ Object

Writes env_vars into the managed block (merge — new values win on conflict). env_vars is a Hash of { "KEY" => "value" }. Chmod 0600 is applied by write so API keys are never world-readable.



25
26
27
28
29
30
# File 'lib/aias/env_file.rb', line 25

def install(env_vars)
  FileUtils.mkdir_p(File.dirname(@path), mode: 0o700)
  merged = parse_block(current_block).merge(env_vars)
  lines  = merged.map { |k, v| "export #{k}=\"#{v}\"" }
  write(replace_block(read, lines))
end

#uninstallObject

Removes the managed block from the file. Deletes the file entirely when no other content remains.



34
35
36
37
38
39
40
41
# File 'lib/aias/env_file.rb', line 34

def uninstall
  content = strip_block(read, BLOCK_OPEN, BLOCK_CLOSE)
  if content.strip.empty?
    File.delete(@path) if File.exist?(@path)
  else
    write(content)
  end
end