Class: OSDN::CLI::Command::Base
- Inherits:
-
Object
- Object
- OSDN::CLI::Command::Base
show all
- Defined in:
- lib/osdn/cli.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(logger) ⇒ Base
Returns a new instance of Base.
54
55
56
57
58
|
# File 'lib/osdn/cli.rb', line 54
def initialize(logger)
@logger = logger
@credential = Hashie::Mash.new
@format = 'pretty'
end
|
Instance Attribute Details
#credential ⇒ Object
Returns the value of attribute credential.
60
61
62
|
# File 'lib/osdn/cli.rb', line 60
def credential
@credential
end
|
Returns the value of attribute format.
60
61
62
|
# File 'lib/osdn/cli.rb', line 60
def format
@format
end
|
#logger ⇒ Object
Returns the value of attribute logger.
59
60
61
|
# File 'lib/osdn/cli.rb', line 59
def logger
@logger
end
|
Instance Method Details
#credential_path ⇒ Object
62
63
64
|
# File 'lib/osdn/cli.rb', line 62
def credential_path
Pathname.new(ENV['HOME']) + '.config/osdn/credential.yml'
end
|
#load_credential ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/osdn/cli.rb', line 66
def load_credential
begin
stat = credential_path.stat()
unless credential_path.owned?
logger.error "Invalid ownership of credential file #{credential_path}, skip loading."
return
end
if RUBY_PLATFORM !~ /mswin|msys|mingw|cygwin|bccwin|wince|emc/ && (stat.mode & 0777).to_s(8) != "600"
logger.error "Invalid permission #{(stat.mode & 0777).to_s(8)} of credential file #{credential_path}, skip loading."
return
end
rescue Errno::ENOENT
return
end
logger.debug "Loading credentials from #{credential_path}"
@credential = Hashie::Mash.new(YAML.load_file(credential_path))
set_client_token
end
|
#load_variables(path = '.', recursive_merge = true) ⇒ Object
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
# File 'lib/osdn/cli.rb', line 141
def load_variables(path = '.', recursive_merge = true)
vars = {}
path = Pathname.new(Dir.getwd) + path
cur_dir = Pathname.new('/')
if recursive_merge
path.each_filename do |d|
cur_dir = cur_dir + d
vf = cur_dir + '.osdn.vars'
vf.exist? or next
begin
logger.debug "Load and merge variables from #{vf}"
vars.update YAML.load_file(vf.to_s)
rescue => e
logger.warn "Failed to load variables from #{vf}; #{e.message}"
end
end
else
begin
path = path+'.osdn.vars'
if path.exist?
logger.debug "Load and merge variables from #{path}"
vars.update YAML.load_file(path)
end
rescue => e
logger.warn "Failed to load variables from #{path}; #{e.message}"
end
end
if vars['local_file_info']
vars['local_file_info'].each do |fname, finfo|
finfo.has_key?('size') or next
finfo['filesize'] = finfo.delete('size')
end
end
logger.debug "Variables: #{vars.inspect}"
Hashie::Mash.new(vars)
end
|
#set_client_token ⇒ Object
133
134
135
136
137
138
139
|
# File 'lib/osdn/cli.rb', line 133
def set_client_token
if credential.access_token && !credential.access_token.empty?
OSDNClient.configure do |config|
config.access_token = credential.access_token
end
end
end
|
#set_credential(token, update_expires = true) ⇒ Object
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/osdn/cli.rb', line 121
def set_credential(token, update_expires = true)
token = Hashie::Mash.new(token.to_hash)
if update_expires
token.expires_at = Time.now + token.expires_in.to_i
end
token.scope = [*token.scope].join(' ').split(' ')
credential.update token
write_credential
set_client_token
end
|
#update_token ⇒ Object
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/osdn/cli.rb', line 92
def update_token
logger.debug "Checking token expires..."
load_credential
unless credential.access_token
logger.fatal "You have no access token. Please login with '#{$0} login'."
return
end
if credential.expires_at > Time.now + 30
logger.debug "You have valid access token, skip to refresh."
return
end
logger.debug "Access token has been expired. Trying to refresh token..."
api = OSDNClient::DefaultApi.new
begin
set_credential api.token(CLI.client_id, CLI.client_secret, grant_type: 'refresh_token', refresh_token: credential.refresh_token)
rescue OSDNClient::ApiError => e
begin
err = JSON.parse(e.response_body)
logger.fatal err["error_description"]
rescue
logger.fatal "Failed to refresh access token."
end
logger.fatal "Please login again."
return
end
logger.debug "Access token is refreshed successfully."
end
|
#update_variables(dir, vars) ⇒ Object
187
188
189
|
# File 'lib/osdn/cli.rb', line 187
def update_variables(dir, vars)
write_variables(load_variables(dir, false).deep_merge(vars), dir)
end
|
#write_credential ⇒ Object
85
86
87
88
89
90
|
# File 'lib/osdn/cli.rb', line 85
def write_credential
FileUtils.mkdir_p credential_path.dirname, verbose: false
cio = credential_path.open('w', 0600)
YAML.dump(credential.to_hash, cio)
cio.close
end
|
#write_variables(vars, dir = nil) ⇒ Object
179
180
181
182
183
184
185
|
# File 'lib/osdn/cli.rb', line 179
def write_variables(vars, dir = nil)
path = Pathname.new(dir || '.') + '.osdn.vars'
logger.info "Save variables to #{path}"
vio = path.open('w')
YAML.dump(vars.to_hash, vio)
vio.close
end
|