Class: Patcmd::Configuration

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

Constant Summary collapse

CONFIG_PATH =
File.expand_path("~/.patcmd/config.yml")

Class Method Summary collapse

Class Method Details

.add_task(name, task_data) ⇒ Object



48
49
50
51
52
53
# File 'lib/patcmd/configuration.rb', line 48

def add_task(name, task_data)
  config = load_config
  config["tasks"] ||= {}
  config["tasks"][name] = task_data
  File.write(CONFIG_PATH, config.to_yaml)
end

.create_defaultObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/patcmd/configuration.rb', line 15

def create_default
  default_config = {
    "tasks" => {
      "hello" => {
        "command" => "bash",
        "args" => ["-c", "'echo \"$GREETING, $TARGET! Args: $ARG1, $ARG2\"'"],
        "env" => {
          "GREETING" => "Hello",
          "TARGET" => "World",
          "ARG1" => "Foo",
          "ARG2" => "Bar",
        },
        "description" => "Print Hello World with multiple arguments and environment variables",
        "group" => "default",
      },
    },
  }
  File.write(CONFIG_PATH, default_config.to_yaml)
end

.exists?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/patcmd/configuration.rb', line 11

def exists?
  File.exist?(CONFIG_PATH)
end

.load_configObject



35
36
37
38
39
40
41
42
# File 'lib/patcmd/configuration.rb', line 35

def load_config
  if exists?
    YAML.load_file(CONFIG_PATH) || { "tasks" => {} }
  else
    create_default
    load_config
  end
end

.load_tasksObject



44
45
46
# File 'lib/patcmd/configuration.rb', line 44

def load_tasks
  load_config["tasks"] || {}
end