clian 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +52 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/clian.gemspec +37 -0
- data/lib/clian/authorizer.rb +44 -0
- data/lib/clian/cli.rb +203 -0
- data/lib/clian/config.rb +205 -0
- data/lib/clian/version.rb +3 -0
- data/lib/clian.rb +12 -0
- metadata +170 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 264474ee5d137de6c6e52c4be3bc48823b49e1df
|
4
|
+
data.tar.gz: af85fb3abf76f468645c2441219dc4f01fa275d7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1abf1a099b6ce0fba76dbdf54d56d02f862775f11526682994e19b7faaa26dec1d5d41eab1f950d4752f04c542e2c39da6f976a7654706d8002b46526ffc8da2
|
7
|
+
data.tar.gz: d6f70db664e52fbe32c3faf942d6c9fc2dd4a36cd55fdbccf94908351ebee4f9153e66ef8be74c617aa6f3eb81ba887f01e0b8b045298c3f812df117e1a8598c
|
data/.gitignore
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/Gemfile.lock
|
4
|
+
/_yardoc/
|
5
|
+
/coverage/
|
6
|
+
/doc/
|
7
|
+
/pkg/
|
8
|
+
/spec/reports/
|
9
|
+
/tmp/
|
10
|
+
#
|
11
|
+
# Added from https://raw.github.com/github/gitignore/master/Ruby.gitignore
|
12
|
+
#
|
13
|
+
*.gem
|
14
|
+
*.rbc
|
15
|
+
/.config
|
16
|
+
/coverage/
|
17
|
+
/InstalledFiles
|
18
|
+
/pkg/
|
19
|
+
/spec/reports/
|
20
|
+
/spec/examples.txt
|
21
|
+
/test/tmp/
|
22
|
+
/test/version_tmp/
|
23
|
+
/tmp/
|
24
|
+
|
25
|
+
## Specific to RubyMotion:
|
26
|
+
.dat*
|
27
|
+
.repl_history
|
28
|
+
build/
|
29
|
+
|
30
|
+
## Documentation cache and generated files:
|
31
|
+
/.yardoc/
|
32
|
+
/_yardoc/
|
33
|
+
/doc/
|
34
|
+
/rdoc/
|
35
|
+
|
36
|
+
## Environment normalization:
|
37
|
+
/.bundle/
|
38
|
+
/vendor/bundle
|
39
|
+
/lib/bundler/man/
|
40
|
+
|
41
|
+
# for a library or gem, you might want to ignore these files since the code is
|
42
|
+
# intended to run in multiple environments; otherwise, check them in:
|
43
|
+
# Gemfile.lock
|
44
|
+
# .ruby-version
|
45
|
+
# .ruby-gemset
|
46
|
+
|
47
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
48
|
+
.rvmrc
|
49
|
+
#
|
50
|
+
# Added from /Users/nom/sys/bin/gem-new
|
51
|
+
#
|
52
|
+
/vendor
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Yoshinari Nomura
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "clian"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/clian.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
git = File.expand_path('../.git', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'clian/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "clian"
|
9
|
+
spec.version = Clian::VERSION
|
10
|
+
spec.authors = ["Yoshinari Nomura"]
|
11
|
+
spec.email = ["nom@quickhack.net"]
|
12
|
+
|
13
|
+
spec.summary = %q{Small set of Ruby classes helpful for creation of CLI tools.}
|
14
|
+
spec.description = %q{Small set of Ruby classes helpful for creation of CLI tools.}
|
15
|
+
spec.homepage = "https://github.com/yoshinari-nomura/clian"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.files = if Dir.exist?(git)
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
else
|
21
|
+
Dir['**/*']
|
22
|
+
end
|
23
|
+
|
24
|
+
spec.bindir = "exe"
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ["lib"]
|
27
|
+
|
28
|
+
spec.add_runtime_dependency "thor", ">= 0.19.1"
|
29
|
+
spec.add_runtime_dependency "google-api-client", "0.9.pre4"
|
30
|
+
spec.add_runtime_dependency "googleauth"
|
31
|
+
spec.add_runtime_dependency "launchy"
|
32
|
+
spec.add_runtime_dependency "mail"
|
33
|
+
|
34
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
35
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
36
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
37
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'google/apis/gmail_v1'
|
2
|
+
require 'googleauth'
|
3
|
+
require 'googleauth/stores/file_token_store'
|
4
|
+
require 'launchy'
|
5
|
+
require "thor"
|
6
|
+
|
7
|
+
module Clian
|
8
|
+
# https://github.com/google/google-auth-library-ruby#example-command-line
|
9
|
+
# https://github.com/google/google-api-ruby-client#example-usage
|
10
|
+
# http://stackoverflow.com/questions/12572723/rails-google-client-api-unable-to-exchange-a-refresh-token-for-access-token
|
11
|
+
#
|
12
|
+
class Authorizer
|
13
|
+
def initialize(client_id, client_secret, scope, token_store_path)
|
14
|
+
@authorizer = Google::Auth::UserAuthorizer.new(
|
15
|
+
Google::Auth::ClientId.new(client_id, client_secret),
|
16
|
+
scope,
|
17
|
+
Google::Auth::Stores::FileTokenStore.new(file: token_store_path)
|
18
|
+
)
|
19
|
+
end
|
20
|
+
|
21
|
+
def credentials(user_id = "default")
|
22
|
+
@authorizer.get_credentials(user_id)
|
23
|
+
end
|
24
|
+
|
25
|
+
def auth_interactively(user_id = "default", 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
|
+
end # Clian
|
data/lib/clian/cli.rb
ADDED
@@ -0,0 +1,203 @@
|
|
1
|
+
require "thor"
|
2
|
+
|
3
|
+
module Clian
|
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
|
+
# rbenv support:
|
17
|
+
# If this file is a symlink, and bound to a specific ruby
|
18
|
+
# version via rbenv (indicated by RBENV_VERSION),
|
19
|
+
# I want to resolve the symlink and re-exec
|
20
|
+
# the original executable respecting the .ruby_version
|
21
|
+
# which should indicate the right version.
|
22
|
+
#
|
23
|
+
def self.bundler_rbenv_setup(command_path)
|
24
|
+
if File.symlink?(command_path) and ENV["RBENV_VERSION"]
|
25
|
+
ENV["RBENV_VERSION"] = nil
|
26
|
+
shims_path = File.expand_path("shims", ENV["RBENV_ROOT"])
|
27
|
+
ENV["PATH"] = shims_path + ":" + ENV["PATH"]
|
28
|
+
exec(File.readlink(__FILE__), *ARGV)
|
29
|
+
end
|
30
|
+
|
31
|
+
gemfile = File.expand_path("../../Gemfile", command_path)
|
32
|
+
|
33
|
+
if File.exists?(gemfile + ".lock")
|
34
|
+
ENV["BUNDLE_GEMFILE"] = gemfile
|
35
|
+
require "bundler/setup"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
################################################################
|
40
|
+
# register preset options
|
41
|
+
|
42
|
+
def self.named_option(name, options)
|
43
|
+
@named_options ||= {}
|
44
|
+
@named_options[name] = options
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.expand_option(*names)
|
48
|
+
expand_named_option(:method, *names)
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.expand_class_option(*names)
|
52
|
+
expand_named_option(:class, *names)
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.expand_named_option(type, *names)
|
56
|
+
names.each do |name|
|
57
|
+
options = @named_options[name]
|
58
|
+
if type == :class
|
59
|
+
class_option name, options
|
60
|
+
else
|
61
|
+
method_option name, options
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
private_class_method :expand_named_option
|
66
|
+
|
67
|
+
# default options
|
68
|
+
named_option :debug, :desc => "Set debug flag", :type => :boolean
|
69
|
+
named_option :profile, :desc => "Set profiler flag", :type => :boolean
|
70
|
+
named_option :config, :desc => "Set config path (default: #{CONFIG_PATH})", :banner => "FILE"
|
71
|
+
named_option :dry_run, :desc => "Perform a trial run with no changes made", :type => :boolean
|
72
|
+
|
73
|
+
################################################################
|
74
|
+
# command name mappings
|
75
|
+
|
76
|
+
map ["--version", "-v"] => :version
|
77
|
+
|
78
|
+
map ["--help", "-h"] => :help
|
79
|
+
|
80
|
+
default_command :help
|
81
|
+
|
82
|
+
################################################################
|
83
|
+
# Command: help
|
84
|
+
################################################################
|
85
|
+
|
86
|
+
desc "help [COMMAND]", "Describe available commands or one specific command"
|
87
|
+
|
88
|
+
def help(command = nil)
|
89
|
+
super(command)
|
90
|
+
end
|
91
|
+
|
92
|
+
################################################################
|
93
|
+
# Command: version
|
94
|
+
################################################################
|
95
|
+
desc "version", "Show version"
|
96
|
+
|
97
|
+
def version
|
98
|
+
puts Clian::VERSION
|
99
|
+
end
|
100
|
+
|
101
|
+
################################################################
|
102
|
+
# Command: completions
|
103
|
+
################################################################
|
104
|
+
check_unknown_options! :except => :completions
|
105
|
+
|
106
|
+
desc "completions [COMMAND]", "List available commands or options for COMMAND", :hide => true
|
107
|
+
|
108
|
+
long_desc <<-LONGDESC
|
109
|
+
List available commands or options for COMMAND
|
110
|
+
This is supposed to be a zsh compsys helper"
|
111
|
+
LONGDESC
|
112
|
+
|
113
|
+
def completions(*command)
|
114
|
+
help = self.class.commands
|
115
|
+
global_options = self.class.class_options
|
116
|
+
Clian::Command::Completions.new(help, global_options, command, config)
|
117
|
+
end
|
118
|
+
|
119
|
+
################################################################
|
120
|
+
# Command: config
|
121
|
+
################################################################
|
122
|
+
desc "configuration", "Show current configuration in various formats."
|
123
|
+
|
124
|
+
def configuration(name = nil)
|
125
|
+
puts Clian::Converter::Emacs.new.to_emacs(config.get_value(name))
|
126
|
+
end
|
127
|
+
|
128
|
+
################################################################
|
129
|
+
# add some hooks to Thor
|
130
|
+
|
131
|
+
no_commands do
|
132
|
+
def invoke_command(command, *args)
|
133
|
+
setup_global_options unless command.name == "init"
|
134
|
+
result = super
|
135
|
+
teardown
|
136
|
+
result
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
################################################################
|
141
|
+
# private
|
142
|
+
|
143
|
+
private
|
144
|
+
|
145
|
+
def exit_on_error(&block)
|
146
|
+
begin
|
147
|
+
yield if block_given?
|
148
|
+
rescue Clian::ConfigurationError => e
|
149
|
+
STDERR.print "ERROR: #{e.message}.\n"
|
150
|
+
exit 1
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
attr_reader :builder, :config, :calendar
|
155
|
+
|
156
|
+
def setup_global_options
|
157
|
+
exit_on_error do
|
158
|
+
# @config = Clian::Config.create_from_file(options[:config] || CONFIG_PATH)
|
159
|
+
# @builder ||= Clian::Builder.new(@config)
|
160
|
+
# if @config.general.tzid
|
161
|
+
# Clian.default_tzid = @config.general.tzid
|
162
|
+
#end
|
163
|
+
|
164
|
+
# calname = options[:calendar] || @config.calendars.first.name
|
165
|
+
# @config.general.repository = options[:repository] if options[:repository]
|
166
|
+
|
167
|
+
# self.class.calendar ||= builder.calendar(calname)
|
168
|
+
# @calendar = self.class.calendar
|
169
|
+
end
|
170
|
+
|
171
|
+
# load_plugins
|
172
|
+
|
173
|
+
# if options[:profile]
|
174
|
+
# require 'profiler'
|
175
|
+
# Profiler__.start_profile
|
176
|
+
# end
|
177
|
+
# if options[:debug]
|
178
|
+
# require "pp"
|
179
|
+
# $MHC_DEBUG = true
|
180
|
+
# $MHC_DEBUG_FOR_DEVELOPER = true if ENV["MHC_DEBUG_FOR_DEVELOPER"]
|
181
|
+
# end
|
182
|
+
end
|
183
|
+
|
184
|
+
def load_plugins
|
185
|
+
config_path = options[:config] || DEFAULT_CONFIG_PATH
|
186
|
+
plugin_dir = File.dirname(config_path)
|
187
|
+
|
188
|
+
Dir.glob(File.expand_path("plugins/*.rb", plugin_dir)) do |rb|
|
189
|
+
require rb
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
def teardown
|
194
|
+
if options[:profile]
|
195
|
+
Profiler__.print_profile($stdout)
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
def symbolize_keys(hash)
|
200
|
+
Hash[hash.map {|k,v| [k.to_sym, v]}]
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
data/lib/clian/config.rb
ADDED
@@ -0,0 +1,205 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'pp'
|
3
|
+
|
4
|
+
module Clian
|
5
|
+
module Config
|
6
|
+
################################################################
|
7
|
+
# Syntax table manipulation
|
8
|
+
class Syntax
|
9
|
+
def initialize(syntax_config)
|
10
|
+
@syntax_config = syntax_config
|
11
|
+
end
|
12
|
+
|
13
|
+
def keyword_symbols
|
14
|
+
@syntax_config.keys
|
15
|
+
end
|
16
|
+
|
17
|
+
def keywords
|
18
|
+
keyword_symbols.map {|sym| sym.to_s.upcase }
|
19
|
+
end
|
20
|
+
|
21
|
+
def keyword?(word)
|
22
|
+
if word.is_a?(Symbol)
|
23
|
+
keyword_symbols.member?(word)
|
24
|
+
else
|
25
|
+
# String
|
26
|
+
keywords.member?(word)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def instance_variable_name(word)
|
31
|
+
return nil unless keyword?(word)
|
32
|
+
return '@' + as_symbol(word).to_s
|
33
|
+
end
|
34
|
+
|
35
|
+
def item_class(word)
|
36
|
+
return nil unless keyword?(word)
|
37
|
+
@syntax_config[as_symbol(word)]
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
def as_symbol(word)
|
42
|
+
word.to_s.downcase.sub(/^@+/, "").to_sym
|
43
|
+
end
|
44
|
+
end # class Syntax
|
45
|
+
|
46
|
+
################################################################
|
47
|
+
# Parse Key-Value object in YAML
|
48
|
+
class Element
|
49
|
+
|
50
|
+
def self.create_from_yaml_file(yaml_file)
|
51
|
+
yaml_string = File.open(File.expand_path(yaml_file)).read
|
52
|
+
return create_from_yaml_string(yaml_string, yaml_file)
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.create_from_yaml_string(yaml_string, filename = nil)
|
56
|
+
hash = YAML.load(yaml_string, filename) || {}
|
57
|
+
return new(hash)
|
58
|
+
end
|
59
|
+
|
60
|
+
# class General < Clian::Config::Element
|
61
|
+
# define_syntax :client_id => String,
|
62
|
+
# :client_secret => String,
|
63
|
+
# :token_store => String,
|
64
|
+
# :context_store => String,
|
65
|
+
# :default_user => String
|
66
|
+
# end # class General
|
67
|
+
#
|
68
|
+
def self.define_syntax(config)
|
69
|
+
@syntax = Syntax.new(config)
|
70
|
+
@syntax.keyword_symbols.each do |sym|
|
71
|
+
attr_reader sym
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def self.syntax
|
76
|
+
return @syntax
|
77
|
+
end
|
78
|
+
|
79
|
+
def initialize(hash = {})
|
80
|
+
@original_hash = hash
|
81
|
+
(hash || {}).each do |key, val|
|
82
|
+
raise Clian::ConfigurationError, "config syntax error (#{key})" unless syntax.keyword?(key)
|
83
|
+
var = syntax.instance_variable_name(key)
|
84
|
+
obj = create_subnode(key, val)
|
85
|
+
instance_variable_set(var, obj)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
attr_reader :original_hash
|
90
|
+
|
91
|
+
def get_value(dot_separated_string = nil)
|
92
|
+
if dot_separated_string.to_s == ""
|
93
|
+
return original_hash
|
94
|
+
end
|
95
|
+
|
96
|
+
key, subkey = dot_separated_string.to_s.upcase.split(".", 2)
|
97
|
+
subnode = get_subnode(key)
|
98
|
+
|
99
|
+
if subnode.respond_to?(:get_value)
|
100
|
+
return subnode.get_value(subkey)
|
101
|
+
else
|
102
|
+
return subnode.to_s
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def to_yaml
|
107
|
+
return self.to_hash.to_yaml
|
108
|
+
end
|
109
|
+
|
110
|
+
def to_hash
|
111
|
+
hash = {}
|
112
|
+
syntax.keywords.each do |key|
|
113
|
+
var = syntax.instance_variable_name(key)
|
114
|
+
obj = instance_variable_get(var)
|
115
|
+
obj = obj.respond_to?(:to_hash) ? obj.to_hash : obj.to_s
|
116
|
+
hash[key] = obj
|
117
|
+
end
|
118
|
+
return hash
|
119
|
+
end
|
120
|
+
|
121
|
+
private
|
122
|
+
def syntax
|
123
|
+
self.class.syntax
|
124
|
+
end
|
125
|
+
|
126
|
+
def get_subnode(key)
|
127
|
+
raise Clian::ConfigurationError, "Invalid key: #{key}" unless syntax.keyword?(key)
|
128
|
+
return instance_variable_get(syntax.instance_variable_name(key))
|
129
|
+
end
|
130
|
+
|
131
|
+
def create_subnode(keyword, value)
|
132
|
+
item_class = syntax.item_class(keyword)
|
133
|
+
if item_class.is_a?(Array)
|
134
|
+
return List.new(item_class.first, value)
|
135
|
+
elsif item_class == String
|
136
|
+
return value.to_s
|
137
|
+
else
|
138
|
+
return item_class.new(value)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
end # class Base
|
143
|
+
|
144
|
+
################################################################
|
145
|
+
# Parse Array object in YAML
|
146
|
+
class List < Element
|
147
|
+
include Enumerable
|
148
|
+
|
149
|
+
def initialize(item_class, array = [])
|
150
|
+
@original_hash = array
|
151
|
+
@configs = []
|
152
|
+
(array || []).each do |value|
|
153
|
+
item = item_class.new(value)
|
154
|
+
@configs << item
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
def [](key)
|
159
|
+
@configs.find {|c| c.name == key}
|
160
|
+
end
|
161
|
+
|
162
|
+
alias_method :get_subnode, :[]
|
163
|
+
|
164
|
+
def <<(conf)
|
165
|
+
@configs << conf
|
166
|
+
end
|
167
|
+
|
168
|
+
def to_hash # XXX: actually, it returns a Array
|
169
|
+
return @configs.map {|c| c.respond_to?(:to_hash) ? c.to_hash : c.to_s}
|
170
|
+
end
|
171
|
+
|
172
|
+
def each
|
173
|
+
@configs.each do |conf|
|
174
|
+
yield conf
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end # class List
|
178
|
+
|
179
|
+
################################################################
|
180
|
+
# Syntax table manipulation
|
181
|
+
class Toplevel < Element
|
182
|
+
DEFAULT_DIR = ENV["XDG_CONFIG_HOME"] || "~/.config"
|
183
|
+
DEFAULT_FILE = "config.yml"
|
184
|
+
|
185
|
+
def self.default_home(package_name)
|
186
|
+
File.join(DEFAULT_DIR, package_name)
|
187
|
+
end
|
188
|
+
|
189
|
+
def self.default_path(package_name)
|
190
|
+
File.join(default_home(package_name), DEFAULT_FILE)
|
191
|
+
end
|
192
|
+
|
193
|
+
def self.create_from_file(file_name = self.default_path)
|
194
|
+
unless File.exists?(File.expand_path(file_name))
|
195
|
+
raise ConfigurationError, "config file '#{file_name}' not found"
|
196
|
+
end
|
197
|
+
begin
|
198
|
+
return create_from_yaml_file(file_name)
|
199
|
+
rescue Psych::SyntaxError, Clian::ConfigurationError => e
|
200
|
+
raise ConfigurationError, e.message
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end # class Toplevel
|
204
|
+
end # class Config
|
205
|
+
end # module Clian
|
data/lib/clian.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "clian/version"
|
2
|
+
|
3
|
+
module Clian
|
4
|
+
class ConfigurationError < StandardError ; end
|
5
|
+
|
6
|
+
dir = File.dirname(__FILE__) + "/clian"
|
7
|
+
|
8
|
+
autoload :Authorizer, "#{dir}/authorizer.rb"
|
9
|
+
autoload :Cli, "#{dir}/cli.rb"
|
10
|
+
autoload :Config, "#{dir}/config.rb"
|
11
|
+
autoload :VERSION, "#{dir}/version.rb"
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,170 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: clian
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yoshinari Nomura
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: thor
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.19.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.19.1
|
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.pre4
|
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.pre4
|
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
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: mail
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: bundler
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '1.11'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1.11'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rake
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '10.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '10.0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rspec
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '3.0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '3.0'
|
125
|
+
description: Small set of Ruby classes helpful for creation of CLI tools.
|
126
|
+
email:
|
127
|
+
- nom@quickhack.net
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- ".rspec"
|
134
|
+
- ".travis.yml"
|
135
|
+
- Gemfile
|
136
|
+
- LICENSE.txt
|
137
|
+
- Rakefile
|
138
|
+
- bin/console
|
139
|
+
- bin/setup
|
140
|
+
- clian.gemspec
|
141
|
+
- lib/clian.rb
|
142
|
+
- lib/clian/authorizer.rb
|
143
|
+
- lib/clian/cli.rb
|
144
|
+
- lib/clian/config.rb
|
145
|
+
- lib/clian/version.rb
|
146
|
+
homepage: https://github.com/yoshinari-nomura/clian
|
147
|
+
licenses:
|
148
|
+
- MIT
|
149
|
+
metadata: {}
|
150
|
+
post_install_message:
|
151
|
+
rdoc_options: []
|
152
|
+
require_paths:
|
153
|
+
- lib
|
154
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
155
|
+
requirements:
|
156
|
+
- - ">="
|
157
|
+
- !ruby/object:Gem::Version
|
158
|
+
version: '0'
|
159
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
160
|
+
requirements:
|
161
|
+
- - ">="
|
162
|
+
- !ruby/object:Gem::Version
|
163
|
+
version: '0'
|
164
|
+
requirements: []
|
165
|
+
rubyforge_project:
|
166
|
+
rubygems_version: 2.5.1
|
167
|
+
signing_key:
|
168
|
+
specification_version: 4
|
169
|
+
summary: Small set of Ruby classes helpful for creation of CLI tools.
|
170
|
+
test_files: []
|