glima 0.2.1 → 0.3.0

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
  SHA1:
3
- metadata.gz: d5f7b67ff62c0d7d8ec231492f2c8231e5f83dbd
4
- data.tar.gz: 16537a6a41967c56c017cc0fe541344c6d787cb6
3
+ metadata.gz: 27d1459f172ac0f758a2ee88cd5d99cb7f8b2daa
4
+ data.tar.gz: 933620cec4309b6ef67a6d845e3bd3adc6089b84
5
5
  SHA512:
6
- metadata.gz: '089186482f1ac400e4a999523640c38e18ce5632676ba65e83a14968b25170beefde1073d1fbe6150c806454f2df609d17a4777dbf3dcfea1918e092260ab017'
7
- data.tar.gz: 43e2325cbafdac0b0e243a1742191c77eadb76def1369ab364e42485e3d41ca010ad28bfc21fb27c41152660c4fcf4fa26cbd3280c98da3011228583cc1e5047
6
+ metadata.gz: f28c39166d7ba2f1ec4002140e2bb4a7120be0561334b38da9d63ab7ec02490abb2172f96b5646d03c3d23d458c02765d0cd76240ce674b01556e46a21a9e85f
7
+ data.tar.gz: 42c56a60f7cb35173827f1a91b50738cdddaed7db81bb796916bdf8cc5ff4335aeed1bff54b9458b257650301d4f5ee86e4623fa6a790777901bb9cac8cfc8d7
@@ -4,5 +4,5 @@ GENERAL:
4
4
  CLIENT_ID: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com"
5
5
  CLIENT_SECRET: "yyyyyyyyyyyyyyyyyyyyyyyy"
6
6
  TOKEN_STORE: "/Users/nom/.config/glima/token_store.yml"
7
- CONTEXT_STORE: "/Users/nom/.config/glima/context.yml"
7
+ CONTEXT_STORE_PATH: "/Users/nom/.config/glima/context.yml"
8
8
  DEFAULT_USER: "????????@gmail.com"
data/exe/glima CHANGED
@@ -23,19 +23,21 @@ if File.exists?(gemfile + ".lock")
23
23
  end
24
24
 
25
25
  require "rubygems"
26
+ require "clian"
26
27
  require "glima"
27
28
  require "cgi"
28
29
  require "logger"
29
30
 
30
31
  Encoding.default_external="UTF-8"
31
32
 
32
- class GlimaCLI < Glima::Cli
33
+ class GlimaCLI < Clian::Cli
33
34
  package_name 'GLIMA'
34
35
 
35
36
  ################################################################
36
37
  # global option
37
38
 
38
39
  class_option :profile, :desc => "Set profiler flag", :type => :boolean
40
+ class_option :user, :desc => "Set Gmail/IMAP account"
39
41
 
40
42
  ################################################################
41
43
  # register frequently used options
@@ -44,6 +46,20 @@ class GlimaCLI < Glima::Cli
44
46
  named_option :dry_run, :desc => "Perform a trial run with no changes made", :type => :boolean
45
47
  named_option :query, :desc => "Filter messages by QUERY"
46
48
 
49
+ ################################################################
50
+ # Command: auth
51
+ ################################################################
52
+ desc "auth", "Authenticate interactively"
53
+
54
+ def auth
55
+ conf = config.general
56
+ authorizer = Clian::Authorizer.new(conf.client_id,
57
+ conf.client_secret,
58
+ Google::Apis::GmailV1::AUTH_SCOPE,
59
+ conf.token_store_path)
60
+ authorizer.auth_interactively(@user)
61
+ end
62
+
47
63
  ################################################################
48
64
  # Command: dezip
49
65
  ################################################################
@@ -76,6 +92,18 @@ class GlimaCLI < Glima::Cli
76
92
  Glima::Command::Guess.new(message_id)
77
93
  end
78
94
 
