From 5e4c38c28ecfd927a466aa128b9cb85a2e0e4ebc Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Mon, 17 Nov 2025 16:54:55 +0100 Subject: locopy: Require all arguments --- Gemfile | 8 +++- Gemfile.lock | 7 ++- README.md | 2 +- locopy/Gemfile | 8 ---- locopy/Gemfile.lock | 18 -------- locopy/bin/locopy.rb | 105 ------------------------------------------- locopy/locopy.rb | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 136 insertions(+), 135 deletions(-) delete mode 100644 locopy/Gemfile delete mode 100644 locopy/Gemfile.lock delete mode 100755 locopy/bin/locopy.rb create mode 100755 locopy/locopy.rb diff --git a/Gemfile b/Gemfile index 48768fa..a40894b 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index 7aa24da..cd95a11 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) diff --git a/README.md b/README.md index b30ff91..9e42e15 100644 --- a/README.md +++ b/README.md @@ -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`. 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/bin/locopy.rb deleted file mode 100755 index f726210..0000000 --- a/locopy/bin/locopy.rb +++ /dev/null @@ -1,105 +0,0 @@ -#!/usr/bin/env ruby -# This Source Code Form is subject to the terms of the Mozilla Public License, -# 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/. - -require 'pathname' -require 'mysql2' -require 'optparse' -require 'json' - -# 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 - - def initialize(wp_config) - @name = wp_config['DB_NAME'] - @user = wp_config['DB_USER'] - @password = wp_config['DB_PASSWORD'] - - self.host = wp_config['DB_HOST'] - end - - def host=(host) - host_part, socket_part = host.split(':') - port = socket_part.to_i - - @host = host_part - if port == 0 - @socket = socket_part - else - @port = port - end - 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) - table_prefix = wp_config['table_prefix'] - php_constants = DatabaseAccess.new wp_config - keep_option_names = ['siteurl', 'home'] - - client = Mysql2::Client.new host: php_constants.host, username: php_constants.user, - password: php_constants.password, database: php_constants.name, socket: php_constants.socket - statement = client.prepare <<~SQL - SELECT option_name, option_value - FROM #{table_prefix}options - WHERE option_name LIKE ? - SQL - keep_option_values = keep_option_names.each_with_object({}) do |keep_option_name, accumulator| - accumulator[keep_option_name] = statement.execute(keep_option_name).first['option_value'] - accumulator - end - keep_option_values = { - "siteurl"=>"http://localhost:8083", - "home"=>"http://localhost:8083" - } - - statement = client.prepare <<~SQL - UPDATE #{table_prefix}options - SET option_value = ? - WHERE option_name LIKE ? - SQL - keep_option_values.each_pair do |name, value| - statement.execute(value, name) - end -end - -case ENV['CMD'] -when 'wordpress' - # Set permissions - copy_db(arguments[:root], wp_config) -else - puts option_parser.help - exit 1 -end diff --git a/locopy/locopy.rb b/locopy/locopy.rb new file mode 100755 index 0000000..0bd48a3 --- /dev/null +++ b/locopy/locopy.rb @@ -0,0 +1,123 @@ +#!/usr/bin/env ruby +# This Source Code Form is subject to the terms of the Mozilla Public License, +# 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 + +class DatabaseAccess + attr_accessor :name, :user, :password + attr_reader :host, :socket, :port + + def initialize(wp_config) + @name = wp_config['DB_NAME'] + @user = wp_config['DB_USER'] + @password = wp_config['DB_PASSWORD'] + + self.host = wp_config['DB_HOST'] + end + + def host=(host) + host_part, socket_part = host.split(':') + port = socket_part.to_i + + @host = host_part + if port == 0 + @socket = socket_part + else + @port = port + end + end +end + +def copy_db(wp_config) + table_prefix = wp_config['table_prefix'] + php_constants = DatabaseAccess.new wp_config + keep_option_names = ['siteurl', 'home'] + + client = Mysql2::Client.new host: php_constants.host, username: php_constants.user, + password: php_constants.password, database: php_constants.name, socket: php_constants.socket + statement = client.prepare <<~SQL + SELECT option_name, option_value + FROM #{table_prefix}options + WHERE option_name LIKE ? + SQL + keep_option_values = keep_option_names.each_with_object({}) do |keep_option_name, accumulator| + accumulator[keep_option_name] = statement.execute(keep_option_name).first['option_value'] + accumulator + end + keep_option_values = { + 'siteurl' => 'http://localhost:8083', + 'home' => 'http://localhost:8083' + } + + statement = client.prepare <<~SQL + UPDATE #{table_prefix}options + SET option_value = ? + WHERE option_name LIKE ? + SQL + keep_option_values.each_pair do |name, value| + statement.execute(value, name) + end +end + +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 -- cgit v1.2.3