diff --git a/bin/gingr-examples.sh b/bin/gingr-examples.sh index 2a69139..c318ff9 100755 --- a/bin/gingr-examples.sh +++ b/bin/gingr-examples.sh @@ -25,5 +25,5 @@ echo "publish vector to geoserver ends" # import all with a zipfile echo "4 - import all starting..." -ruby bin/gingr all spec/fixture/zipfile/test_public.zip +ruby bin/gingr all spec/fixture/zipfile/vector.zip --update-reference-field echo "import all ends" diff --git a/lib/gingr/cli.rb b/lib/gingr/cli.rb index b8c9368..c7ac0a2 100644 --- a/lib/gingr/cli.rb +++ b/lib/gingr/cli.rb @@ -1,10 +1,13 @@ # frozen_string_literal: true + require 'thor' require_relative 'import_util' +require_relative 'config' require_relative 'watcher' module Gingr class Cli < Thor + include Config include ImportUtil include Logging @@ -33,7 +36,7 @@ def watch(root_dir = nil) long_desc <<-TEXT, wrapping: false examples:\n 1) ruby bin/import solr tmp/test_public \n - 2) ruby bin/import solr tmp/test_public --no-update_reference_field \n + 2) ruby bin/import solr tmp/test_public --update-reference-field \n (it will update reference urls from 'dct_references_s' field in each geoblacklight json file \n with current spatial_url, geoserver_url, geoserver_secure_url) TEXT @@ -44,7 +47,7 @@ def watch(root_dir = nil) option :solr_url def solr(dir_path) reference_urls = ImportUtil.get_reference_urls(options) - solr_url = options[:solr_url] || ENV.fetch('SOLR_URL', nil) + solr_url = options[:solr_url] || ENV.fetch('SOLR_URL', Config.default_options[:solr_url]) ImportUtil.index_solr_from_dir(dir_path, solr_url, reference_urls) txt = "all json files under '#{dir_path}' and subdirectories have been indexed to solr #{solr_url} successfully" logger.info(txt) @@ -60,7 +63,13 @@ def solr(dir_path) option :is_public, type: :boolean, default: true def geoserver(filename) url = options[:geoserver_url] - url ||= options[:is_public] ? ENV.fetch('GEOSERVER_URL', nil) : ENV.fetch('GEOSERVER_SECURE_URL', nil) + url ||= if options[:is_public] + ENV.fetch('GEOSERVER_URL', Config.default_options[:geoserver_url]) + else + ENV.fetch( + 'GEOSERVER_SECURE_URL', Config.default_options[:geoserver_secure_url] + ) + end publisher = GeoserverPublisher.new(url) publisher.update(filename) logger.info("'#{filename}' - published to geoserver #{url} successfully") @@ -75,8 +84,10 @@ def geoserver(filename) option :geoserver_root def unpack(zipfile) zipfile_path = zipfile == File.basename(zipfile) ? File.join(ImportUtil.root_path, 'import', zipfile) : zipfile - DataHandler.spatial_root = options[:spatial_root] || ENV.fetch('SPATIAL_ROOT', nil) || 'data/spatial/' - DataHandler.geoserver_root = options[:geoserver_root] || ENV.fetch('GEOSERVER_ROOT', nil) || 'data/geoserver/' + DataHandler.spatial_root = options[:spatial_root] || ENV.fetch('SPATIAL_ROOT', + Config.default_options[:spatial_root]) + DataHandler.geoserver_root = options[:geoserver_root] || ENV.fetch('GEOSERVER_ROOT', + Config.default_options[:geoserver_root]) temp_path = File.join(Dir.pwd, 'tmp') DataHandler.extract_and_move(zipfile_path, temp_path) @@ -114,7 +125,13 @@ def all(zipfile) option :is_public, type: :boolean, default: true def geoserver_workspace(name) url = options[:geoserver_url] - url ||= options[:is_public] ? ENV.fetch('GEOSERVER_URL', nil) : ENV.fetch('GEOSERVER_SECURE_URL', nil) + url ||= if options[:is_public] + ENV.fetch('GEOSERVER_URL', Config.default_options[:geoserver_url]) + else + ENV.fetch( + 'GEOSERVER_SECURE_URL', Config.default_options[:geoserver_secure_url] + ) + end publisher = GeoserverPublisher.new(url) publisher.create_workspace(name) logger.info("geoserver workspace '#{name}' - created successfully") diff --git a/lib/gingr/config.rb b/lib/gingr/config.rb index b37ef38..dec4af2 100644 --- a/lib/gingr/config.rb +++ b/lib/gingr/config.rb @@ -12,9 +12,18 @@ module Config # dirname where all geofile related ingestion files located inside the ingestion zipfile @geofile_ingestion_dirname = 'ingestion_files' + # default options for commands + @default_options = { + geoserver_secure_url: 'http://admin:geoserver@geoserver_secure:8080/geoserver/rest/', + geoserver_url: 'http://admin:geoserver@geoserver:8080/geoserver/rest/', + spatial_url: 'https://spatial.lib.berkeley.edu', + spatial_root: 'data/spatial/', + geoserver_root: 'data/geoserver/', + solr_url: 'http://solr:8983/solr/geodata-test' + } + class << self - attr_accessor :geofile_ingestion_dirname - attr_accessor :reference_urls + attr_accessor :geofile_ingestion_dirname, :reference_urls, :default_options include Config end diff --git a/lib/gingr/import_util.rb b/lib/gingr/import_util.rb index a556ee5..b433aa3 100644 --- a/lib/gingr/import_util.rb +++ b/lib/gingr/import_util.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + require 'find' require 'uri' require_relative 'config' @@ -50,7 +51,13 @@ def root_path def publish_geoserver_files(files, url, is_public) return if files.empty? - url ||= is_public ? ENV.fetch('GEOSERVER_URL', nil) : ENV.fetch('GEOSERVER_SECURE_URL', nil) + url ||= if is_public + ENV.fetch('GEOSERVER_URL', + Config.default_options[:geoserver_url]) + else + ENV.fetch('GEOSERVER_SECURE_URL', + Config.default_options[:geoserver_secure_url]) + end publisher = GeoserverPublisher.new(url) publisher.batch_update(files) end @@ -65,19 +72,16 @@ def geo_url(url) def add_trailing_slash(url) original_uri = URI.parse(url) original_uri.path += '/' unless original_uri.path.end_with?('/') - original_uri.to_s + original_uri end def reference_url(key, options) - new_url = '' - if key == 'spatial_url' - new_url = options[key] || ENV.fetch(key.to_s.upcase, nil) || 'https://spatial.lib.berkeley.edu' - else - original_url = options[key] || ENV.fetch(key.to_s.upcase) - new_url = geo_url(original_url) - end - add_trailing_slash(new_url) + default_option_value = Config.default_options[key] + new_url = options[key] || ENV.fetch(key.to_s.upcase, default_option_value) + new_url = geo_url(new_url) if %w[geoserver_url geoserver_secure_url].include?(key.to_s) + add_trailing_slash(new_url).to_s end + end end end