95
+ ################################################################
96
+ # Command: init
97
+ ################################################################
98
+ desc "init", "Create new config file"
99
+
100
+ expand_option :config
101
+
102
+ def init
103
+ config_file = options[:config] || DEFAULT_CONFIG_PATH
104
+ Glima::Command::Init.new(config_file)
105
+ end
106
+
79
107
  ################################################################
80
108
  # Command: label
81
109
  ################################################################
@@ -218,18 +246,6 @@ class GlimaCLI < Glima::Cli
218
246
  del_dst_labels: parse_label_names(options[:del_dst_labels]))
219
247
  end
220
248
 
221
- ################################################################
222
- # add some hooks to Thor
223
-
224
- no_commands do
225
- def invoke_command(command, *args)
226
- setup_global_options unless command.name == "init"
227
- result = super
228
- teardown
229
- result
230
- end
231
- end
232
-
233
249
  ################################################################
234
250
  # private
235
251
 
@@ -249,19 +265,19 @@ class GlimaCLI < Glima::Cli
249
265
  end
250
266
  end
251
267
 
252
- attr_reader :builder, :config, :client, :context
268
+ attr_reader :builder, :config, :user, :context
253
269
 
254
- def setup_global_options
270
+ def setup_global_options(command, *args)
255
271
  exit_on_error do
256
- @config = Glima::Config.create_from_file(options[:config] || CONFIG_PATH)
272
+ @config = Glima::Config.create_from_file(options[:config] || DEFAULT_CONFIG_PATH) unless command.name == "init"
257
273
  @datastore = Glima::DataStore.new(File.expand_path("~/Mail"))
258
- @client = Glima::GmailClient.new(config.general, @datastore)
259
- @context = Glima::Context.new(File.expand_path(CONFIG_HOME))
274
+ @user = options[:user] || config.general.default_user
275
+ @context = Glima::Context.new(File.expand_path(DEFAULT_CONFIG_HOME))
260
276
 
261
277
  logger = ::Logger.new($stderr)
262
278
  logger.formatter = proc {|severity, datetime, progname, msg| "#{msg}\n"}
263
279
 
264
- Glima::Command.client = client
280
+ Glima::Command.client = client unless command.name == "auth"
265
281
  Glima::Command.logger = logger
266
282
  # Glima::GmailClient.logger = logger
267
283
 
@@ -276,6 +292,15 @@ class GlimaCLI < Glima::Cli
276
292
  end
277
293
  end
278
294
  end
295
+
296
+ def client
297
+ begin
298
+ @client ||= Glima::GmailClient.new(config.general, @user, @datastore)
299
+ rescue Glima::GmailClient::AuthorizationError
300
+ STDERR.print "Authorization Error: try ``glima auth'' command.\n"
301
+ exit(1)
302
+ end
303
+ end
279
304
  end
280
305
 
281
306
  def teardown
@@ -27,9 +27,7 @@ Gem::Specification.new do |spec|
27
27
  spec.required_ruby_version = ">= 2.3.0"
28
28
 
29
29
  spec.add_runtime_dependency "thor", ">= 0.19.1"
30
- spec.add_runtime_dependency "google-api-client", ">0.9"
31
- spec.add_runtime_dependency "googleauth"
32
- spec.add_runtime_dependency "launchy"
30
+ spec.add_runtime_dependency "clian", ">= 0.3.0"
33
31
  spec.add_runtime_dependency "mail"
34
32
  spec.add_runtime_dependency "rubyzip"
35
33
 
@@ -4,7 +4,6 @@ module Glima
4
4
 
5
5
  dir = File.dirname(__FILE__) + "/glima"
6
6
 
7
- autoload :Cli, "#{dir}/cli.rb"
8
7
  autoload :Command, "#{dir}/command.rb"
9
8
  autoload :Config, "#{dir}/config.rb"
10
9
  autoload :Context, "#{dir}/context.rb"
@@ -21,6 +21,7 @@ module Glima
21
21
  autoload :Guess, "#{dir}/guess.rb"
22
22
  autoload :Label, "#{dir}/label.rb"
