cmd: use CLI::Parser.

Migrate all commands to use CLI::Parser.
master
Mike McQuaid 2020-02-03 16:37:23 +01:00
parent 7957895140
commit 6399ee19cb
4 changed files with 224 additions and 188 deletions

61
cmd/aspell-dictionaries.rb Executable file
View File

@ -0,0 +1,61 @@
require "cli/parser"
require "open-uri"
require "resource"
require "formula"
module Homebrew
module_function
def aspell_dictionaries_args
Homebrew::CLI::Parser.new do
usage_banner <<~EOS
`aspell-dictionaries`
Generates the new dictionaries for the `aspell` formula.
EOS
end
end
def aspell_dictionaries
aspell_dictionaries_args.parse
dict_url = "https://ftp.gnu.org/gnu/aspell/dict"
dict_mirror = "https://ftpmirror.gnu.org/aspell/dict"
languages = {}
URI.parse("#{dict_url}/0index.html").open do |content|
content.each_line do |line|
break if %r{^</table}.match?(line)
next unless /^<tr><td><a/.match?(line)
fields = line.split('"')
lang = fields[1]
path = fields[3]
lang.tr!("-", "_")
languages[lang] = path
end
end
resources = languages.map do |lang, path|
r = Resource.new(lang)
r.owner = Formulary.factory("aspell")
r.url "#{dict_url}/#{path}"
r.mirror "#{dict_mirror}/#{path}"
r
end
resources.each { |r| r.fetch(verify_download_integrity: false) }
resources.each do |r|
puts <<-EOS
resource "#{r.name}" do
url "#{r.url}"
mirror "#{r.mirrors.first}"
sha256 "#{r.cached_download.sha256}"
end
EOS
end
end
end

View File

@ -1,45 +0,0 @@
#: * `aspell-dictionaries`:
#: Generates the new dictionaries for the `aspell` formula.
require "open-uri"
require "resource"
require "formula"
dict_url = "https://ftp.gnu.org/gnu/aspell/dict"
dict_mirror = "https://ftpmirror.gnu.org/aspell/dict"
languages = {}
URI.parse("#{dict_url}/0index.html").open do |content|
content.each_line do |line|
break if %r{^</table}.match?(line)
next unless /^<tr><td><a/.match?(line)
fields = line.split('"')
lang = fields[1]
path = fields[3]
lang.tr!("-", "_")
languages[lang] = path
end
end
resources = languages.map do |lang, path|
r = Resource.new(lang)
r.owner = Formulary.factory("aspell")
r.url "#{dict_url}/#{path}"
r.mirror "#{dict_mirror}/#{path}"
r
end
resources.each { |r| r.fetch(verify_download_integrity: false) }
resources.each do |r|
puts <<-EOS
resource "#{r.name}" do
url "#{r.url}"
mirror "#{r.mirrors.first}"
sha256 "#{r.cached_download.sha256}"
end
EOS
end

View File

