1 require "osdn/cli/version"
2 require "osdn/cli/runner"
3 require "osdn/cli/overrides"
16 @@client_id = "osdn-cli"
17 @@client_secret = "not-secret"
25 @@_show_progress = false
29 def _show_progress=(v)
41 module_function :client_id, :client_secret,
42 :_show_progress, :_show_progress=,
43 :_rate_limit, :_rate_limit=
46 autoload :Login, 'osdn/cli/command/login'
47 autoload :Package, 'osdn/cli/command/package'
48 autoload :Release, 'osdn/cli/command/release'
49 autoload :Relfile, 'osdn/cli/command/relfile'
50 autoload :FrsMkdirs, 'osdn/cli/command/frs_mkdirs'
51 autoload :FrsUpload, 'osdn/cli/command/frs_upload'
54 def initialize(logger)
56 @credential = Hashie::Mash.new
60 attr_accessor :credential, :format
63 Pathname.new(ENV['HOME']) + '.config/osdn/credential.yml'
68 stat = credential_path.stat()
69 unless credential_path.owned?
70 logger.error "Invalid ownership of credential file #{credential_path}, skip loading."
73 if RUBY_PLATFORM !~ /mswin|msys|mingw|cygwin|bccwin|wince|emc/ && (stat.mode & 0777).to_s(8) != "600"
74 logger.error "Invalid permission #{(stat.mode & 0777).to_s(8)} of credential file #{credential_path}, skip loading."
80 logger.debug "Loading credentials from #{credential_path}"
81 @credential = Hashie::Mash.new(YAML.load_file(credential_path))
86 FileUtils.mkdir_p credential_path.dirname, verbose: false
87 cio = credential_path.open('w', 0600)
88 YAML.dump(credential.to_hash, cio)
93 logger.debug "Checking token expires..."
95 unless credential.access_token
96 logger.fatal "You have no access token. Please login with '#{$0} login'."
99 if credential.expires_at > Time.now + 30
100 logger.debug "You have valid access token, skip to refresh."
104 logger.debug "Access token has been expired. Trying to refresh token..."
105 api = OSDNClient::DefaultApi.new
107 set_credential api.token(CLI.client_id, CLI.client_secret, grant_type: 'refresh_token', refresh_token: credential.refresh_token)
108 rescue OSDNClient::ApiError => e
110 err = JSON.parse(e.response_body)
111 logger.fatal err["error_description"]
113 logger.fatal "Failed to refresh access token."
115 logger.fatal "Please login again."
118 logger.debug "Access token is refreshed successfully."
121 def set_credential(token, update_expires = true)
122 token = Hashie::Mash.new(token.to_hash)
124 token.expires_at = Time.now + token.expires_in.to_i
126 token.scope = [*token.scope].join(' ').split(' ')
128 credential.update token
134 if credential.access_token && !credential.access_token.empty?
135 OSDNClient.configure do |config|
136 config.access_token = credential.access_token
141 def load_variables(path = '.', recursive_merge = true)
143 path = Pathname.new(Dir.getwd) + path
144 cur_dir = Pathname.new('/')
146 path.each_filename do |d|
147 cur_dir = cur_dir + d
148 vf = cur_dir + '.osdn.vars'
151 logger.debug "Load and merge variables from #{vf}"
152 vars.update YAML.load_file(vf.to_s)
154 logger.warn "Failed to load variables from #{vf}; #{e.message}"
159 path = path+'.osdn.vars'
161 logger.debug "Load and merge variables from #{path}"
162 vars.update YAML.load_file(path)
165 logger.warn "Failed to load variables from #{path}; #{e.message}"
168 logger.debug "Variables: #{vars.inspect}"
169 Hashie::Mash.new(vars)
172 def write_variables(vars, dir = nil)
173 path = Pathname.new(dir || '.') + '.osdn.vars'
174 logger.info "Save variables to #{path}"
176 YAML.dump(vars.to_hash, vio)
180 def update_variables(dir, vars)
181 write_variables(load_variables(dir, false).deep_merge(vars), dir)
185 def hexdigest(klass, file)
188 while buf = fio.read(1024*1024) and buf.length > 0
200 api = OSDNClient::DefaultApi.new
215 subcommand = ARGV.shift ||'show'
222 puts load_variables[name]
229 name, value = ARGV.shift, ARGV.shift
230 if !name || name.empty?
231 logger.fatal "Missing variable name"
235 if !value || value.empty?
236 logger.fatal "Missing variable value"
240 vars = load_variables('.', false)
246 puts "#{$0} vars show [name] -- Show current variable"
247 puts "#{$0} vars set <name> <value> -- Save variable to .osdn.vars"
251 "Get/set request environment variable."