23
23
  autoload :Labels, "#{dir}/labels.rb"
24
+ autoload :Init, "#{dir}/init.rb"
24
25
  # autoload :Open, "#{dir}/open.rb"
25
26
  autoload :Profile, "#{dir}/profile.rb"
26
27
  autoload :Push, "#{dir}/push.rb"
@@ -0,0 +1,99 @@
1
+ module Glima
2
+ module Command
3
+ class Init
4
+ TEMPLATE_DIR = File.expand_path("../../templates", __FILE__)
5
+
6
+ def initialize(config_file)
7
+ @shell = Thor.new
8
+ @status = {green: 0, yellow: 0, red: 0}
9
+ config = {}
10
+
11
+ config_dir = File.dirname(config_file)
12
+
13
+ if File.exists?(File.expand_path(config_file))
14
+ say_status "exist", "Ignore #{config_file}", :red
15
+ return
16
+ end
17
+
18
+ say "Creating #{config_file} ..."
19
+ say "Get your CLIENT_ID/CLIENT_SECRET at https://console.developers.google.com", :green
20
+ say "Googling 'Creating a Google API Console project and client ID' would help."
21
+
22
+ config[:client_id] = @shell.ask "CLIENT_ID:"
23
+ config[:client_secret] = @shell.ask "CLIENT_SECRET:"
24
+ config[:default_user] = @shell.ask "Gmail address:"
25
+ config[:token_store_path] = File.expand_path("token_store.yml", config_dir)
26
+
27
+ # mkdir
28
+ unless Dir.exist?(File.expand_path(config_dir))
29
+ say "Making config directory #{config_dir} ..."
30
+ mkdir_p(File.expand_path(config_dir))
31
+ end
32
+
33
+ # make config file from tamplate
34
+ say "Copying file(s) into #{config_file} ..."
35
+ src = File.expand_path("config.yml.erb", TEMPLATE_DIR)
36
+ dst = File.expand_path(config_file)
37
+ expand_template(src, dst, config)
38
+
39
+ say_status_report
40
+ end
41
+
42
+ private
43
+
44
+ def say(message, color = nil)
45
+ @shell.say(message, color)
46
+ end
47
+
48
+ def say_status(status, message, log_status = nil)
49
+ @status[log_status] += 1
50
+ @shell.say_status(status, message, log_status)
51
+ end
52
+
53
+ def say_status_report
54
+ if (errors = @status[:red]) > 0
55
+ say "#{errors} error(s) were occurred.", :red
56
+ else
57
+ say "done."
58
+ end
59
+ end
60
+
61
+ def expand_template(template_path, dest_path, config)
62
+ require "erb"
63
+ template = ERB.new(File.open(template_path).read, nil, "-")
64
+
65
+ if File.exists?(dest_path)
66
+ say_status "exist", "Ignore #{dest_path}", :yellow
67
+ return
68
+ end
69
+
70
+ begin
71
+ mkdir_p(File.expand_path("..", dest_path))
72
+ File.open(dest_path, "w", 0600) do |file|
73
+ file.write(template.result(binding))
74
+ end
75
+ say_status "ok", "copy #{dest_path}", :green
76
+ rescue StandardError => e
77
+ say_status "failed", "#{e.message.split(' @').first} #{dest_path}", :red
78
+ end
79
+ end
80
+
81
+ def mkdir_p(path)
82
+ path = File.expand_path(path)
83
+
84
+ if File.directory?(path)
85
+ say_status "exist", "Ignore #{path}", :yellow
86
+ return
87
+ end
88
+
89
+ begin
90
+ FileUtils.mkdir_p(path)
91
+ say_status "create", "#{path}", :green
92
+ rescue StandardError => e
93
+ say_status "failed", "#{e.message.split(' @').first} #{path}", :red
94
+ end
95
+ end
96
+
97
+ end # class Init
98
+ end # module Command
99
+ end # module Glima
@@ -1,205 +1,16 @@
1
- require 'yaml'
2
- require 'pp'
3
-
4
1
  module Glima
