locopy: Require all arguments

This commit is contained in:
2025-11-17 16:54:55 +01:00
parent a3530666a6
commit 5e4c38c28e
6 changed files with 70 additions and 69 deletions

View File

@@ -2,6 +2,10 @@
source 'https://rubygems.org'
gem 'rubyzip', '~> 3.2'
gem 'pg', '~> 1.6'
gem "term-ansicolor", "~> 1.11"
gem 'mysql2', '~> 0.5'
gem 'rubyzip', '~> 3.2'
gem 'optparse', '~> 0.8.0'
gem 'term-ansicolor', '~> 1.11'

View File

@@ -3,13 +3,16 @@ GEM
specs:
bigdecimal (3.3.1)
mize (0.6.1)
mysql2 (0.5.7)
bigdecimal
optparse (0.8.0)
pg (1.6.2)
pg (1.6.2-x86_64-linux)
rubyzip (3.2.2)
sync (0.5.0)
term-ansicolor (1.11.3)
tins (~> 1)
tins (1.46.0)
tins (1.47.0)
bigdecimal
mize (~> 0.6)
sync
@@ -19,6 +22,8 @@ PLATFORMS
x86_64-linux
DEPENDENCIES
mysql2 (~> 0.5)
optparse (~> 0.8.0)
pg (~> 1.6)
rubyzip (~> 3.2)
term-ansicolor (~> 1.11)

View File

@@ -2,7 +2,7 @@ The repository contains a collection of random scripts and short programs.
# Requirements
- Ruby 3 for Ruby scripts in `bin/`.
- Ruby 3 for Ruby scripts.
- For Haskell: The GHC compiler and cabal build system. The programs can be then
built and run with `cabal run program-name -- --options`.

View File

@@ -1,8 +0,0 @@
# frozen_string_literal: true
source 'https://rubygems.org'
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
gem 'mysql2', '~> 0.5'
gem 'optparse', '~> 0.8.0'

View File

@@ -1,18 +0,0 @@
GEM
remote: https://rubygems.org/
specs:
bigdecimal (3.3.1)
mysql2 (0.5.7)
bigdecimal
optparse (0.8.0)
PLATFORMS
arm64-darwin-24
x86_64-linux
DEPENDENCIES
mysql2 (~> 0.5)
optparse (~> 0.8.0)
BUNDLED WITH
2.6.9

View File

@@ -3,35 +3,16 @@
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.
# frozen_string_literal: true
require 'pathname'
require 'mysql2'
require 'optparse'
require 'json'
require 'term/ansicolor'
# Tool for easy updating a local copy of a website
arguments = {}
option_parser = OptionParser.new do |options|
options.banner = "Usage: #{File.basename $0} [OPTIONS] (wordpress)"
options.on '--root=ROOT', 'Website configuration directory' do |option|
arguments[:root] = Pathname.new option
end
options.on '--dump=DUMP', 'Database file' do |option|
arguments[:root] = Pathname.new option
end
end
option_parser.parse!
# Read arguments
while argument = ARGV.shift do
case argument
when 'wordpress'
ENV['CMD'] = argument
end
end
class DatabaseAccess
attr_accessor :name, :user, :password
attr_reader :host, :socket, :port
@@ -57,14 +38,7 @@ class DatabaseAccess
end
end
wp_settings = Pathname.new('wp-settings.php').realpath
wp_config = nil
Dir.chdir arguments[:root] do
wp_config = JSON.parse `php #{wp_settings}`
end
def copy_db(root, wp_config)
def copy_db(wp_config)
table_prefix = wp_config['table_prefix']
php_constants = DatabaseAccess.new wp_config
keep_option_names = ['siteurl', 'home']
@@ -81,8 +55,8 @@ def copy_db(root, wp_config)
accumulator
end
keep_option_values = {
"siteurl"=>"http://localhost:8083",
"home"=>"http://localhost:8083"
'siteurl' => 'http://localhost:8083',
'home' => 'http://localhost:8083'
}
statement = client.prepare <<~SQL
@@ -95,11 +69,55 @@ def copy_db(root, wp_config)
end
end
case ENV['CMD']
when 'wordpress'
# Set permissions
copy_db(arguments[:root], wp_config)
else
puts option_parser.help
exit 1
class CommandOptions
attr_reader :root, :dump
def []=(key, value)
case key
when :root
@root = Pathname.new value
when :dump
@dump = Pathname.new value
end
end
def valid?
!@root.nil? && !@dump.nil?
end
end
def wordpress
arguments = CommandOptions.new
option_parser = OptionParser.new do |options|
options.banner = "Usage: #{File.basename $0} [OPTIONS] (wordpress)"
options.on '--root=ROOT', 'Website configuration directory'
options.on '--dump=DUMP', 'Database file'
end
option_parser.parse!(into: arguments)
unless arguments.valid?
$stderr.puts option_parser.help
exit 2
end
wp_settings = Pathname.new('locopy/wp-settings.php').realpath
wp_config = nil
Dir.chdir arguments.root do
wp_config = JSON.parse `php #{wp_settings}`
end
copy_db wp_config
end
# Check for supported command.
command = ARGV.shift
case command
when 'wordpress'
wordpress
when nil
Kernel.abort Term::ANSIColor.red "No command given at the command line."
else
Kernel.abort Term::ANSIColor.red %Q(Unsupported command "#{command}".)
end