@ -1,143 +0,0 @@
#: * `postgresql-upgrade-database`:
#: Upgrades the database for the `postgresql` formula.
name = "postgresql"
pg = Formula[name]
bin = pg.bin
var = pg.var
version = pg.version
pg_version_file = var/"postgres/PG_VERSION"
pg_version_installed = version.to_s[/^\d+/]
pg_version_data = pg_version_file.read.chomp
if pg_version_installed == pg_version_data
odie <<~EOS
#{name} data already upgraded!
EOS
end
datadir = var/"postgres"
old_datadir = var/"postgres.old"
if old_datadir.exist?
odie <<~EOS
#{old_datadir} already exists!
Remove it if you want to upgrade data automatically.
EOS
end
old_pg_name = "#{name}@#{pg_version_data}"
old_pg_glob = "#{HOMEBREW_PREFIX}/Cellar/#{old_pg_name}/#{pg_version_data}.*/bin"
old_bin = Pathname.glob(old_pg_glob).first
old_bin ||= begin
Formula[old_pg_name]
ohai "brew install #{old_pg_name}"
system "brew", "install", old_pg_name
Pathname.glob(old_pg_glob).first
rescue FormulaUnavailableError
nil
end
odie "No #{name} #{pg_version_data}.* version installed!" unless old_bin
server_stopped = false
moved_data = false
initdb_run = false
upgraded = false
begin
# Following instructions from:
# https://www.postgresql.org/docs/10/static/pgupgrade.html
ohai "Upgrading #{name} data from #{pg_version_data} to #{pg_version_installed}..."
if /#{name}\s+started/.match?(Utils.popen_read("brew", "services", "list"))
system "brew", "services", "stop", name
service_stopped = true
elsif quiet_system "#{bin}/pg_ctl", "-D", datadir, "status"
system "#{bin}/pg_ctl", "-D", datadir, "stop"
server_stopped = true
end
# Shut down old server if it is up via brew services
if /#{old_pg_name}\s+started/.match?(Utils.popen_read("brew", "services", "list"))
system "brew", "services", "stop", old_pg_name
end
# get 'lc_collate' from old DB"
unless quiet_system "#{old_bin}/pg_ctl", "-w", "-D", datadir, "status"
system "#{old_bin}/pg_ctl", "-w", "-D", datadir, "start"
end
initdb_args = []
locale_settings = %w[
lc_collate
lc_ctype
lc_messages
lc_monetary
lc_numeric
lc_time
server_encoding
]
locale_settings.each do |setting|
sql = "SELECT setting FROM pg_settings WHERE name LIKE '#{setting}';"
value = Utils.popen_read("#{old_bin}/psql", "postgres", "-qtAX", "-U", ENV["USER"], "-c", sql).strip
next if value.empty?
if setting == "server_encoding"
initdb_args += ["-E #{value}"]
else
initdb_args += ["--#{setting.tr!("_", "-")}=#{value}"]
end
end
if quiet_system "#{old_bin}/pg_ctl", "-w", "-D", datadir, "status"
system "#{old_bin}/pg_ctl", "-w", "-D", datadir, "stop"
end
ohai "Moving #{name} data from #{datadir} to #{old_datadir}..."
FileUtils.mv datadir, old_datadir
moved_data = true
(var/"postgres").mkpath
ohai "Creating database..."
safe_system "#{bin}/initdb", *initdb_args, "#{var}/postgres"
initdb_run = true
ohai "Migrating and upgrading data..."
(var/"log").cd do
safe_system "#{bin}/pg_upgrade",
"-r",
"-b", old_bin,
"-B", bin,
"-d", old_datadir,
"-D", datadir,
"-j", Hardware::CPU.cores.to_s
end
upgraded = true
ohai "Upgraded #{name} data from #{pg_version_data} to #{pg_version_installed}!"
ohai "Your #{name} #{pg_version_data} data remains at #{old_datadir}"
ensure
if upgraded
if server_stopped
safe_system "#{bin}/pg_ctl", "-D", datadir, "start"
elsif service_stopped
safe_system "brew", "services", "start", name
end
else
onoe "Upgrading #{name} data from #{pg_version_data} to #{pg_version_installed} failed!"
if initdb_run
ohai "Removing empty #{name} initdb database..."
FileUtils.rm_r datadir
end
if moved_data
ohai "Moving #{name} data back from #{old_datadir} to #{datadir}..."
FileUtils.mv old_datadir, datadir
end
if server_stopped
system "#{bin}/pg_ctl", "-D", datadir, "start"
elsif service_stopped
system "brew", "services", "start", name
end
end
end

View File