5
- class Config
6
- # Syntax table manipulation
7
- class Syntax
8
- def initialize(syntax_config)
9
- @syntax_config = syntax_config
10
- end
11
-
12
- def keyword_symbols
13
- @syntax_config.keys
14
- end
15
-
16
- def keywords
17
- keyword_symbols.map {|sym| sym.to_s.upcase }
18
- end
19
-
20
- def keyword?(word)
21
- if word.is_a?(Symbol)
22
- keyword_symbols.member?(word)
23
- else
24
- # String
25
- keywords.member?(word)
26
- end
27
- end
28
-
29
- def instance_variable_name(word)
30
- return nil unless keyword?(word)
31
- return '@' + as_symbol(word).to_s
32
- end
33
-
34
- def item_class(word)
35
- return nil unless keyword?(word)
36
- @syntax_config[as_symbol(word)]
37
- end
38
-
39
- private
40
- def as_symbol(word)
41
- word.to_s.downcase.sub(/^@+/, "").to_sym
42
- end
43
- end # class Syntax
44
-
45
- # Parse Key-Value object in YAML
46
- class Base
47
- # attr_accessor :name
48
-
49
- def self.create_from_yaml_file(yaml_file)
50
- yaml_string = File.open(File.expand_path(yaml_file)).read
51
- return create_from_yaml_string(yaml_string, yaml_file)
52
- end
53
-
54
- def self.create_from_yaml_string(yaml_string, filename = nil)
55
- hash = YAML.load(yaml_string, filename) || {}
56
- return new(hash)
57
- end
58
-
59
- def self.define_syntax(config)
60
- @syntax = Syntax.new(config)
61
- @syntax.keyword_symbols.each do |sym|
62
- attr_accessor sym # XXX: attr_reader is enough?
63
- end
64
- end
65
-
66
- def self.syntax
67
- return @syntax
68
- end
69
-
70
- def initialize(hash = {})
71
- @original_hash = hash
72
- (hash || {}).each do |key, val|
73
- raise Glima::ConfigurationError, "config syntax error (#{key})" unless syntax.keyword?(key)
74
- var = syntax.instance_variable_name(key)
75
- obj = create_subnode(key, val)
76
- instance_variable_set(var, obj)
77
- end
78
- end
79
-
80
- attr_reader :original_hash
81
-
82
- def get_value(dot_separated_string = nil)
83
- if dot_separated_string.to_s == ""
84
- return original_hash
85
- end
86
-
87
- key, subkey = dot_separated_string.to_s.upcase.split(".", 2)
88
- subnode = get_subnode(key)
89
-
90
- if subnode.respond_to?(:get_value)
91
- return subnode.get_value(subkey)
92
- else
93
- return subnode.to_s
94
- end
95
- end
2
+ class Config < Clian::Config::Toplevel
96
3
 
