Class: TrakFlow::CLI::ConfigCommands

Inherits:
Thor
  • Object
show all
Defined in:
lib/trak_flow/cli/config_commands.rb

Overview

Config subcommands

Constant Summary collapse

XDG_CONFIG_PATH =
File.expand_path("~/.config/trak_flow/trak_flow.yml").freeze
PROJECT_CONFIG_PATH =
".trak_flow/config.yml"

Instance Method Summary collapse

Instance Method Details

#defaultsObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/trak_flow/cli/config_commands.rb', line 40

def defaults
  defaults_content = File.read(Config::DEFAULTS_PATH)

  if options[:json]
    yaml_data = YAML.safe_load(defaults_content, permitted_classes: [Symbol], symbolize_names: true)
    puts Oj.dump(yaml_data, mode: :compat, indent: 2)
  else
    puts pastel.bold("# TrakFlow Bundled Defaults")
    puts pastel.dim("# #{Config::DEFAULTS_PATH}")
    puts ""
    puts defaults_content
  end
end

#get(key) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/trak_flow/cli/config_commands.rb', line 81

def get(key)
  value = get_nested_value(key)

  if value.nil?
    output({ key: key, value: nil, error: "Key not found" }) do
      puts pastel.red("Key '#{key}' not found in configuration")
    end
    return
  end

  output({ key: key, value: serialize_value(value) }) do
    if value.is_a?(TrakFlow::ConfigSection)
      puts value.to_h.to_yaml.lines[1..].join
    else
      puts value
    end
  end
end

#pathObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/trak_flow/cli/config_commands.rb', line 136

def path
  project_path = File.expand_path(PROJECT_CONFIG_PATH)
  project_exists = File.exist?(project_path)
  xdg_exists = File.exist?(XDG_CONFIG_PATH)

  paths = {
    defaults: Config::DEFAULTS_PATH,
    xdg: { path: XDG_CONFIG_PATH, exists: xdg_exists },
    project: { path: project_path, exists: project_exists },
    active: find_active_config_path
  }

  output(paths) do
    puts "Configuration paths:"
    puts "  Defaults:  #{Config::DEFAULTS_PATH}"
    puts "  XDG:       #{XDG_CONFIG_PATH} #{xdg_exists ? pastel.green('(exists)') : pastel.dim('(not found)')}"
    puts "  Project:   #{project_path} #{project_exists ? pastel.green('(exists)') : pastel.dim('(not found)')}"
    puts ""
    puts "Active config: #{find_active_config_path || pastel.dim('(defaults only)')}"
  end
end

#resetObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/trak_flow/cli/config_commands.rb', line 60

def reset
  target_path = determine_config_path(options[:global])
  target_dir = File.dirname(target_path)

  if File.exist?(target_path) && !options[:force]
    output({ success: false, error: "Config file already exists. Use --force to overwrite." }) do
      puts pastel.red("Config file already exists at #{target_path}")
      puts "Use --force to overwrite"
    end
    return
  end

  FileUtils.mkdir_p(target_dir)
  FileUtils.cp(Config::DEFAULTS_PATH, target_path)

  output({ success: true, path: target_path }) do
    puts pastel.green("Configuration reset to defaults at #{target_path}")
  end
end

#set(key, value) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/trak_flow/cli/config_commands.rb', line 101

def set(key, value)
  config_path = find_writable_config_path

  # Load existing config or create new
  config_data = if File.exist?(config_path)
                  YAML.safe_load_file(config_path, permitted_classes: [Symbol], symbolize_names: true) || {}
                else
                  {}
                end

  # Ensure defaults section exists
  config_data[:defaults] ||= {}

  # Parse the value (convert to appropriate type)
  parsed_value = parse_value(value)

  # Set the nested value
  set_nested_value(config_data[:defaults], key, parsed_value)

  # Ensure directory exists
  FileUtils.mkdir_p(File.dirname(config_path))

  # Write the config file
  File.write(config_path, config_data.to_yaml)

  # Reset config to pick up new values
  TrakFlow.reset_config!

  output({ key: key, value: parsed_value, path: config_path }) do
    puts pastel.green("Set #{key} = #{parsed_value}")
    puts "Configuration saved to #{config_path}"
  end
end

#showObject



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
# File 'lib/trak_flow/cli/config_commands.rb', line 13

def show
  active_path = find_active_config_path

  if active_path.nil?
    output({ error: "No config file found", paths: { xdg: XDG_CONFIG_PATH, project: PROJECT_CONFIG_PATH } }) do
      puts pastel.yellow("No configuration file found.")
      puts "Create one with: tf config reset"
      puts ""
      puts "Expected locations:"
      puts "  XDG:     #{XDG_CONFIG_PATH}"
      puts "  Project: #{File.expand_path(PROJECT_CONFIG_PATH)}"
    end
    return
  end

  config_content = File.read(active_path)

  if options[:json]
    yaml_data = YAML.safe_load(config_content, permitted_classes: [Symbol], symbolize_names: true)
    puts Oj.dump({ path: active_path, config: yaml_data }, mode: :compat, indent: 2)
  else
    puts pastel.bold("# #{active_path}")
    puts config_content
  end
end