Module: AppConfig

Defined in:
lib/app_config.rb

Constant Summary collapse

CONFIG_FILE =
File.join(Dir.home, '.gpterm', 'config.yml').freeze

Class Method Summary collapse

Class Method Details

.add_openapi_key(config, openapi_key) ⇒ Object



58
59
60
61
# File 'lib/app_config.rb', line 58

def self.add_openapi_key(config, openapi_key)
  config['openapi_key'] = openapi_key
  save_config(config)
end

.add_preset(config, preset_name, preset_prompt) ⇒ Object



63
64
65
66
67
68
# File 'lib/app_config.rb', line 63

def self.add_preset(config, preset_name, preset_prompt)
  # This is a YAML file so we need to make sure the presets key exists
  config['presets'] ||= {}
  config['presets'][preset_name] = preset_prompt
  save_config(config)
end

.loadObject



9
10
11
12
13
14
15
16
17
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
# File 'lib/app_config.rb', line 9

def self.load
  # Check if the directory exists, if not, create it
  unless File.directory?(File.dirname(CONFIG_FILE))
    Dir.mkdir(File.dirname(CONFIG_FILE))
  end

  unless File.exist?(self::CONFIG_FILE)
    puts 'Welcome to gpterm! It looks like this is your first time using this application.'.colorize(:magenta)

    new_config = {}
    puts "Before we get started, we need to configure the application. All the info you provide will be saved in #{self::CONFIG_FILE}.".colorize(:magenta)

    puts "Enter your OpenAI API key's \"SECRET KEY\" value then hit return: ".colorize(:yellow)
    new_config['openapi_key'] = Input.non_empty

    puts "Your PATH environment variable is: #{ENV['PATH']}".colorize(:magenta)
    puts 'Are you happy for your PATH to be sent to OpenAI to help with command generation? (Y/n then hit return) '.colorize(:yellow)

    input = Input.yes_or_no

    if input == 'y'
      new_config['send_path'] = true
    else
      new_config['send_path'] = false
    end

    default_model = 'gpt-4-turbo-preview'

    puts "The default model is #{default_model}. If you would like to change it please enter the name of your preferred model:".colorize(:yellow)
    new_config['model'] = STDIN.gets.chomp.strip || default_model

    self.save_config(new_config)

    puts "Configuration saved to #{self::CONFIG_FILE}".colorize(:green)

    new_config
  else
    self.load_config_from_file
  end
end

.load_config_from_fileObject



50
51
52
# File 'lib/app_config.rb', line 50

def self.load_config_from_file
  YAML.load_file(CONFIG_FILE)
end

.save_config(config) ⇒ Object



54
55
56
# File 'lib/app_config.rb', line 54

def self.save_config(config)
  File.write(CONFIG_FILE, config.to_yaml)
end