Module: ShapeupCli::Config

Defined in:
lib/shapeup_cli/config.rb

Constant Summary collapse

CONFIG_DIR =
File.join(Dir.home, ".config", "shapeup")
PROFILES_FILE =
File.join(CONFIG_DIR, "profiles.json")
CONFIG_FILE =
File.join(CONFIG_DIR, "config.json")
PROJECT_CONFIG_NAME =

Project-level config — walks up from current directory

".shapeup/config.json"

Class Method Summary collapse

Class Method Details

.authenticated?Boolean

— Auth status (for plugin hook) —

Returns:

  • (Boolean)


124
125
126
# File 'lib/shapeup_cli/config.rb', line 124

def self.authenticated?
  !!token
end

.clear_credentialsObject



78
79
80
# File 'lib/shapeup_cli/config.rb', line 78

def self.clear_credentials
  File.delete(PROFILES_FILE) if File.exist?(PROFILES_FILE)
end

.current_profileObject



69
70
71
72
73
74
75
76
# File 'lib/shapeup_cli/config.rb', line 69

def self.current_profile
  data = load_profiles_raw
  name = ENV["SHAPEUP_PROFILE"] || data["default"]
  return nil unless name
  profile = data.dig("profiles", name)
  return nil unless profile
  profile.merge("profile_name" => name)
end

.current_profile_nameObject



128
129
130
# File 'lib/shapeup_cli/config.rb', line 128

def self.current_profile_name
  ENV["SHAPEUP_PROFILE"] || load_profiles_raw["default"]
end

.delete_profile(name) ⇒ Object



53
54
55
56
57
58
# File 'lib/shapeup_cli/config.rb', line 53

def self.delete_profile(name)
  data = load_profiles_raw
  data["profiles"]&.delete(name)
  data["default"] = data["profiles"]&.keys&.first if data["default"] == name
  File.write(PROFILES_FILE, JSON.pretty_generate(data))
end

.ensure_config_dirObject



12
13
14
# File 'lib/shapeup_cli/config.rb', line 12

def self.ensure_config_dir
  FileUtils.mkdir_p(CONFIG_DIR)
end

.hostObject

Resolution order: env var > project config > global config > profile > default



104
105
106
# File 'lib/shapeup_cli/config.rb', line 104

def self.host
  ENV["SHAPEUP_HOST"] || load_config["host"] || current_profile&.dig("host") || ShapeupCli::DEFAULT_HOST
end

.list_profilesObject



60
61
62
63
64
65
66
67
# File 'lib/shapeup_cli/config.rb', line 60

def self.list_profiles
  data = load_profiles_raw
  default = data["default"]
  (data["profiles"] || {}).map do |key, profile|
    { name: key, display_name: profile["name"], organisation_id: profile["organisation_id"],
      host: profile["host"], default: key == default }
  end
end

.load_configObject



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/shapeup_cli/config.rb', line 91

def self.load_config
  data = load_config_raw

  # Merge project-level config (walks up directory tree, takes precedence)
  if (project_config = find_project_config)
    project = JSON.parse(File.read(project_config)) rescue {}
    data.merge!(project)
  end

  data
end

.organisation_idObject



108
109
110
# File 'lib/shapeup_cli/config.rb', line 108

def self.organisation_id
  ENV["SHAPEUP_ORG"] || load_config["organisation_id"] || current_profile&.dig("organisation_id")&.then { |v| v.empty? ? nil : v }
end

.piped?Boolean

— Pipe detection —

Returns:

  • (Boolean)


118
119
120
# File 'lib/shapeup_cli/config.rb', line 118

def self.piped?
  !$stdout.tty?
end

.save_config(key, value) ⇒ Object

— Config (defaults) —



84
85
86
87
88
89
# File 'lib/shapeup_cli/config.rb', line 84

def self.save_config(key, value)
  ensure_config_dir
  data = load_config_raw
  data[key] = value
  File.write(CONFIG_FILE, JSON.pretty_generate(data))
end

.save_profile(name, token:, host:, organisation_id:, display_name: nil) ⇒ Object

— Profiles —

profiles.json stores named profiles: {

"default": "compass-labs",
"profiles": {
  "acme-corp":   { "token": "...", "host": "https://shapeup.cc", "organisation_id": "2", "name": "Acme Corp" },
  "side-project": { "token": "...", "host": "https://shapeup.cc", "organisation_id": "5", "name": "Side Project" }
}

}



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/shapeup_cli/config.rb', line 27

def self.save_profile(name, token:, host:, organisation_id:, display_name: nil)
  ensure_config_dir
  data = load_profiles_raw
  data["profiles"] ||= {}
  data["profiles"][name] = {
    "token" => token,
    "host" => host,
    "organisation_id" => organisation_id.to_s,
    "name" => display_name || name
  }
  # Set as default if it's the first profile
  data["default"] ||= name
  File.write(PROFILES_FILE, JSON.pretty_generate(data))
  File.chmod(0600, PROFILES_FILE)
end

.switch_profile(name) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/shapeup_cli/config.rb', line 43

def self.switch_profile(name)
  data = load_profiles_raw
  unless data.dig("profiles", name)
    available = (data["profiles"] || {}).keys
    abort "Profile '#{name}' not found. Available: #{available.join(", ")}"
  end
  data["default"] = name
  File.write(PROFILES_FILE, JSON.pretty_generate(data))
end

.tokenObject



112
113
114
# File 'lib/shapeup_cli/config.rb', line 112

def self.token
  ENV["SHAPEUP_TOKEN"] || current_profile&.dig("token")
end