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 :Relfile, 'osdn/cli/command/relfile'
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 unless credential.access_token
87 logger.fatal "You have no access token. Please login with '#{$0} login'."
90 if credential.expires_at > Time.now + 30
91 logger.debug "You have valid access token, skip to refresh."
95 logger.debug "Access token has been expired. Refresh access token..."
96 api = OSDNClient::DefaultApi.new
98 set_credential api.token(CLI.client_id, CLI.client_secret, grant_type: 'refresh_token', refresh_token: credential.refresh_token)
99 rescue OSDNClient::ApiError => e
101 err = JSON.parse(e.response_body)
102 logger.fatal err["error_description"]
104 logger.fatal "Failed to refresh access token."
106 logger.fatal "Please login again."
109 logger.debug "Access token refreshed successfully."
112 def set_credential(token, update_expires = true)
113 token = Hashie::Mash.new(token.to_hash)
115 token.expires_at = Time.now + token.expires_in.to_i
117 token.scope = [*token.scope].join(' ').split(' ')
119 credential.update token
125 if credential.access_token && !credential.access_token.empty?
126 OSDNClient.configure do |config|
127 config.access_token = credential.access_token
132 def load_variables(path = '.', recursive_merge = true)
134 path = Pathname.new(Dir.getwd) + path
135 cur_dir = Pathname.new('/')
137 path.each_filename do |d|
138 cur_dir = cur_dir + d
139 vf = cur_dir + '.osdn.vars'
142 logger.debug "Load and merge variables from #{vf}"
143 vars.update YAML.load_file(vf.to_s)
145 logger.warn "Failed to load variables from #{vf}; #{e.message}"
150 path = path+'.osdn.vars'
152 logger.debug "Load and merge variables from #{path}"
153 vars.update YAML.load_file(path)
156 logger.warn "Failed to load variables from #{path}; #{e.message}"
159 logger.debug "Variables: #{vars.inspect}"
160 Hashie::Mash.new(vars)
163 def write_variables(vars, dir = nil)
164 path = Pathname.new(dir || '.') + '.osdn.vars'
165 logger.info "Save variables to #{path}"
167 YAML.dump(vars.to_hash, vio)
171 def update_variables(dir, vars)
172 write_variables(load_variables(dir, false).deep_merge(vars), dir)
176 def hexdigest(klass, file)
179 while buf = fio.read(1024*1024) and buf.length > 0
191 api = OSDNClient::DefaultApi.new
206 subcommand = ARGV.shift ||'show'
213 puts load_variables[name]
220 name, value = ARGV.shift, ARGV.shift
221 if !name || name.empty?
222 logger.fatal "Missing variable name"
226 if !value || value.empty?
227 logger.fatal "Missing variable value"
231 vars = load_variables('.', false)
237 puts "#{$0} vars show [name] -- Show current variable"
238 puts "#{$0} vars set <name> <value> -- Save variable to .osdn.vars"
242 "Get/set request environment variable."