Class: Bruh::Config

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/bruh/config.rb

Overview

Manages configuration for Bruh

Constant Summary collapse

CONFIG_DIR =
T.let(File.expand_path('~/.config/bruh').freeze, String)
CONFIG_FILE =
T.let(File.join(CONFIG_DIR, 'config.toml').freeze, String)

Class Method Summary collapse

Class Method Details

.ensure_config_existsObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/bruh/config.rb', line 90

def self.ensure_config_exists
  FileUtils.mkdir_p(CONFIG_DIR) unless Dir.exist?(CONFIG_DIR)

  unless File.exist?(CONFIG_FILE)
    default_config = {
      hackage_username: '',
      hackage_password: '',
      github_token: '',
      default_formula_template: 'standard',
      bottle_platforms: ['arm64_monterey']
    }

    File.write(CONFIG_FILE, TomlRB.dump(default_config))
  end

  nil
end

.get(key) ⇒ Object



50
51
52
53
# File 'lib/bruh/config.rb', line 50

def self.get(key)
  config = load
  config[key]
end

.loadObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/bruh/config.rb', line 17

def self.load
  ensure_config_exists

  begin
    config = TomlRB.load_file(CONFIG_FILE)
    symbolize_keys(config)
  rescue StandardError => e
    puts "Error loading config: #{e.message}"
    {}
  end
end

.save(config) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bruh/config.rb', line 30

def self.save(config)
  ensure_config_exists

  begin
    File.write(CONFIG_FILE, TomlRB.dump(stringify_keys(config)))
    true
  rescue StandardError => e
    puts "Error saving config: #{e.message}"
    false
  end
end

.set(key, value) ⇒ Object



43
44
45
46
47
# File 'lib/bruh/config.rb', line 43

def self.set(key, value)
  config = load
  config[key] = value
  save(config)
end

.setup_credentials(interactive: true) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/bruh/config.rb', line 56

def self.setup_credentials(interactive: true)
  config = load

  if interactive
    # Prompt for credentials
    puts 'Setting up Hackage credentials:'
    print 'Hackage username: '
    username = gets.chomp

    print 'Hackage password (input will be hidden): '
    system('stty -echo')
    password = gets.chomp
    system('stty echo')
    puts

    puts 'Setting up GitHub credentials:'
    print 'GitHub token (for bottle uploads): '
    system('stty -echo')
    github_token = gets.chomp
    system('stty echo')
    puts

    # Save credentials
    config[:hackage_username] = username
    config[:hackage_password] = password
    config[:github_token] = github_token

    save(config)
  end

  config
end

.stringify_keys(hash) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/bruh/config.rb', line 122

def self.stringify_keys(hash)
  result = {}
  hash.each do |key, value|
    result[key.to_s] = if value.is_a?(Hash)
                         stringify_keys(value)
                       else
                         value
                       end
  end
  result
end

.symbolize_keys(hash) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/bruh/config.rb', line 109

def self.symbolize_keys(hash)
  result = {}
  hash.each do |key, value|
    result[key.to_sym] = if value.is_a?(Hash)
                           symbolize_keys(value)
                         else
                           value
                         end
  end
  result
end