Skip to content

Commit

Permalink
Merge pull request #16 from pgilad/add-xdg-directories-support
Browse files Browse the repository at this point in the history
Add XDG_CONFIG_HOME support
  • Loading branch information
edgarjs authored Nov 13, 2019
2 parents 31c1cf7 + 8e92c7a commit 79d9bb2
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
23 changes: 23 additions & 0 deletions lib/config_path.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Github
class ConfigPath
APP_NAME = 'github-repos'

# @param [String] target
# @param [String] legacy_path
def initialize(target, legacy_path = nil)
@target = target
@legacy_file = legacy_path
end

def get
unless @legacy_file.nil?
# Fallback to legacy behavior if legacy path exists
legacy_file = File.expand_path(@legacy_file)
return legacy_file if File.file?(legacy_file)
end

xdg_config_home = ENV['XDG_CONFIG_HOME'] || File.join(ENV['HOME'], '.config')
File.join(xdg_config_home, APP_NAME, @target)
end
end
end
14 changes: 11 additions & 3 deletions lib/github/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
require 'cgi'
require 'github/authorization'
require 'local_storage'
require 'config_path'

module Github
class Api
HOST_FILE = '~/.github-repos/host'.freeze
LEGACY_HOST_FILE = '~/.github-repos/host'.freeze
DEFAULT_HOST = 'https://api.github.com'.freeze
HOST_FILE_NAME = 'host'.freeze

def search_repos(query)
path = "/search/repositories?q=#{escape(query)}"
Expand All @@ -18,15 +21,20 @@ def search_repos(query)
end

class << self
def host_storage
host_path = ConfigPath.new(HOST_FILE_NAME, LEGACY_HOST_FILE).get
LocalStorage.new(host_path)
end

def configure_host(host)
LocalStorage.new(HOST_FILE).put("#{host}/api/v3")
host_storage.put("#{host}/api/v3")
end
end

private

def host
@host ||= (LocalStorage.new(HOST_FILE).get || 'https://api.github.com')
@host ||= (Api.host_storage.get || DEFAULT_HOST)
end

def escape(str)
Expand Down
7 changes: 5 additions & 2 deletions lib/github/authorization.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# frozen_string_literal: true

require 'local_storage'
require 'config_path'

module Github
class Authorization
class NotAuthorizedError < StandardError; end

AUTHORIZATION_URL = 'https://github.com/settings/tokens/new?description=Github%20Repos&scopes=repo'.freeze
CREDENTIALS = '~/.github-repos/config'.freeze
LEGACY_CREDENTIALS = '~/.github-repos/config'.freeze
CREDENTIALS_FILE_NAME = 'config'.freeze

attr_reader :username, :token

Expand Down Expand Up @@ -37,7 +39,8 @@ def stored?
private

def credentials
LocalStorage.new(CREDENTIALS)
config_path = ConfigPath.new(CREDENTIALS_FILE_NAME, LEGACY_CREDENTIALS).get
LocalStorage.new(config_path)
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/local_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class LocalStorage
attr_reader :location

# @param [String] filepath
def initialize(filepath)
@location = File.expand_path(filepath)
create_parent_dir
Expand Down

0 comments on commit 79d9bb2

Please sign in to comment.