Class: OSDN::CLI::Command::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/osdn/cli.rb

Direct Known Subclasses

FrsBase, FrsMkdirs, FrsUpload, Login, Ping, Vars

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ Base

Returns a new instance of Base.



45
46
47
48
49
# File 'lib/osdn/cli.rb', line 45

def initialize(logger)
  @logger = logger
  @credential = Hashie::Mash.new
  @format = 'pretty'
end

Instance Attribute Details

#credentialObject

Returns the value of attribute credential.



51
52
53
# File 'lib/osdn/cli.rb', line 51

def credential
  @credential
end

#formatObject

Returns the value of attribute format.



51
52
53
# File 'lib/osdn/cli.rb', line 51

def format
  @format
end

#loggerObject (readonly)

Returns the value of attribute logger.



50
51
52
# File 'lib/osdn/cli.rb', line 50

def logger
  @logger
end

Instance Method Details

#credential_pathObject



53
54
55
# File 'lib/osdn/cli.rb', line 53

def credential_path
  Pathname.new(ENV['HOME']) + '.config/osdn/credential.yml'
end

#load_credentialObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/osdn/cli.rb', line 57

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
    unless (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



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/osdn/cli.rb', line 128

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
  logger.debug "Variables: #{vars.inspect}"
  Hashie::Mash.new(vars)
end

#set_client_tokenObject



120
121
122
123
124
125
126
# File 'lib/osdn/cli.rb', line 120

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



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/osdn/cli.rb', line 108

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_tokenObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/osdn/cli.rb', line 83

def update_token
  logger.debug "Checking token expires..."
  load_credential
  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. Refresh access 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 refreshed successfully."
end

#update_variables(dir, vars) ⇒ Object



167
168
169
# File 'lib/osdn/cli.rb', line 167

def update_variables(dir, vars)
  write_variables(load_variables(dir, false).merge(vars), dir)
end

#write_credentialObject



76
77
78
79
80
81
# File 'lib/osdn/cli.rb', line 76

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



159
160
161
162
163
164
165
# File 'lib/osdn/cli.rb', line 159

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