97
- def to_yaml
98
- return self.to_hash.to_yaml
99
- end
100
-
101
- def to_hash
102
- hash = {}
103
- syntax.keywords.each do |key|
104
- var = syntax.instance_variable_name(key)
105
- obj = instance_variable_get(var)
106
- obj = obj.respond_to?(:to_hash) ? obj.to_hash : obj.to_s
107
- hash[key] = obj
108
- end
109
- return hash
110
- end
111
-
112
- private
113
- def syntax
114
- self.class.syntax
115
- end
116
-
117
- def get_subnode(key)
118
- raise Glima::ConfigurationError, "Invalid key: #{key}" unless syntax.keyword?(key)
119
- return instance_variable_get(syntax.instance_variable_name(key))
120
- end
121
-
122
- def create_subnode(keyword, value)
123
- item_class = syntax.item_class(keyword)
124
- if item_class.is_a?(Array)
125
- return List.new(item_class.first, value)
126
- elsif item_class == String
127
- return value.to_s
128
- else
129
- return item_class.new(value)
130
- end
131
- end
132
-
133
- end # class Base
134
-
135
- # Parse Array object in YAML
136
- class List < Base
137
- include Enumerable
138
-
139
- def initialize(item_class, array = [])
140
- @original_hash = array
141
- @configs = []
142
- (array || []).each do |value|
143
- item = item_class.new(value)
144
- @configs << item
145
- end
146
- end
147
-
148
- def [](key)
149
- @configs.find {|c| c.name == key}
150
- end
151
-
152
- alias_method :get_subnode, :[]
153
-
154
- def <<(conf)
155
- @configs << conf
156
- end
157
-
158
- def to_hash # XXX: actually, it returns a Array
159
- return @configs.map {|c| c.respond_to?(:to_hash) ? c.to_hash : c.to_s}
160
- end
161
-
162
- def each
163
- @configs.each do |conf|
164
- yield conf
165
- end
166
- end
167
- end # List
168
-
169
- ## concrete config classes
170
-
171
- class General < Base
4
+ class General < Clian::Config::Element
172
5
  define_syntax :client_id => String,
173
6
  :client_secret => String,
174
- :token_store => String,
7
+ :token_store_path => String,
175
8
  :context_store => String,
176
9
  :zip_passwords_file => String,
177
10
  :default_user => String
178
11
  end # class General
179
12
 
180
- # Top-Level Config
181
- class Top < Base
182
- define_syntax :general => General
183
- end # class Top
184
-
185
- def self.create_from_file(file_name)
186
- unless File.exists?(File.expand_path(file_name))
187
- raise Glima::ConfigurationError, "config file '#{file_name}' not found"
188
- end
189
- begin
190
- return Top.create_from_yaml_file(file_name)
191
- rescue Psych::SyntaxError => e
192
- raise Glima::ConfigurationError, e.message
193
- end
194
- end
195
-
196
- def self.create_from_string(string)
197
- begin
198
- return Top.create_from_yaml_string(string)
199
- rescue Psych::SyntaxError => e
200
- raise Glima::ConfigurationError, e.message
201
- end
202
- end
13
+ define_syntax :general => General
203
14
 
204
- end # class Config
205
- end # module Glima
15
+ end # Config
16
+ end # Glima
@@ -8,41 +8,9 @@ require 'forwardable'
8
8
  Google::Apis::RequestOptions.default.retries = 5
9
9
 
10
10
  module Glima
11
- class Authorizer
12
- def initialize(client_id, client_secret, scope, token_store_path)
13
- @authorizer = Google::Auth::UserAuthorizer.new(
14
- Google::Auth::ClientId.new(client_id, client_secret),
15
- scope,
16
- Google::Auth::Stores::FileTokenStore.new(file: token_store_path)
17
- )
18
- end
19
-
20
- def credentials(user_id)
21
- @authorizer.get_credentials(user_id)
22
- end
23
-
24
- def auth_interactively(user_id)
25
- shell = Thor.new.shell
26
- oob_uri = "urn:ietf:wg:oauth:2.0:oob"
27
-
28
- url = @authorizer.get_authorization_url(base_url: oob_uri)
29
- begin
30
- Launchy.open(url)
31
- rescue
32
- puts "Open URL in your browser:\n #{url}"
33
- end
34
-
35
- code = shell.ask "Enter the resulting code:"
36
-
37
- @authorizer.get_and_store_credentials_from_code(
38
- user_id: user_id,
39
- code: code,
40
- base_url: oob_uri
41
- )
42
- end
43
- end # Authorizer
44
-
45
11
  class GmailClient
12
+ class AuthorizationError < StandardError ; end
13
+
46
14
  def self.logger
47
15
  Google::Apis.logger
48
16
  end
@@ -124,19 +92,21 @@ module Glima
124
92
  }.map(&:addr).map(&:ip_address).length > 0
125
93
  end
126
94
 
