pg_export 1.0.0.rc6 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +23 -2
  3. data/CHANGELOG.md +6 -3
  4. data/README.md +43 -34
  5. data/bin/pg_export +13 -131
  6. data/bin/setup +0 -2
  7. data/lib/pg_export/commands_factory.rb +158 -0
  8. data/lib/pg_export/configuration.rb +15 -16
  9. data/lib/pg_export/configuration_parser.rb +126 -0
  10. data/lib/pg_export/{lib/pg_export/factories → factories}/dump_factory.rb +1 -1
  11. data/lib/pg_export/{lib/pg_export/factories → factories}/ftp_gateway_factory.rb +1 -1
  12. data/lib/pg_export/{lib/pg_export/repositories/gateway_dump_file_repository.rb → factories/gateway_dump_file_factory.rb} +4 -4
  13. data/lib/pg_export/{lib/pg_export/factories → factories}/ssh_gateway_factory.rb +1 -1
  14. data/lib/pg_export/{lib/pg_export/operations → operations}/decrypt_dump.rb +1 -1
  15. data/lib/pg_export/{lib/pg_export/operations → operations}/encrypt_dump.rb +1 -1
  16. data/lib/pg_export/{lib/pg_export/operations → operations}/open_connection.rb +1 -1
  17. data/lib/pg_export/{lib/pg_export/operations → operations}/remove_old_dumps.rb +1 -1
  18. data/lib/pg_export/{lib/pg_export/repositories → repositories}/gateway_dump_repository.rb +2 -2
  19. data/lib/pg_export/{lib/pg_export/transactions → transactions}/export_dump.rb +10 -10
  20. data/lib/pg_export/{lib/pg_export/transactions → transactions}/import_dump_interactively.rb +19 -10
  21. data/lib/pg_export/{lib/pg_export/value_objects → value_objects}/result.rb +1 -1
  22. data/lib/pg_export/version.rb +1 -1
  23. data/lib/pg_export.rb +12 -14
  24. data/pg_export.gemspec +1 -3
  25. metadata +52 -51
  26. data/lib/pg_export/factory.rb +0 -148
  27. /data/lib/pg_export/{lib/pg_export/adapters → adapters}/shell_adapter.rb +0 -0
  28. /data/lib/pg_export/{lib/pg_export/entities → entities}/dump.rb +0 -0
  29. /data/lib/pg_export/{lib/pg_export/factories → factories}/cipher_factory.rb +0 -0
  30. /data/lib/pg_export/{lib/pg_export/gateways → gateways}/ftp.rb +0 -0
  31. /data/lib/pg_export/{lib/pg_export/gateways → gateways}/ssh.rb +0 -0
  32. /data/lib/pg_export/{lib/pg_export/listeners → listeners}/interactive/build_dump.rb +0 -0
  33. /data/lib/pg_export/{lib/pg_export/listeners → listeners}/interactive/close_connection.rb +0 -0
  34. /data/lib/pg_export/{lib/pg_export/listeners → listeners}/interactive/decrypt_dump.rb +0 -0
  35. /data/lib/pg_export/{lib/pg_export/listeners → listeners}/interactive/download_dump.rb +0 -0
  36. /data/lib/pg_export/{lib/pg_export/listeners → listeners}/interactive/encrypt_dump.rb +0 -0
  37. /data/lib/pg_export/{lib/pg_export/listeners → listeners}/interactive/fetch_dumps.rb +0 -0
  38. /data/lib/pg_export/{lib/pg_export/listeners → listeners}/interactive/open_connection.rb +0 -0
  39. /data/lib/pg_export/{lib/pg_export/listeners → listeners}/interactive/remove_old_dumps.rb +0 -0
  40. /data/lib/pg_export/{lib/pg_export/listeners → listeners}/interactive/restore.rb +0 -0
  41. /data/lib/pg_export/{lib/pg_export/listeners → listeners}/interactive/select_database.rb +0 -0
  42. /data/lib/pg_export/{lib/pg_export/listeners → listeners}/interactive/select_dump.rb +0 -0
  43. /data/lib/pg_export/{lib/pg_export/listeners → listeners}/interactive/upload_dump.rb +0 -0
  44. /data/lib/pg_export/{lib/pg_export/listeners → listeners}/interactive_listener.rb +0 -0
  45. /data/lib/pg_export/{lib/pg_export/listeners → listeners}/plain/build_dump.rb +0 -0
  46. /data/lib/pg_export/{lib/pg_export/listeners → listeners}/plain/close_connection.rb +0 -0
  47. /data/lib/pg_export/{lib/pg_export/listeners → listeners}/plain/decrypt_dump.rb +0 -0
  48. /data/lib/pg_export/{lib/pg_export/listeners → listeners}/plain/download_dump.rb +0 -0
  49. /data/lib/pg_export/{lib/pg_export/listeners → listeners}/plain/encrypt_dump.rb +0 -0
  50. /data/lib/pg_export/{lib/pg_export/listeners → listeners}/plain/fetch_dumps.rb +0 -0
  51. /data/lib/pg_export/{lib/pg_export/listeners → listeners}/plain/open_connection.rb +0 -0
  52. /data/lib/pg_export/{lib/pg_export/listeners → listeners}/plain/prepare_params.rb +0 -0
  53. /data/lib/pg_export/{lib/pg_export/listeners → listeners}/plain/remove_old_dumps.rb +0 -0
  54. /data/lib/pg_export/{lib/pg_export/listeners → listeners}/plain/restore.rb +0 -0
  55. /data/lib/pg_export/{lib/pg_export/listeners → listeners}/plain/upload_dump.rb +0 -0
  56. /data/lib/pg_export/{lib/pg_export/listeners → listeners}/plain_listener.rb +0 -0
  57. /data/lib/pg_export/{lib/pg_export/transactions → transactions}/evaluator.rb +0 -0
  58. /data/lib/pg_export/{lib/pg_export/ui → ui}/interactive/input.rb +0 -0
  59. /data/lib/pg_export/{lib/pg_export/ui → ui}/plain/input.rb +0 -0
  60. /data/lib/pg_export/{lib/pg_export/value_objects → value_objects}/dump_file.rb +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ca033b9c62cc381584bd9696e9607b63d89f52310a5847b714908c7140e755dc