@ -0,0 +1,163 @@
require "cli/parser"
require "open-uri"
require "resource"
require "formula"
module Homebrew
module_function
def postgresql_upgrade_database_args
Homebrew::CLI::Parser.new do
usage_banner <<~EOS
`postgresql-upgrade-database`
Upgrades the database for the `postgresql` formula.
EOS
end
end
def postgresql_upgrade_database
postgresql_upgrade_database_args.parse
name = "postgresql"
pg = Formula[name]
bin = pg.bin
var = pg.var
version = pg.version
pg_version_file = var/"postgres/PG_VERSION"
pg_version_installed = version.to_s[/^\d+/]
pg_version_data = pg_version_file.read.chomp
if pg_version_installed == pg_version_data
odie <<~EOS
#{name} data already upgraded!
EOS
end
datadir = var/"postgres"
old_datadir = var/"postgres.old"
if old_datadir.exist?
odie <<~EOS
#{old_datadir} already exists!
Remove it if you want to upgrade data automatically.
EOS
end
old_pg_name = "#{name}@#{pg_version_data}"
old_pg_glob = "#{HOMEBREW_PREFIX}/Cellar/#{old_pg_name}/#{pg_version_data}.*/bin"
old_bin = Pathname.glob(old_pg_glob).first
old_bin ||= begin
Formula[old_pg_name]
ohai "brew install #{old_pg_name}"
system "brew", "install", old_pg_name
Pathname.glob(old_pg_glob).first
rescue FormulaUnavailableError
nil
end
odie "No #{name} #{pg_version_data}.* version installed!" unless old_bin
server_stopped = false
moved_data = false
initdb_run = false
upgraded = false
begin
# Following instructions from:
# https://www.postgresql.org/docs/10/static/pgupgrade.html
ohai "Upgrading #{name} data from #{pg_version_data} to #{pg_version_installed}..."
if /#{name}\s+started/.match?(Utils.popen_read("brew", "services", "list"))
system "brew", "services", "stop", name
service_stopped = true
elsif quiet_system "#{bin}/pg_ctl", "-D", datadir, "status"
system "#{bin}/pg_ctl", "-D", datadir, "stop"
server_stopped = true
end
# Shut down old server if it is up via brew services
if /#{old_pg_name}\s+started/.match?(Utils.popen_read("brew", "services", "list"))
system "brew", "services", "stop", old_pg_name
end
# get 'lc_collate' from old DB"
unless quiet_system "#{old_bin}/pg_ctl", "-w", "-D", datadir, "status"
system "#{old_bin}/pg_ctl", "-w", "-D", datadir, "start"
end
initdb_args = []
locale_settings = %w[
lc_collate
lc_ctype
lc_messages
lc_monetary
lc_numeric
lc_time
server_encoding
]
locale_settings.each do |setting|
sql = "SELECT setting FROM pg_settings WHERE name LIKE '#{setting}';"
value = Utils.popen_read("#{old_bin}/psql", "postgres", "-qtAX", "-U", ENV["USER"], "-c", sql).strip
next if value.empty?
if setting == "server_encoding"
initdb_args += ["-E #{value}"]
else
initdb_args += ["--#{setting.tr!("_", "-")}=#{value}"]
end
end
if quiet_system "#{old_bin}/pg_ctl", "-w", "-D", datadir, "status"
system "#{old_bin}/pg_ctl", "-w", "-D", datadir, "stop"
end
ohai "Moving #{name} data from #{datadir} to #{old_datadir}..."
FileUtils.mv datadir, old_datadir
moved_data = true
(var/"postgres").mkpath
ohai "Creating database..."
safe_system "#{bin}/initdb", *initdb_args, "#{var}/postgres"
initdb_run = true
ohai "Migrating and upgrading data..."
(var/"log").cd do
safe_system "#{bin}/pg_upgrade",
"-r",
"-b", old_bin,
"-B", bin,
"-d", old_datadir,
"-D", datadir,
"-j", Hardware::CPU.cores.to_s
end
upgraded = true
ohai "Upgraded #{name} data from #{pg_version_data} to #{pg_version_installed}!"
ohai "Your #{name} #{pg_version_data} data remains at #{old_datadir}"
ensure
if upgraded
if server_stopped
safe_system "#{bin}/pg_ctl", "-D", datadir, "start"
elsif service_stopped
safe_system "brew", "services", "start", name
end
else
onoe "Upgrading #{name} data from #{pg_version_data} to #{pg_version_installed} failed!"
if initdb_run
ohai "Removing empty #{name} initdb database..."
FileUtils.rm_r datadir
end
if moved_data
ohai "Moving #{name} data back from #{old_datadir} to #{datadir}..."
FileUtils.mv old_datadir, datadir
end
if server_stopped
system "#{bin}/pg_ctl", "-D", datadir, "start"
elsif service_stopped
system "brew", "services", "start", name
end
end
end
end
end