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/locopy.rb similarity index 59% rename from locopy/bin/locopy.rb rename to 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'] -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