4
- data.tar.gz: 755e0fa8760fa1ba6b2d3d8af65532da26b9a8978b5005853d9aaede8131d748
3
+ metadata.gz: 568f50f6bbac2163007c60e8a27909ef5090c8ff043b22d4951733f74a31435f
4
+ data.tar.gz: a7401975c6950a99177279e63f9c8a3a6e39af5f3832c3357d01ac9532f98906
5
5
  SHA512:
6
- metadata.gz: 01a50e23dff7a059c606aaff6ea744f3ee3da607ffcf5062514c447a0f9413386a7df378ae7467ff9e35745b65d5a78fa96c6e9abf93b6cff2d8dc4fa9be5e55
7
- data.tar.gz: a32e7517a57dcc5eee39c40334ea5552709930d2e5bd402eaf55e67c96625b528aa40f99ef2ee5cdbacae8ad052250d2122f87eb83705ab85fa41599a755fff0
6
+ metadata.gz: 06241c56c3d2cb35c64860e015db5e1d6a638a579372dad8a81c70b00cf4fdf0907c4375b7ff469f1de4c00ab361c15f8744a76791c808f89d85cfb0d8f346fa
7
+ data.tar.gz: bd265680aa904baedf80e1a006ae5afed705918e7cf393098bac42a6c39d9d3ae23ca62915af5b782c74310b410eb709af890be89405dbe67779fa1a46608a6e
data/.rubocop.yml CHANGED
@@ -2,8 +2,6 @@ AllCops:
2
2
  TargetRubyVersion: 2.3
3
3
 
4
4
  Metrics/BlockLength:
5
- Exclude:
6
- - 'bin/pg_export'
7
5
  ExcludedMethods: ['describe', 'context']
8
6
 
9
7
  Layout/LineLength:
@@ -32,6 +30,29 @@ Style/TrailingCommaInHashLiteral:
32
30
 
33
31
  Metrics/AbcSize:
34
32
  Max: 30
33
+ Exclude:
34
+ - 'lib/pg_export/configuration_parser.rb'
35
+ - 'lib/pg_export/configuration.rb'
35
36
 
