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

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

Direct Known Subclasses

FrsMkdirs, FrsUpload, Login, Package, Ping, Vars

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ Base

Returns a new instance of Base.



33
34
35
36
37
# File 'lib/osdn/cli.rb', line 33

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

Instance Attribute Details

#credentialObject

Returns the value of attribute credential.



39
40
41
# File 'lib/osdn/cli.rb', line 39

def credential
  @credential
end

#formatObject

Returns the value of attribute format.



39
40
41
# File 'lib/osdn/cli.rb', line 39

def format
  @format
end

#loggerObject (readonly)

Returns the value of attribute logger.



38
39
40
# File 'lib/osdn/cli.rb', line 38

def logger
  @logger
end

Instance Method Details

#credential_pathObject



41
42
43
# File 'lib/osdn/cli.rb', line 41

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

#load_credentialObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/osdn/cli.rb', line 45

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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/osdn/cli.rb', line 116

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



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

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



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/osdn/cli.rb', line 96

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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/osdn/cli.rb', line 71

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



155
156
157
# File 'lib/osdn/cli.rb', line 155

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

#write_credentialObject



64
65
66
67
68
69
# File 'lib/osdn/cli.rb', line 64

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



147
148
149
150
151
152
153
# File 'lib/osdn/cli.rb', line 147

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