Skip to content

Commit bc5c57b

Browse files
committed
Look for cred/config files in user's home dir
Replaces the s-p-u install directory as a place to check for cred/smtp/email files.
1 parent 8f70e08 commit bc5c57b

File tree

6 files changed

+60
-27
lines changed

6 files changed

+60
-27
lines changed

README.md

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,17 @@ Sierra::DB.write_results('output.csv', format: 'csv')
115115
Sierra::DB.write_results('output.xlsx', format: 'xlsx') # Requires WIN32OLE so probably Windows-only.
116116

117117
# Send results as attachment
118-
details = {:from => '[email protected]',
119-
120-
121-
:subject => 'that query',
122-
:body => 'Attached.'}
118+
details = {from: '[email protected]',
119+
120+
121+
subject: 'that query',
122+
body: 'Attached.'}
123123
Sierra::DB.mail_results('output.tsv', mail_details: details)
124124
```
125125

126126
### Retrieve arbitrary views
127127

128-
Retrieve arbitrary views as arrays of hashes via ```Sierra::DB.db[:view_name]```.
128+
Retrieve arbitrary views as arrays of hashes via `Sierra::DB.db[:view_name]`.
129129

130130
```ruby
131131
Sierra::DB.db[:request_rule].first
@@ -135,17 +135,17 @@ Sierra::DB.db[:request_rule].first
135135
## SETUP
136136

137137
* git clone <https://github.com/UNC-Libraries/sierra-postgres-utilities>
138-
* cd sierra-postgres-utilities
139-
* bundle install
140-
* bundle exec rake install
138+
* `cd sierra-postgres-utilities`
139+
* `bundle install`
140+
* `bundle exec rake install`
141141
* supply the Sierra postgres credentials per the below
142142
* optionally supply smtp server address
143143

144-
When possible, it is recommended that you also install the ```sequel_pg``` gem which makes database access significantly faster. See <https://github.com/jeremyevans/sequel_pg> for installation details / requirements.
144+
When possible, it is recommended that you also install the `sequel_pg` gem which makes database access significantly faster. See <https://github.com/jeremyevans/sequel_pg> for installation details / requirements.
145145

146146
### Credentials
147147

148-
Create a yaml file in the base directory like so:
148+
Create a yaml/text file like so:
149149

150150
```yaml
151151
host: myhost.example.com
@@ -155,11 +155,18 @@ user: myusername
155155
password: mypassword
156156
```
157157
158-
Store the creds in a file ```sierra_prod.secret``` in the
159-
current working directory or the base directory of sierra_postgres_utilities.
160-
Creds from this file will be used as the default connection.
158+
You may need to quote values (e.g. password) if they contain special characters.
161159
162-
Alternately, specify a credential file location as an environment variable, e.g.:
160+
By default, sierra_postgres_utilities will try to read credentials from:
161+
- a file `sierra_prod.secret` in the current working directory
162+
- failing that, a file `sierra_prod.secret` in the user's home directory
163+
164+
For casual use, we recommend keeping the credentials in your home directory,
165+
in a file called `sierra_prod.secret`.
166+
167+
#### Credentials via environment variables
168+
169+
Alternately, you can specify a credential file location as an environment variable, e.g.:
163170

164171
```bash
165172
SIERRA_INIT_CREDS=my/path/file.yaml irb
@@ -173,10 +180,13 @@ ENV['SIERRA_INIT_CREDS'] = 'my/path/file.yaml'
173180
require 'sierra_postgres_utilities'
174181
```
175182

183+
This still relies on credentials being stored in a file. The path to the
184+
credential file, not the credentials themselves, is set as an env variable.
185+
176186
### SMTP connection / email address storage
177187

178188
Define an smtp connection (that does not require authentication) if you'll use this to send emails.
179-
Create ```smtp.secret``` in the working directory:
189+
Create `smtp.secret` in the working directory or home directory:
180190

