diff options
| author | Eugen Wissner <belka@caraus.de> | 2025-11-17 16:54:55 +0100 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2025-11-17 16:54:55 +0100 |
| commit | 5e4c38c28ecfd927a466aa128b9cb85a2e0e4ebc (patch) | |
| tree | 59b4ba2f104a9c357b711bfc315e90ea41e135ec /locopy | |
| parent | a3530666a60dd004fe2c28bb87fbabdf5e5bebab (diff) | |
| download | kazbek-5e4c38c28ecfd927a466aa128b9cb85a2e0e4ebc.tar.gz | |
locopy: Require all arguments
Diffstat (limited to 'locopy')
| -rw-r--r-- | locopy/Gemfile | 8 | ||||
| -rw-r--r-- | locopy/Gemfile.lock | 18 | ||||
| -rwxr-xr-x | locopy/locopy.rb (renamed from locopy/bin/locopy.rb) | 92 |
3 files changed, 55 insertions, 63 deletions
diff --git a/locopy/Gemfile b/locopy/Gemfile deleted file mode 100644 index 3cfb1a0..0000000 --- a/locopy/Gemfile +++ /dev/null @@ -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' diff --git a/locopy/Gemfile.lock b/locopy/Gemfile.lock deleted file mode 100644 index 0393bfd..0000000 --- a/locopy/Gemfile.lock +++ /dev/null @@ -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 diff --git a/locopy/bin/locopy.rb b/locopy/locopy.rb index f726210..0bd48a3 100755 --- a/locopy/bin/locopy.rb +++ b/locopy/locopy.rb @@ -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'] +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' - # Set permissions - copy_db(arguments[:root], wp_config) + wordpress +when nil + Kernel.abort Term::ANSIColor.red "No command given at the command line." else - puts option_parser.help - exit 1 + Kernel.abort Term::ANSIColor.red %Q(Unsupported command "#{command}".) end |
