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)
33 module_function :client_id, :client_secret,
34 :_show_progress, :_show_progress=
37 autoload :Login, 'osdn/cli/command/login'
38 autoload :Package, 'osdn/cli/command/package'
39 #autoload :Release, 'osdn/cli/command/release'
40 #autoload :FrsFile, 'osdn/cli/command/frs_file'
41 autoload :FrsMkdirs, 'osdn/cli/command/frs_mkdirs'
42 autoload :FrsUpload, 'osdn/cli/command/frs_upload'
45 def initialize(logger)
47 @credential = Hashie::Mash.new
51 attr_accessor :credential, :format
54 Pathname.new(ENV['HOME']) + '.config/osdn/credential.yml'
59 stat = credential_path.stat()
60 unless credential_path.owned?
61 logger.error "Invalid ownership of credential file #{credential_path}, skip loading."
64 unless (stat.mode & 0777).to_s(8) == "600"
65 logger.error "Invalid permission #{(stat.mode & 0777).to_s(8)} of credential file #{credential_path}, skip loading."
71 logger.debug "Loading credentials from #{credential_path}"
72 @credential = Hashie::Mash.new(YAML.load_file(credential_path))
77 FileUtils.mkdir_p credential_path.dirname, verbose: false
78 cio = credential_path.open('w', 0600)
79 YAML.dump(credential.to_hash, cio)
84 logger.debug "Checking token expires..."
86 if credential.expires_at > Time.now + 30
87 logger.debug "You have valid access token, skip to refresh."
91 logger.debug "Access token has been expired. Refresh access token..."
92 api = OSDNClient::DefaultApi.new
94 set_credential api.token(CLI.client_id, CLI.client_secret, grant_type: 'refresh_token', refresh_token: credential.refresh_token)
95 rescue OSDNClient::ApiError => e
97 err = JSON.parse(e.response_body)
98 logger.fatal err["error_description"]
100 logger.fatal "Failed to refresh access token."
102 logger.fatal "Please login again."
105 logger.debug "Access token refreshed successfully."
108 def set_credential(token, update_expires = true)
109 token = Hashie::Mash.new(token.to_hash)
111 token.expires_at = Time.now + token.expires_in.to_i
113 token.scope = [*token.scope].join(' ').split(' ')
115 credential.update token
121 if credential.access_token && !credential.access_token.empty?
122 OSDNClient.configure do |config|
123 config.access_token = credential.access_token
128 def load_variables(path = '.', recursive_merge = true)
130 path = Pathname.new(Dir.getwd) + path
131 cur_dir = Pathname.new('/')
133 path.each_filename do |d|
134 cur_dir = cur_dir + d
135 vf = cur_dir + '.osdn.vars'
138 logger.debug "Load and merge variables from #{vf}"
139 vars.update YAML.load_file(vf.to_s)
141 logger.warn "Failed to load variables from #{vf}; #{e.message}"
146 path = path+'.osdn.vars'
148 logger.debug "Load and merge variables from #{path}"
149 vars.update YAML.load_file(path)
152 logger.warn "Failed to load variables from #{path}; #{e.message}"
155 logger.debug "Variables: #{vars.inspect}"
156 Hashie::Mash.new(vars)
159 def write_variables(vars, dir = nil)
160 path = Pathname.new(dir || '.') + '.osdn.vars'
161 logger.info "Save variables to #{path}"
163 YAML.dump(vars.to_hash, vio)
167 def update_variables(dir, vars)
168 write_variables(load_variables(dir, false).merge(vars), dir)
172 def hexdigest(klass, file)
175 while buf = fio.read(1024*1024) and buf.length > 0
187 api = OSDNClient::DefaultApi.new
202 subcommand = ARGV.shift ||'show'
209 puts load_variables[name]
216 name, value = ARGV.shift, ARGV.shift
217 if !name || name.empty?
218 logger.fatal "Missing variable name"
222 if !value || value.empty?
223 logger.fatal "Missing variable value"
227 vars = load_variables('.', false)
233 puts "#{$0} vars show [name] -- Show current variable"
234 puts "#{$0} vars set <name> <value> -- Save variable to .osdn.vars"
238 "Get/set request environment variable."