drive_env 0.3.0 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 168dd41a8e0711d190bd6ccef00ca9aa062b1ac5ca917a568bb008772f23fa49
4
- data.tar.gz: 8950ea85fc100460414061b5521751ac1750aad1c906073720aae0df4d96403c
3
+ metadata.gz: 160b4c57c17bf60b88b243060169567a3c1d84f40feb407106dde5b094ac5dd3
4
+ data.tar.gz: 4dd6578c6a1d4c4f0772067c7eaae6cb686ff66857f00c18993bb1f561591a3d
5
5
  SHA512:
6
- metadata.gz: b048edbdc74fe660b992cbdd22f644a01df340668127e05aa6a1050349cbe8aeeff5807ca04dfa77367cb7c9f1c4d7ee83de89d742dd7cb0fce981a7f5ffc753
7
- data.tar.gz: c6c19f501c70a5f2996b2206c70286397f7f6c9c5b63993a219ac0126844072b00afa3a6534f6ee495fa6d0aa1dda4fe20c5ded0287eae7982c4c097ac61b951
6
+ metadata.gz: be5b1a9e9d2959db4fdccff1b4fdc55e53be0b2bcfbd53d916f8ef1869c242ab918b02557aca95ab4568bc604917e83ce744f748719e2971a23fb283143fa31b
7
+ data.tar.gz: 834f7a2ee629aff135af47bf423e975fd57e77fefa07893b7be809d4f2f7747739534d7dbf269751240b02099bd5226fe100c58b2629c1b5e56adc9a970a054e
@@ -0,0 +1,22 @@
1
+ name: CI
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ test:
7
+ name: Test
8
+ strategy:
9
+ matrix:
10
+ ruby_version:
11
+ - '3.0'
12
+ - 2.7
13
+ fail-fast: false
14
+ runs-on: ubuntu-latest
15
+ steps:
16
+ - uses: actions/checkout@v2
17
+ - uses: ruby/setup-ruby@v1
18
+ with:
19
+ ruby-version: ${{ matrix.ruby_version }}
20
+ bundler-cache: true
21
+ - run: bundle exec rake spec
22
+
data/README.md CHANGED
@@ -36,7 +36,9 @@ Or install it yourself as:
36
36
 
37
37
  ## Usage
38
38
 
39
- First, you need to login Google Cloud Platform and enable APIs.
39
+ First, you need to login Google Cloud Platform and enable APIs. We support authentication with `User account` and `Service account`.
40
+
41
+ ### User authentication
40
42
 
