Class: Cli::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/cli/configuration.rb

Constant Summary collapse

FILE_NAME =
".pear-programmer.yml"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cli/configuration.rb', line 35

def initialize
    @root = Dir.pwd
    @configuration_file_path = File.join(@root, FILE_NAME)
    if !File.exist?(@configuration_file_path)
        raise "Pear Programmer configuration file does not exist, please run 'pear-on init' or switch to working directory"
    end
    @configuration_file = YAML.load_file(@configuration_file_path)

    # validations
    if @configuration_file["auth"]&.[]("api_key").nil? || @configuration_file["auth"]["api_key"].empty?
        raise "Pear Programmer api key is missing. Please add your api key to #{@configuration_file_path}"
    end
end

Instance Attribute Details

#rootObject

Returns the value of attribute root.



34
35
36
# File 'lib/cli/configuration.rb', line 34

def root
  @root
end

Class Method Details

.create(context, api_key, python_command) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cli/configuration.rb', line 7

def self.create(context, api_key, python_command)
    if !["python", "python2", "python3", ""].include?(python_command)
        raise "Invalid python command, please choose python, python2, or python3"
    end
    
    # the version of the configuration file, not cli version
    config = {
        "version" => 1.0,
        "project_settings" => {
            "context" => context,
        },
        "auth" => {
            "api_key" => api_key,
        }
    }

    if !python_command.empty?
        config["commands"] = {
            "python" => python_command
        }
    end

    File.open(File.join(Dir.pwd, FILE_NAME), "w") do |file|
        file.write(config.to_yaml)
    end
end

Instance Method Details

#api_keyObject



58
59
60
# File 'lib/cli/configuration.rb', line 58

def api_key
    @configuration_file["auth"]["api_key"]
end

#default_contextObject



62
63
64
# File 'lib/cli/configuration.rb', line 62

def default_context
    @configuration_file["project_settings"]["context"]
end

#python_commandObject



49
50
51
52
53
54
55
56
# File 'lib/cli/configuration.rb', line 49

def python_command
    command = @configuration_file&.[]("commands")&.[]("python")
    if command.nil? || command.empty?
        nil
    else
        command
    end
end