2021-01-12 16:27:25 -05:00
|
|
|
# typed: true
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-04-14 14:21:16 +01:00
|
|
|
require "system_command"
|
|
|
|
|
2021-01-13 11:16:09 -05:00
|
|
|
module Homebrew
|
|
|
|
# Helper functions for reading and writing settings.
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
module Settings
|
|
|
|
extend T::Sig
|
2021-04-14 14:21:16 +01:00
|
|
|
include SystemCommand::Mixin
|
2021-01-13 11:16:09 -05:00
|
|
|
|
|
|
|
module_function
|
|
|
|
|
|
|
|
sig { params(setting: T.any(String, Symbol), repo: Pathname).returns(T.nilable(String)) }
|
|
|
|
def read(setting, repo: HOMEBREW_REPOSITORY)
|
|
|
|
return unless (repo/".git/config").exist?
|
|
|
|
|
2021-04-22 20:46:51 +02:00
|
|
|
system_command("git", args: ["config", "--get", "homebrew.#{setting}"], chdir: repo).stdout.chomp.presence
|
2021-01-12 16:27:25 -05:00
|
|
|
end
|
|
|
|
|
2021-01-13 11:16:09 -05:00
|
|
|
sig { params(setting: T.any(String, Symbol), value: T.any(String, T::Boolean), repo: Pathname).void }
|
|
|
|
def write(setting, value, repo: HOMEBREW_REPOSITORY)
|
|
|
|
return unless (repo/".git/config").exist?
|
2021-01-12 16:27:25 -05:00
|
|
|
|
2021-04-22 20:46:51 +02:00
|
|
|
value = value.to_s
|
|
|
|
|
|
|
|
return if read(setting, repo: repo) == value
|
|
|
|
|
|
|
|
system_command! "git", args: ["config", "--replace-all", "homebrew.#{setting}", value], chdir: repo
|
2021-01-12 16:27:25 -05:00
|
|
|
end
|
|
|
|
|
2021-01-13 11:16:09 -05:00
|
|
|
sig { params(setting: T.any(String, Symbol), repo: Pathname).void }
|
|
|
|
def delete(setting, repo: HOMEBREW_REPOSITORY)
|
|
|
|
return unless (repo/".git/config").exist?
|
2021-04-22 20:46:51 +02:00
|
|
|
|
2021-01-13 11:16:09 -05:00
|
|
|
return if read(setting, repo: repo).blank?
|
2021-01-12 16:27:25 -05:00
|
|
|
|
2021-04-22 20:46:51 +02:00
|
|
|
system_command! "git", args: ["config", "--unset-all", "homebrew.#{setting}"], chdir: repo
|
2021-01-12 16:27:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|