36
37
  Metrics/PerceivedComplexity:
37
38
  Max: 30
39
+
40
+ Lint/AmbiguousOperator:
41
+ Enabled: false
42
+
43
+ Metrics/ClassLength:
44
+ Max: 200
45
+
46
+ Layout/ExtraSpacing:
47
+ Exclude:
48
+ - 'lib/pg_export/configuration_parser.rb'
49
+
50
+ Metrics/ParameterLists:
51
+ Exclude:
52
+ - 'lib/pg_export/configuration.rb'
53
+
54
+ Metrics/ParameterLists:
55
+ Enabled: false
56
+
57
+ Style/Semicolon:
58
+ Enabled: false
data/CHANGELOG.md CHANGED
@@ -1,4 +1,4 @@
1
- ### 1.0.0 - 2022.05.17
1
+ ### 1.0.0 - 2023.12.15
2
2
  - Make it compatible with Ruby 3.0
3
3
  - Change configuration envs:
4
4
  - BACKUP_FTP_HOST -> PG_EXPORT_GATEWAY_HOST
@@ -7,9 +7,12 @@
7
7
  - DUMP_ENCRYPTION_KEY -> PG_EXPORT_ENCRYPTION_KEY
8
8
  - Drop Ruby 2.3 support
9
9
  - Add SSH option
10
- - Add `encryption_algorithm` option to interface
10
+ - Add `encryption_algorithm` option
11
+ - All command line options has now their equivalents in ENVs (and vice-versa)
11
12
  - Remove dry libraries dependencies
12
- - in case of failure bin/pg_export now returns with 1 exit value
13
+ - In case of failure bin/pg_export now returns exit value 1
14
+ - Improve performance
15
+ - Improve error handling
13
16
 
14
17
  ### 0.7.7 - 2020.09.07
15
18
 
data/README.md CHANGED
@@ -54,34 +54,48 @@ Or install it yourself as:
54
54
  pg_export DATABASE [OPTION..]
55
55
  pg_export --interactive DATABASE [OPTION..]
56
56
 
57
+ EXIT VALUES
58
+ 0 - Success
59
+ 1 - Error
60
+
57
61
  ARGUMENTS
58
62
  DATABASE - database name to export (when default mode)
59
- - phrase to filter database dumps by (when interactive mode)
63
+ - phrase to filter database dumps by (when interactive mode)
60
64
 
61
65
  OPTIONS
62
- -g, --gateway GATEWAY Allowed values: ftp, ssh. Default: ftp. Credentials need to be set via ENVs
63
- -s, --ssh Same as "--gateway ssh"
64
- -f, --ftp Same as "--gateway ftp"
65
- -d, --database DATABASE Alternative way of specifying name of the database to export or phrase to filter by
66
- -e, --encryption_key KEY Dumps will be SSL encrypted using this key. Should have exactly 16 characters. Overwrites PG_EXPORT_ENCRYPTION_KEY env
67
- -a, --algorithm ALGORITHM Encryption cipher algorithm (default: AES-128-CBC). Overwrites PG_EXPORT_ENCRYPTION_ALGORITHM env. For available option see `$ openssl list -cipher-algorithms`
68
- -k, --keep KEEP Number of dump files to keep on FTP (default: 10). Overwrites KEEP_DUMPS env
69
- -t, --timestamped Enables log messages with timestamps
70
- -m, --muted Mutes log messages (overrides -t option)
71
- -i, --interactive Interactive mode, for importing dumps
72
- -v, --version Show version
73
- -h, --help Show this message
66
+ -g, --gateway GATEWAY Allowed values: ftp, ssh. Default: ftp. Credentials need to be set via ENVs
67
+ -U, --user USER Gateway (ftp or ssh) user
68
+ -H, --host HOST Gateway (ftp or ssh) host
69
+ -P, --password PASSWORD Gateway (ftp or ssh) password
70
+ -d, --database DATABASE In plain mode: database name to export;
71
+ In interactive mode: phrase to filter by
72
+ -e, --encryption_key KEY Dumps will be SSL encrypted using this key. Should have exactly 16 characters
73
+ -a, --algorithm ALGORITHM Encryption cipher algorithm (default: AES-128-CBC);
74
+ For available option see `$ openssl list -cipher-algorithms`
75
+ -k, --keep KEEP Number of dump files to keep on FTP (default: 10)
76
+ -s, --ssh Same as "--gateway ssh". When set, the --gateway option is ignored
77
+ -f, --ftp Same as "--gateway ftp". When set, the --gateway option is ignored
78
+ -t, --timestamped Prepends log messages with timestamps
79
+ -m, --muted Mutes log messages. When set, the -t option is ignored. Prints message only on error
80
+ -i, --interactive Interactive mode, for importing dumps. Whan set, the -t and -m options are ignored
81
+ -w, --welcome Try connecting to the gateway (FTP or SSH) to verify the connection and exit
82
+ -c, --configuration Print the configuration and exit
83
+ -v, --version Print version
84
+ -h, --help Print this message and exit
74
85
 