181191
```yaml
182192
address: smtp.example.com

lib/sierra_postgres_utilities/db/connection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def self.creds_from_file(file)
113113
creds = YAML.load_file(file)
114114
rescue Errno::ENOENT
115115
begin
116-
creds = YAML.load_file(File.join(base_dir, file))
116+
creds = YAML.load_file(File.join(Dir.home, file))
117117
rescue Errno::ENOENT
118118
puts 'WARN: Connection credentials invalid or not found.'
119119
end

lib/sierra_postgres_utilities/db/query.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ def self.headers
8282
# (see #write_results)
8383
def self.write_results(outfile, results: self.results, headers: nil,
8484
include_headers: true, format: :tsv)
85-
puts 'writing results'
8685
headers ||= self.headers
8786
headers = '' unless include_headers
8887

@@ -112,7 +111,13 @@ def self.write_csv(outfile, results, headers, col_sep: ',')
112111
outfile = File.open(outfile, 'wb') unless outfile.respond_to?(:read)
113112
csv = CSV.new(outfile, col_sep: col_sep)
114113
csv << headers unless headers.empty?
114+
115+
query_completed = false
115116
results.each do |record|
117+
unless query_completed
118+
puts 'writing results'
119+
query_completed = true
120+
end
116121
csv << record.values
117122
end
118123
outfile.close
@@ -146,6 +151,7 @@ def self.write_xlsx(outfile, results, headers)
146151
# write data
147152
i = 1
148153
results.each do |result|
154+
puts 'writing results' if i == 1
149155
i += 1
150156
worksheet.Range("A#{i}:#{end_col}#{i}").value = result.values
151157
end
@@ -164,7 +170,7 @@ def self.emails(file = 'email.secret')
164170
begin
165171
YAML.load_file(file)
166172
rescue Errno::ENOENT
167-
YAML.load_file(File.join(Connection.base_dir, file))
173+
YAML.load_file(File.join(Dir.home, file))
168174
rescue TypeError
169175
YAML.load(file)
170176
end
@@ -183,7 +189,7 @@ def self.smtp(file = 'smtp.secret')
183189
begin
184190
YAML.load_file(file)
185191
rescue Errno::ENOENT
186-
YAML.load_file(File.join(Connection.base_dir, file))
192+
YAML.load_file(File.join(Dir.home, file))
187193
rescue TypeError
188194
YAML.load(file)
189195
end
@@ -195,6 +201,7 @@ def self.smtp=(hsh)
195201

196202
# (see #mail_results)
197203
def self.send_mail(outfile, mail_details, remove_file: false)
204+
smtp = Sierra::DB::Query.smtp
198205
Mail.defaults do
199206
delivery_method :smtp, address: smtp['address'], port: smtp['port']
200207
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Sierra
2-
VERSION = '0.3.2'.freeze
2+
VERSION = '0.3.3'.freeze
33
end

spec/spec_data/b1841152a.mrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
00469cam 2200169Ia 4500001000800000005001700008008004100025020001500066040001300081100002500094245005300119260003200172300002100204907001500225915004600240935001300286867113419820807000000.0820807s1981 enk 000 1 eng d a0094643407 aNOCcNOC1 aFassnidge, Virginia.10aSomething else :ba novel /cVirginia Fassnidge. aLondon :bConstable,c1981. a152 p. ;c23 cm. a.b18411526 9Baseline 09_20139Under Authority Control aADH-2114
1+
00614cam a2200205Ia 4500001000800000005001700008008004100025020001500066040001800081100007200099245005300171260003200224300002300256336002600279337002800305338002700333907001500360915002000375935001300395867113419820807000000.0820807s1981 enk 000 1 eng d a0094643407 aNOCbengcNOC1 aFassnidge, Virginia.0http://id.loc.gov/authorities/names/n8712893810aSomething else :ba novel /cVirginia Fassnidge. aLondon :bConstable,c1981. a152 pages ;c23 cm atextbtxt2rdacontent aunmediatedbn2rdamedia avolumebnc2rdacarrier a.b18411526 9MARCIVE20190805 aADH-2114

spec/spec_data/b1841152a.xml

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<record>
2-
<leader>00469cam 2200169Ia 4500</leader>
2+
<leader>00614cam a2200205Ia 4500</leader>
33
<controlfield tag='001'>8671134</controlfield>
44
<controlfield tag='005'>19820807000000.0</controlfield>
55
<controlfield tag='008'>820807s1981 enk 000 1 eng d</controlfield>
@@ -8,10 +8,12 @@
88
</datafield>
99
<datafield tag='040' ind1=' ' ind2=' '>
1010
<subfield code='a'>NOC</subfield>
11+
<subfield code='b'>eng</subfield>
1112
<subfield code='c'>NOC</subfield>
1213
</datafield>
1314
<datafield tag='100' ind1='1' ind2=' '>
1415
<subfield code='a'>Fassnidge, Virginia.</subfield>
16+
<subfield code='0'>http://id.loc.gov/authorities/names/n87128938</subfield>
1517
</datafield>
1618
<datafield tag='245' ind1='1' ind2='0'>
1719
<subfield code='a'>Something else :</subfield>
@@ -24,15 +26,29 @@
2426
<subfield code='c'>1981.</subfield>
2527
</datafield>
2628
<datafield tag='300' ind1=' ' ind2=' '>
27-
<subfield code='a'>152 p. ;</subfield>
28-
<subfield code='c'>23 cm.</subfield>
29+
<subfield code='a'>152 pages ;</subfield>
30+
<subfield code='c'>23 cm</subfield>
31+
</datafield>
32+
<datafield tag='336' ind1=' ' ind2=' '>
33+
<subfield code='a'>text</subfield>
34+
<subfield code='b'>txt</subfield>
35+
<subfield code='2'>rdacontent</subfield>
36+
</datafield>
37+
<datafield tag='337' ind1=' ' ind2=' '>
38+
<subfield code='a'>unmediated</subfield>
39+
<subfield code='b'>n</subfield>
40+
<subfield code='2'>rdamedia</subfield>
41+
</datafield>
42+
<datafield tag='338' ind1=' ' ind2=' '>
43+
<subfield code='a'>volume</subfield>
44+
<subfield code='b'>nc</subfield>
45+
<subfield code='2'>rdacarrier</subfield>
2946
</datafield>
3047
<datafield tag='907' ind1=' ' ind2=' '>
3148
<subfield code='a'>.b18411526</subfield>
3249
</datafield>
3350
<datafield tag='915' ind1=' ' ind2=' '>
34-
<subfield code='9'>Baseline 09_2013</subfield>
35-
<subfield code='9'>Under Authority Control</subfield>
51+
<subfield code='9'>MARCIVE20190805</subfield>
3652
</datafield>
3753
<datafield tag='935' ind1=' ' ind2=' '>
3854
<subfield code='a'>ADH-2114</subfield>

0 commit comments

Comments
 (0)