127
- def initialize(config, datastore)
128
- authorizer = Authorizer.new(config.client_id,
95
+ def initialize(config, user, datastore)
96
+ authorizer = Clian::Authorizer.new(config.client_id,
129
97
  config.client_secret,
130
98
  Google::Apis::GmailV1::AUTH_SCOPE,
131
- config.token_store)
99
+ config.token_store_path)
100
+
101
+ unless credentials = authorizer.credentials(user)
102
+ raise AuthorizationError.new
103
+ end
132
104
 
133
- credentials = authorizer.credentials(config.default_user) ||
134
- authorizer.auth_interactively(config.default_user)
135
105
  @datastore = datastore
136
106
  @client = Google::Apis::GmailV1::GmailService.new
137
107
  @client.client_options.application_name = 'glima'
138
108
  @client.authorization = credentials
139
- @client.authorization.username = config.default_user # for IMAP
109
+ @client.authorization.username = user # for IMAP
140
110
 
141
111
  return @client
142
112
  end
@@ -0,0 +1,7 @@
1
+ ################################################################
2
+ GENERAL:
3
+ ################################################################
4
+ CLIENT_ID: "<%= config[:client_id] %>"
5
+ CLIENT_SECRET: "<%= config[:client_secret] %>"
6
+ TOKEN_STORE_PATH: "<%= config[:token_store_path] %>"
7
+ DEFAULT_USER: "<%= config[:default_user] %>"
@@ -1,3 +1,3 @@
1
1
  module Glima
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glima
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoshinari Nomura
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-05-18 00:00:00.000000000 Z
11
+ date: 2017-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -25,47 +25,19 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.19.1
27
27
  - !ruby/object:Gem::Dependency
28
- name: google-api-client
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">"
32
- - !ruby/object:Gem::Version
33
- version: '0.9'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">"
39
- - !ruby/object:Gem::Version
40
- version: '0.9'
41
- - !ruby/object:Gem::Dependency
42
- name: googleauth
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: launchy
28
+ name: clian
57
29
  requirement: !ruby/object:Gem::Requirement
58
30
  requirements:
59
31
  - - ">="
60
32
  - !ruby/object:Gem::Version
61
- version: '0'
33
+ version: 0.3.0
62
34
  type: :runtime
63
35
  prerelease: false
64
36
  version_requirements: !ruby/object:Gem::Requirement
65
37
  requirements:
66
38
  - - ">="
67
39
  - !ruby/object:Gem::Version
68
- version: '0'
40
+ version: 0.3.0
69
41
  - !ruby/object:Gem::Dependency
70
42
  name: mail
71
43
  requirement: !ruby/object:Gem::Requirement
@@ -157,11 +129,11 @@ files:
157
129
  - exe/glima
158
130
  - glima.gemspec
159
131
  - lib/glima.rb
160
- - lib/glima/cli.rb
161
132
  - lib/glima/command.rb
162
133
  - lib/glima/command/base.rb
163
134
  - lib/glima/command/dezip.rb
164
135
  - lib/glima/command/guess.rb
136
+ - lib/glima/command/init.rb
165
137
  - lib/glima/command/label.rb
166
138
  - lib/glima/command/labels.rb
167
139
  - lib/glima/command/profile.rb
@@ -182,6 +154,7 @@ files:
182
154
  - lib/glima/resource/label.rb
183
155
  - lib/glima/resource/mail.rb
184
156
  - lib/glima/resource/message.rb
157
+ - lib/glima/templates/config.yml.erb
185
158
  - lib/glima/version.rb
186
159
  - lib/glima/zip.rb
187
160
  - spec/glima_spec.rb