75
86
  ENV
76
- PG_EXPORT_GATEWAY_HOST required
77
- PG_EXPORT_GATEWAY_USER required
78
- PG_EXPORT_GATEWAY_PASSWORD optional when eg. authorized key is added
79
- PG_EXPORT_ENCRYPTION_KEY required or set by --encryption_key)
80
- PG_EXPORT_ENCRYPTION_ALGORITHM required or set by --algorithm)
87
+ Each of the above options can be also set using enviromental variables.
88
+ Use full option names prepending with PG_EXPORT_ phrase.
89
+ Command line options takes precedence over the ENVs.
90
+
91
+ Eg. below two commands are equivalent:
92
+ pg_export -s -U user -H host database_name -k 10
81
93
 
82
- TEST RUN
83
- -c, --configuration Print the configuration
84
- -w, --welcome Try connecting to the gateway (FTP or SSH) to verify the connection
94
+ Is equivalent to:
95
+ export PG_EXPORT_GATEWAY_USER=user
96
+ export PG_EXPORT_GATEWAY_HOST=host
97
+ export PG_EXPORT_KEEP=20
98
+ pg_export -s database_name -k 10
85
99
 
86
100
  ## How to start
87
101
 
@@ -96,21 +110,16 @@ __Step 1.__ Prepare ENV variables.
96
110
  /* Dumps will be SSL(AES-128-CBC) encrypted using this key. */
97
111
  PG_EXPORT_ENCRYPTION_KEY=1234567890abcdef
98
112
 
