locopy: Require all arguments
This commit is contained in:
@@ -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'
|
||||
@@ -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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user