@@ -1,151 +0,0 @@
1
- require "thor"
2
-
3
- module Glima
4
- class Cli < Thor
5
- ################################################################
6
- # config files
7
-
8
- CONFIG_HOME = File.join((ENV["XDG_CONFIG_HOME"] || "~/.config"), basename)
9
- CONFIG_FILE = "config.yml"
10
- CONFIG_PATH = File.join(CONFIG_HOME, CONFIG_FILE)
11
-
12
- def self.config_home ; CONFIG_HOME; end
13
- def self.config_path ; CONFIG_PATH; end
14
-
15
- ################################################################
16
- # register preset options
17
-
18
- def self.named_option(name, options)
19
- @named_options ||= {}
20
- @named_options[name] = options
21
- end
22
-
23
- def self.expand_option(*names)
24
- expand_named_option(:method, *names)
25
- end
26
-
27
- def self.expand_class_option(*names)
28
- expand_named_option(:class, *names)
29
- end
30
-
31
- def self.expand_named_option(type, *names)
32
- names.each do |name|
33
- options = @named_options[name]
34
- if type == :class
35
- class_option name, options
36
- else
37
- method_option name, options
38
- end
39
- end
40
- end
41
- private_class_method :expand_named_option
42
-
43
- named_option :debug, :desc => "Set debug flag", :type => :boolean
44
- named_option :profile, :desc => "Set profiler flag", :type => :boolean
45
- named_option :config, :desc => "Set config path (default: #{CONFIG_PATH})", :banner => "FILE"
46
- named_option :dry_run, :desc => "Perform a trial run with no changes made", :type => :boolean
47
-
48
- ################################################################
49
- # command name mappings
50
-
51
- map ["--version", "-v"] => :version
52
-
53
- map ["--help", "-h"] => :help
54
-
55
- default_command :help
56
-
57
- ################################################################
58
- # Command: help
59
- ################################################################
60
-
61
- desc "help [COMMAND]", "Describe available commands or one specific command"
62
-
63
- def help(command = nil)
64
- super(command)
65
- end
66
-
67
- ################################################################
68
- # Command: version
69
- ################################################################
70
- desc "version", "Show version"
71
-
72
- def version
73
- puts Glima::VERSION
74
- end
75
-
76
- ################################################################
77
- # Command: completions
78
- ################################################################
79
- check_unknown_options! :except => :completions
80
-
81
- desc "completions [COMMAND]", "List available commands or options for COMMAND", :hide => true
82
-
83
- long_desc <<-LONGDESC
84
- List available commands or options for COMMAND
85
- This is supposed to be a zsh compsys helper"
86
- LONGDESC
87
-
88
- def completions(*command)
89
- help = self.class.commands
90
- global_options = self.class.class_options
91
- Glima::Command::Completions.new(help, global_options, command, config)
92
- end
93
-
94
- ################################################################
95
- # add some hooks to Thor
96
-
97
- no_commands do
98
- def invoke_command(command, *args)
99
- setup_global_options unless command.name == "init"
100
- result = super
101
- teardown
102
- result
103
- end
104
- end
105
-
106
- ################################################################
107
- # private
108
-
109
- private
110
-
111
- def exit_on_error(&block)
112
- begin
113
- yield if block_given?
114
- rescue Glima::ConfigurationError => e
115
- STDERR.print "ERROR: #{e.message}.\n"
116
- exit 1
117
- end
118
- end
119
-
120
- attr_reader :builder, :config, :client, :context, :datastore
121
-
122
- def setup_global_options
123
- exit_on_error do
124
- if options[:profile]
125
- require 'profiler'
126
- Profiler__.start_profile
127
- end
128
- if options[:debug]
129
- require "pp"
130
- $GLIMA_DEBUG = true
131
- $GLIMA_DEBUG_FOR_DEVELOPER = true if ENV["GLIMA_DEBUG_FOR_DEVELOPER"]
132
- end
133
- end
134
- end
135
-
136
- def load_plugins
137
- config_path = options[:config] || CONFIG_PATH
138
- plugin_dir = File.dirname(config_path)
139
-
140
- Dir.glob(File.expand_path("plugins/*.rb", plugin_dir)) do |rb|
141
- require rb
142
- end
143
- end
144
-
145
- def teardown
146
- if options[:profile]
147
- Profiler__.print_profile($stdout)
148
- end
149
- end
150
- end
151
- end