99
- /* Dumps to be kept on FTP */
100
- /* Optional, defaults to 10 */
101
- KEEP_DUMPS=5
102
-
103
- Note, that variables cannot include `#` sign, [more info](http://serverfault.com/questions/539730/environment-variable-in-etc-environment-with-pound-hash-sign-in-the-value).
104
-
105
113
  __Step 2.__ Print the configuration to verify if env variables has been loaded properly.
106
114
 
107
115
  $ pg_export --configuration
108
116
  => encryption_key k4***
109
117
  gateway_host yourftp.example.com
110
118
  gateway_user your_gateway_user
111
- gateway_password pass***
119
+ gateway_password
112
120
  logger_format plain
113
- keep_dumps 2
121
+ keep_dumps 10
122
+ ....
114
123
 
115
124
  __Step 3.__ Try connecting to FTP to verify the connection.
116
125
 
@@ -119,18 +128,18 @@ __Step 3.__ Try connecting to FTP to verify the connection.
119
128
 
120
129
  __Step 4.__ Perform database export.
121
130
 
122
- $ pg_export -d your_database [-k 5]
123
- => Dump database your_database to your_database_20181016_121314 (1.36MB)
124
- Encrypt your_database_20181016_121314 (1.34MB)
131
+ $ pg_export -d your_database -k 5
132
+ => Dump database your_database to database_name (1.36MB)
133
+ Encrypt database_name_20181016_121314 (1.34MB)
125
134
  Connect to yourftp.example.com
126
- Upload your_database_20181016_121314 (1.34MB) to yourftp.example.com
135
+ Upload database_name_20181016_121314 (1.34MB) to yourftp.example.com
127
136
  Close FTP
128
137
 
129
138
  ## How to restore a dump?
130
139
 
131
140
  Run interactive mode and follow the instructions:
132
141
 
133
- pg_export [-d your_database] -i
142
+ pg_export [-d database_name] -i
134
143
 
135
144
  ## Development
136
145
 
data/bin/pg_export CHANGED
@@ -4,143 +4,25 @@
4
4
  lib = File.expand_path('../lib', __dir__)
5
5
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
6
 
7
- require 'optparse'
8
7
  require 'pg_export'
9
8
 
10
- database = nil
11
- x = nil
12
-
13
- option_parser = OptionParser.new do |opts|
14
- opts.banner = <<~TXT
15
- NAME
16
- pg_export - CLI for exporting/importing PostgreSQL dumps via FTP/SSH
17
-
18
- SYNOPSIS
19
- pg_export DATABASE [OPTION..]
20
- pg_export --interactive DATABASE [OPTION..]
21
-
22
- EXIT VALUES
23
- 0 - Success
24
- 1 - Error
25
-
26
- ARGUMENTS
27
- DATABASE - database name to export (when default mode)
28
- - phrase to filter database dumps by (when interactive mode)
29
-
30
- OPTIONS
31
- TXT
32
-
33
- opts.program_name = 'pg_export'
34
- opts.version = PgExport::VERSION
35
-
36
- opts.on('-g', '--gateway GATEWAY', %w[ftp ssh], 'Allowed values: ftp, ssh. Default: ftp. Credentials need to be set via ENVs') do |g|
37
- ENV['GATEWAY'] = g
38
- end
39
-
40
- opts.on('-s', '--ssh', 'Same as "--gateway ssh"') do
41
- ENV['GATEWAY'] = 'ssh'
42
- end
43
-
44
- opts.on('-f', '--ftp', 'Same as "--gateway ftp"') do
45
- ENV['GATEWAY'] = 'ftp'
46
- end
47
-
48
- opts.on('-d', '--database DATABASE', String, 'Alternative way of specifying name of the database to export or phrase to filter by') do |d|
49
- database = d
50
- end
51
-
52
- opts.on('-e', '--encryption_key KEY', String, 'Dumps will be SSL encrypted using this key. Should have exactly 16 characters. Overwrites PG_EXPORT_ENCRYPTION_KEY env') do |key|
53
- ENV['PG_EXPORT_ENCRYPTION_KEY'] = key
54
- end
55
-
56
- opts.on('-a', '--algorithm ALGORITHM', String, 'Encryption cipher algorithm (default: AES-128-CBC). Overwrites PG_EXPORT_ENCRYPTION_ALGORITHM env. For available option see `$ openssl list -cipher-algorithms`') do |key|
57
- ENV['PG_EXPORT_ENCRYPTION_KEY'] = key
58
- end
59
-
60
- opts.on('-k', '--keep KEEP', String, 'Number of dump files to keep on FTP (default: 10). Overwrites KEEP_DUMPS env') do |keep|
61
- ENV['KEEP_DUMPS'] = keep
62
- end
63
-
64
- opts.on('-t', '--timestamped', 'Enables log messages with timestamps') do
65
- ENV['LOGGER_FORMAT'] = 'timestamped'
66
- end
67
-
68
- opts.on('-m', '--muted', 'Mutes log messages (overrides -t option)') do
69
- ENV['LOGGER_FORMAT'] = 'muted'
70
- end
71
-
72
- opts.on('-i', '--interactive', 'Interactive mode, for importing dumps') do
73
- ENV['PG_EXPORT_MODE'] = 'interactive'
74
- end
75
-
76
- opts.on('-v', '--version', 'Show version') do
77
- puts PgExport::VERSION
78
- exit
79
- end
80
-
81
- opts.on('-h', '--help', 'Show this message') do
82
- puts opts
83
- exit
84
- end
85
-
86
- opts.separator <<~TXT
87
-
88
- ENV
89
- PG_EXPORT_GATEWAY_HOST required
90
- PG_EXPORT_GATEWAY_USER required
91
- PG_EXPORT_GATEWAY_PASSWORD optional when eg. authorized key is added
92
- PG_EXPORT_ENCRYPTION_KEY required or set by --encryption_key)
93
- PG_EXPORT_ENCRYPTION_ALGORITHM required or set by --algorithm)
94
- TXT
95
-
96
- opts.separator "\nTEST RUN"
97
-
98
- opts.on('-c', '--configuration', 'Print the configuration') do
99
- x = -> { puts PgExport.new.config }
100
- end
101
-
102
- opts.on('-w', '--welcome', 'Try connecting to the gateway (FTP or SSH) to verify the connection') do
103
- x = -> { puts PgExport.new.gateway_welcome }
104
- end
105
-
106
- if ARGV.empty?
107
- puts opts
108
- exit
109
- end
110
- end
111
-
112
9
  begin
