Skip to content

Commit

Permalink
Add XDG_CONFIG_HOME support
Browse files Browse the repository at this point in the history
  • Loading branch information
pgilad committed Nov 9, 2019
1 parent 31c1cf7 commit d0cbe52
Show file tree
Hide file tree
Showing 5 changed files with 47 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
13 changes: 10 additions & 3 deletions lib/github/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

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 +20,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
6 changes: 4 additions & 2 deletions lib/github/authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ 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 +38,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
23 changes: 23 additions & 0 deletions lib/github/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
if @legacy_file
# 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
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 d0cbe52

Please sign in to comment.