41
43
  1. Open [API library page](https://console.developers.google.com/apis/library)
42
44
  2. Create/Select project
@@ -56,6 +58,23 @@ $ drive_env auth login
56
58
 
57
59
  Now you have access token and refresh token, you can access to Google APIs.
58
60
 
61
+ ### Service account authentication
62
+
63
+ 1. Open [API library page](https://console.developers.google.com/apis/library)
64
+ 2. Create/Select project
65
+ 3. Enable "Google Drive API" and "Google Sheets API"
66
+ 4. Open [Credentials page](https://console.developers.google.com/apis/credentials) in the same project
67
+ 5. Click "Create credentials" and choose "Service account key"
68
+ 6. Save credentials as JSON file
69
+ 7. Set `GOOGLE_APPLICATION_CREDENTIALS` environment variables with the value of the downloaded JSON file path.
70
+ ```
71
+ $ export GOOGLE_APPLICATION_CREDENTIALS="/path/to/service_account_credential.json"
72
+ ```
73
+
74
+ The application search `GOOGLE_APPLICATION_CREDENTIALS` first and then try to use user credential.
75
+
76
+ ### Access Spreadsheet
77
+
59
78
  Show Spreadsheet in Google Drive:
60
79
 
61
80
  ```
@@ -97,9 +116,47 @@ $ drive_env spreadsheet to_env sheet1 > .env
97
116
  $ your-ruby-application-with-dotenv-gem
98
117
  ```
99
118
 
119
+ #### Output format
120
+
121
+ JSON output is available by `--format=json` option.
122
+
123
+ ```
124
+ $ drive_env spreadsheet to_env sheet1 --format=json
125
+ [
126
+ {
127
+ "key": "RAILS_ENV",
128
+ "value": "production"
129
+ },
130
+ {
131
+ "key": "DATABASE_HOSTNAME",
132
+ "value": "x.x.x.x"
133
+ },
134
+ {
135
+ "key": "DATABASE_USERNAME",
136
+ "value": "appuser"
137
+ },
138
+ {
139
+ "key": "DATABASE_PASSWORD",
140
+ "value": "appuser"
141
+ },
142
+ {
143
+ "key": "SMTP_HOST",
144
+ "value": "x.x.x.x"
145
+ },
146
+ {
147
+ "key": "SMTP_USERNAME",
148
+ "value": "appuser"
149
+ },
150
+ {
151
+ "key": "SMTP_PASSWORD",
152
+ "value": "appuser"
153
+ }
154
+ ]
155
+ ```
156
+
100
157
  ## Development
101
158
 
102
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
159
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
103
160
 
104
161
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
105
162
 
@@ -111,4 +168,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/groove
111
168
  ## License
112
169
 
113
170
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
114
-
data/drive_env.gemspec CHANGED
@@ -29,11 +29,11 @@ Gem::Specification.new do |spec|
29
29
 
30
30
  spec.add_dependency "google_drive", "~> 3.0"
31
31
  spec.add_dependency "googleauth", "~> 0.5"
32
- spec.add_dependency "thor", "~> 0.19"
32
+ spec.add_dependency "thor", "~> 1.0"
33
33
  spec.add_dependency "text-table", "~> 1.2"
34
34
 
35
- spec.add_development_dependency "bundler", "~> 1.10"
36
- spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "bundler"
36
+ spec.add_development_dependency "rake"
37
37
  spec.add_development_dependency "rspec"
38
38
  spec.add_development_dependency "pry"
39
39
  end
@@ -2,6 +2,7 @@ require 'thor'
2
2
  require 'text-table'
3
3
  require 'google_drive'
4
4
  require 'drive_env'
5
+ require 'json'
5
6
 
6
7
  module DriveEnv
7
8
  module Cli
@@ -22,19 +23,34 @@ module DriveEnv
22
23
  end
23
24
 
24
25
  desc 'to_env SPREADSHEET_URL_OR_ALIAS', ''
26
+ option :format, type: 'string', default: 'dotenv', enum: %w[dotenv json],
27
+ desc: 'output format'
25
28
  def to_env(url_or_alias)
26
29
  ws = worksheet(url_or_alias)
30
+
31
+ envs = []
27
32
  ws.rows.each.with_index do |row, idx|
28
33
  row_to_env(row) do |key, value, comment|
34
+ envs << { key: key, value: value, comment: comment }
35
+ end
36
+ end
37
+
38
+ case options[:format]
39
+ when 'dotenv'
40
+ envs.each do |env|
29
41
  case
30
- when key && value && comment
31
- puts "#{key}=#{value} #{comment}"
32
- when key && value && !comment
33
- puts "#{key}=#{value}"
34
- when !key && !value && comment
35
- puts comment
42
+ when env[:key] && env[:value] && env[:comment]
43
+ puts "#{env[:key]}=#{env[:value]} #{env[:comment]}"
44
+ when env[:key] && env[:value] && !env[:comment]
45
+ puts "#{env[:key]}=#{env[:value]}"
46
+ when !env[:key] && !env[:value] && env[:comment]
47
+ puts "#{env[:comment]}"
36
48
  end
37
49
  end
50
+ when 'json'
51
+ puts JSON.pretty_generate(
52
+ envs.select{|env| env[:key] }.map{|env| { key: env[:key], value: env[:value] } }
53
+ )
38
54
  end
39
55
  end
40
56
 
@@ -75,14 +91,20 @@ module DriveEnv
75
91
 
76
92
  def credential
77
93
  unless @credential
78
- @credential = authorizer.get_credentials(DriveEnv::Config::DEFAULT_TOKEN_USER_ID)
79
- case
80
- when @credential.nil?
81
- abort "please set access_token: #{$0} auth login"
82
- when @credential.expired?
94
+ case authorizer
95
+ when Google::Auth::UserAuthorizer
96
+ @credential = authorizer.get_credentials(DriveEnv::Config::DEFAULT_TOKEN_USER_ID)
97
+ case
98
+ when @credential.nil?
99
+ abort "please set access_token: #{$0} auth login"
100
+ when @credential.expired?
101
+ @credential.fetch_access_token!
102
+ @credential.expires_at = credential.issued_at + credential.expires_in
103
+ authorizer.store_credentials(DriveEnv::Config::DEFAULT_TOKEN_USER_ID, @credential)
104
+ end
105
+ when Google::Auth::ServiceAccountCredentials
106
+ @credential = authorizer
83
107
  @credential.fetch_access_token!
84
- @credential.expires_at = credential.issued_at + credential.expires_in
85
- authorizer.store_credentials(DriveEnv::Config::DEFAULT_TOKEN_USER_ID, @credential)
86
108
  end
87
109
  end
88
110
  @credential
@@ -134,4 +156,3 @@ module DriveEnv
134
156
  end
135
157
  end
136
158
  end
137
-
@@ -1,3 +1,3 @@
1
1
  module DriveEnv
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.2'
3
3
  end
data/lib/drive_env.rb CHANGED
@@ -9,13 +9,21 @@ module DriveEnv
9
9
  class << self
10
10
  def authorizer(client_id, client_secret, token_store_file)
11
11
  unless @authorizer
12
- client_id = Google::Auth::ClientId.new(client_id, client_secret)
12
+ cred = ENV['GOOGLE_APPLICATION_CREDENTIALS']
13
13
  scope = %w[
14
14
  https://www.googleapis.com/auth/drive
15
15
  https://spreadsheets.google.com/feeds/
16
16
  ]
17
- token_store = Google::Auth::Stores::FileTokenStore.new(file: token_store_file)
18
- @authorizer = Google::Auth::UserAuthorizer.new(client_id, scope, token_store)
17
+ if cred.nil?
18
+ client_id = Google::Auth::ClientId.new(client_id, client_secret)
19
+ token_store = Google::Auth::Stores::FileTokenStore.new(file: token_store_file)
20
+ @authorizer = Google::Auth::UserAuthorizer.new(client_id, scope, token_store)
21
+ else
22
+ @authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
23
+ json_key_io: File.open(cred),
24
+ scope: scope
25
+ )
26
+ end
19
27
  end
20
28
  @authorizer
21
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: drive_env
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - YAMADA Tsuyoshi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-05 00:00:00.000000000 Z
11
+ date: 2021-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: google_drive
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0.19'
47
+ version: '1.0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0.19'
54
+ version: '1.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: text-table
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -70,30 +70,30 @@ dependencies:
70
70
  name: bundler
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: '1.10'
75
+ version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "~>"
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: '1.10'
82
+ version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rake
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - "~>"
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
- version: '10.0'
89
+ version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - "~>"
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
- version: '10.0'
96
+ version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: rspec
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -130,9 +130,9 @@ executables:
130
130
  extensions: []
131
131
  extra_rdoc_files: []
132
132
  files:
133
+ - ".github/workflows/ci.yml"
133
134
  - ".gitignore"
134
135
  - ".rspec"
135
- - ".travis.yml"
136
136
  - Gemfile
137
137
  - LICENSE.txt
138
138
  - README.md
@@ -169,8 +169,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
169
169
  - !ruby/object:Gem::Version
170
170
  version: '0'
171
171
  requirements: []
172
- rubyforge_project:
173
- rubygems_version: 2.7.6
172
+ rubygems_version: 3.3.3
174
173
  signing_key:
175
174
  specification_version: 4
176
175
  summary: Generate `.env` file from Spreadsheet in Google Drive
data/.travis.yml DELETED
@@ -1,5 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 2.5.3
4
- - 2.4.5
5
- - 2.3.8