113
- option_parser.parse!
114
- if x
115
- x.call
116
- exit
117
- end
118
- database = ARGV.first unless ARGV.empty?
119
- rescue OptionParser::ParseError => e
10
+ config = PgExport::ConfigurationParser.parse
11
+ pg_export = PgExport.new(config)
12
+ result = pg_export.call
13
+ result
14
+ .on_success { |outcome| puts outcome if outcome }
15
+ .on_failure { |outcome| warn outcome[:message]; exit 1 }
16
+ rescue PgExport::ConfigurationParser::Error => e
120
17
  warn e.message.capitalize
121
- warn 'Details:'
122
- warn option_parser.to_s.split("\n").grep(/ #{e.args.first}/).join("\n")
123
- warn 'Type "pg_export" for available options'
124
- exit 1
125
- end
126
-
127
- begin
128
- pg_export = PgExport.new
129
- rescue PgExport::InitializationError => e
130
- warn 'Unable to initialize PgExport due to invalid configuration. Check you ENVs.'
131
- warn "Detailed message: #{e.message}"
132
- exit 1
133
- end
134
18
 
135
- begin
136
- pg_export
137
- .call(database)
138
- .on_success { exit 0 }
139
- .on_failure do |outcome|
140
- warn outcome[:message]
141
- exit 1
142
- end
19
+ if e.reason == 'missing argument'
20
+ warn "\nHint for the #{e.args.first} option:"
21
+ warn PgExport::ConfigurationParser.help.to_s.split("\n").grep(/ #{e.args.first}/).join("\n")
22
+ end
143
23
 
24
+ warn 'Type "pg_export -h" to print options.'
25
+ exit 1
144
26
  rescue Interrupt
145
27
  puts
146
28
  end
data/bin/setup CHANGED
@@ -4,5 +4,3 @@ IFS=$'\n\t'
4
4
  set -vx
5
5
 
6
6
  bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -0,0 +1,158 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pg_export/value_objects/result'
4
+
5
+ class PgExport
6
+ class CommandsFactory
7
+ attr_reader :config
8
+
9
+ def initialize(config:)
10
+ @config = config
11
+ end
12
+
13
+ def print_configuration
14
+ ->(_) { ValueObjects::Success.new(config) }
15
+ end
16
+
17
+ def gateway_welcome
18
+ ->(_) { ValueObjects::Success.new(gateway_factory.gateway.welcome) }
19
+ end
20
+
21
+ def print_version
22
+ ->(_) { ValueObjects::Success.new(PgExport::VERSION) }
23
+ end
24
+
25
+ def print_help
26
+ ->(_) { ValueObjects::Success.new(PgExport::ConfigurationParser.help) }
27
+ end
28
+
29
+ def export_dump
30
+ require 'pg_export/transactions/export_dump'
31
+ require 'pg_export/factories/dump_factory'
32
+ require 'pg_export/adapters/shell_adapter'
33
+ require 'pg_export/operations/encrypt_dump'
34
+ require 'pg_export/operations/open_connection'
35
+ require 'pg_export/operations/remove_old_dumps'
36
+ require 'pg_export/repositories/gateway_dump_repository'
37
+
38
+ PgExport::Transactions::ExportDump.new(
39
+ dump_factory: Factories::DumpFactory.new,
40
+ shell_adapter: Adapters::ShellAdapter.new,
41
+ encrypt_dump: Operations::EncryptDump.new(cipher_factory: cipher_factory),
42
+ open_connection: Operations::OpenConnection.new(gateway_factory: gateway_factory),
43
+ remove_old_dumps: Operations::RemoveOldDumps.new(
44
+ gateway_dump_repository: Repositories::GatewayDumpRepository.new,
45
+ keep: config.keep_dumps
46
+ ),
47
+ listeners: plain_listeners
48
+ )
49
+ end
50
+
51
+ def import_dump_interactively
52
+ require 'pg_export/transactions/import_dump_interactively'
53
+ require 'pg_export/ui/interactive/input'
54
+ require 'pg_export/adapters/shell_adapter'
55
+ require 'pg_export/factories/gateway_dump_file_factory'
56
+ require 'pg_export/repositories/gateway_dump_repository'
57
+ require 'pg_export/operations/open_connection'
58
+ require 'pg_export/operations/decrypt_dump'
59
+
60
+ PgExport::Transactions::ImportDumpInteractively.new(
61
+ input: Ui::Interactive::Input.new,
62
+ shell_adapter: Adapters::ShellAdapter.new,
63
+ gateway_dump_file_factory: Factories::GatewayDumpFileFactory.new,
64
+ gateway_dump_repository: Repositories::GatewayDumpRepository.new,
65
+ open_connection: Operations::OpenConnection.new(gateway_factory: gateway_factory),
66
+ decrypt_dump: Operations::DecryptDump.new(cipher_factory: cipher_factory),
67
+ listeners: interactive_listeners
68
+ )
69
+ end
70
+
71
+ private
72
+
73
+ def gateway_factory
74
+ if config.gateway == :ftp
75
+ require 'pg_export/factories/ftp_gateway_factory'
76
+
77
+ Factories::FtpGatewayFactory.new(config: config)
78
+ elsif config.gateway == :ssh
79
+ require 'pg_export/factories/ssh_gateway_factory'
80
+
81
+ Factories::SshGatewayFactory.new(config: config)
82
+ else
83
+ raise ArgumentError, "Unknown gateway #{config.gateway}"
84
+ end
85
+ end
86
+
87
+ def cipher_factory
88
+ require 'pg_export/factories/cipher_factory'
89
+
90
+ Factories::CipherFactory.new(
91
+ encryption_algorithm: config.encryption_algorithm,
92
+ encryption_key: config.encryption_key
93
+ )
94
+ end
95
+
96
+ def plain_listeners
97
+ require 'pg_export/listeners/plain/prepare_params'
98
+ require 'pg_export/listeners/plain/build_dump'
99
+ require 'pg_export/listeners/plain/close_connection'
100
+ require 'pg_export/listeners/plain/decrypt_dump'
101
+ require 'pg_export/listeners/plain/download_dump'
102
+ require 'pg_export/listeners/plain/encrypt_dump'
103
+ require 'pg_export/listeners/plain/fetch_dumps'
104
+ require 'pg_export/listeners/plain/open_connection'
105
+ require 'pg_export/listeners/plain/remove_old_dumps'
106
+ require 'pg_export/listeners/plain/restore'
107
+ require 'pg_export/listeners/plain/upload_dump'
108
+
109
+ logger = build_logger(config.logger_format)
110
+
111
+ {
112
+ prepare_params: Listeners::Plain::PrepareParams.new(logger: logger),
113
+ build_dump: Listeners::Plain::BuildDump.new(logger: logger),
114
+ encrypt_dump: Listeners::Plain::EncryptDump.new(logger: logger),
115
+ open_connection: Listeners::Plain::OpenConnection.new(logger: logger),
116
+ upload_dump: Listeners::Plain::UploadDump.new(logger: logger),
117
+ remove_old_dumps: Listeners::Plain::RemoveOldDumps.new(logger: logger),
118
+ close_connection: Listeners::Plain::CloseConnection.new(logger: logger)
119
+ }
120
+ end
121
+
122
+ def interactive_listeners
123
+ require 'pg_export/listeners/interactive/open_connection'
124
+ require 'pg_export/listeners/interactive/fetch_dumps'
125
+ require 'pg_export/listeners/interactive/select_dump'
126
+ require 'pg_export/listeners/interactive/download_dump'
127
+ require 'pg_export/listeners/interactive/close_connection'
128
+ require 'pg_export/listeners/interactive/decrypt_dump'
129
+ require 'pg_export/listeners/interactive/select_database'
130
+ require 'pg_export/listeners/interactive/restore'
131
+
132
+ {
133
+ open_connection: Listeners::Interactive::OpenConnection.new,
134
+ fetch_dumps: Listeners::Interactive::FetchDumps.new,
135
+ select_dump: Listeners::Interactive::SelectDump.new,
136
+ download_dump: Listeners::Interactive::DownloadDump.new,
137
+ close_connection: Listeners::Interactive::CloseConnection.new,
138
+ decrypt_dump: Listeners::Interactive::DecryptDump.new,
139
+ select_database: Listeners::Interactive::SelectDatabase.new,
140
+ restore: Listeners::Interactive::Restore.new,
141
+ }
142
+ end
143
+
144
+ def build_logger(logger_format)
145
+ require 'logger'
146
+
147
+ formatters = {
148
+ plain: ->(_, _, _, message) { "#{message}\n" },
149
+ muted: ->(*) {},
150
+ timestamped: lambda do |severity, datetime, progname, message|
151
+ "#{datetime} #{Process.pid} TID-#{Thread.current.object_id.to_s(36)}#{progname} #{severity}: #{message}\n"
152
+ end
153
+ }
154
+
155
+ Logger.new($stdout, formatter: formatters.fetch(logger_format))
156
+ end
157
+ end
158
+ end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'optparse'
4
+
3
5
  class PgExport
4
6
  class Configuration
5
7
  ATTRS = %i[
@@ -11,24 +13,12 @@ class PgExport
11
13
  logger_format
12
14
  keep_dumps
13
15
  gateway mode
16
+ database
17
+ command
14
18
  ].freeze
15
19
 
16
20
  attr_reader *ATTRS
17
21
 
18
- def self.build(env)
19
- new(
20
- encryption_key: env['PG_EXPORT_ENCRYPTION_KEY'],
21
- encryption_algorithm: env['PG_EXPORT_ENCRYPTION_ALGORITHM'],
22
- gateway_host: env['PG_EXPORT_GATEWAY_HOST'],
23
- gateway_user: env['PG_EXPORT_GATEWAY_USER'],
24
- gateway_password: env['PG_EXPORT_GATEWAY_PASSWORD'],
25
- logger_format: env['LOGGER_FORMAT'],
26
- keep_dumps: env['KEEP_DUMPS'],
27
- gateway: env['GATEWAY'],
28
- mode: env['PG_EXPORT_MODE']
29
- )
30
- end
31
-
32
22
  def initialize(
33
23
  encryption_key: nil,
34
24
  encryption_algorithm: nil,
@@ -38,7 +28,9 @@ class PgExport
38
28
  logger_format: nil,
39
29
  keep_dumps: nil,
40
30
  gateway: nil,
41
- mode: nil
31
+ mode: nil,
32
+ database: nil,
33
+ command: nil
42
34
  )
43
35
 
44
36
  @encryption_key = String(encryption_key)
@@ -57,7 +49,9 @@ class PgExport
57
49
 
58
50
  @logger_format = logger_format.to_s.to_sym
59
51
  @logger_format = :plain if @logger_format.empty?
60
- raise ArgumentError, 'Logger format must be one of: plain, timestamped, muted' unless %i[plain timestamped muted].include?(@logger_format)
52
+ unless %i[plain timestamped muted].include?(@logger_format)
53
+ raise ArgumentError, 'Logger format must be one of: plain, timestamped, muted'
54
+ end
61
55
 
62
56
  @keep_dumps = Integer(keep_dumps || 10)
63
57
  raise ArgumentError, 'Keep dumps must greater or equal to 1' unless @keep_dumps >= 1
@@ -69,6 +63,11 @@ class PgExport
69
63
  @mode = mode.to_s.to_sym
70
64
  @mode = :plain if @mode.empty?
71
65
  raise ArgumentError, 'Mode must be one of: plain, interactive' unless %i[plain interactive].include?(@mode)
66
+
67
+ @database = String(database) unless database.nil?
68
+
69
+ @command = command.to_s.to_sym
70
+ @command = :export_dump if @command.empty?
72
71
  end
73
72
 
74
73
  def to_s