Skip to content

Commit

Permalink
Merge pull request #7 from alces-software/develop
Browse files Browse the repository at this point in the history
Use `flight_config` to manage the config file
  • Loading branch information
WilliamMcCumstie authored Apr 8, 2019
2 parents e56949e + cedccf1 commit 6359cdb
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 64 deletions.
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@ source 'https://rubygems.org' do
gem 'tty-pager'
gem 'zxcvbn-ruby'
end

git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

gem 'flight_config', github: 'alces-software/flight_config'

11 changes: 10 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
GIT
remote: https://github.com/alces-software/flight_config
revision: b4408f04de95e83de3e6c23ca503554cef2ce67e
specs:
flight_config (0.1.0)
tty-config

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -47,6 +54,7 @@ GEM
timers (4.1.2)
hitimes
tty-color (0.4.2)
tty-config (0.3.1)
tty-cursor (0.5.0)
tty-pager (0.11.0)
strings (~> 0.1.0)
Expand Down Expand Up @@ -85,6 +93,7 @@ DEPENDENCIES
activesupport!
colorize!
commander!
flight_config!
html2text!
http!
paint!
Expand All @@ -96,4 +105,4 @@ DEPENDENCIES
zxcvbn-ruby!

BUNDLED WITH
1.15.3
1.17.3
13 changes: 9 additions & 4 deletions lib/alces/account/commands/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,12 @@ def login(args, options)
token = login['authentication_token']
email = login['email']

Config.set(:auth_token, token)
Config.set(:auth_user, username)
Config.set(:auth_email, email)
Config.update do |config|
config.sso_url # Ensures the sso_url is set in the data during the save
config.set(:auth_token, token)
config.set(:auth_user, username)
config.set(:auth_email, email)
end
prompt.say Paint["\nYou are now logged in to the Alces Flight plaform.", '#2794d8']
rescue AccountError
prompt.error "Log in failed: #{$!.message}"
Expand All @@ -207,7 +210,9 @@ def login(args, options)
end

def logout(args, options)
Config.set(:auth_token, nil)
Config.update do |config|
config.set(:auth_token, nil)
end
prompt.say Paint["You are now logged out of the Alces Flight platform.", '#2794d8']
end

Expand Down
100 changes: 41 additions & 59 deletions lib/alces/account/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,87 +25,69 @@
# https://github.com/alces-software/flight-account
#===============================================================================

require 'xdg'
require 'yaml'
require 'fileutils'
require 'etc'
require 'flight_config'

module Alces
module Account
module Config
class << self
include XDG::BaseDir::Mixin
class Config
include FlightConfig::Updater

def root
File.join(File.dirname(__FILE__),'..','..','..')
class << self
def cache
@cache ||= read
end

def method_missing(s,*a,&b)
if data.key?(s)
data[s]
def method_missing(s, *a, &b)
if respond_to_missing?(s) == :account_config_method
cache.send(s, *a, &b)
else
super
end
end

def respond_to_missing?(s)
data.key?(s)
end

def sso_url
ENV['cw_SSO_URL'] ||
data[:sso_url] ||
# 'http://accounts.alces-flight.lvh.me:4000'
# 'https://staging.accounts.alces-flight.com'
'https://accounts.alces-flight.com'
def respond_to_missing?(s, **k)
if cache.respond_to?(s, **k)
:account_config_method
else
super
end
end
end

def auth_token
data[:auth_token]
end
allow_missing_read

def username
data[:auth_user] || Etc.getlogin
end
def root
File.join(File.dirname(__FILE__),'..','..','..')
end

def set(key, value)
if value
data[key.to_sym] = value
else
data.delete(key.to_sym)
end
save
end
def sso_url
__data__.set_if_empty(:sso_url, value: 'https://accounts.alces-flight.com')
__data__.fetch(:sso_url)
end

private
def data
@data ||= load
end
def auth_user
__data__.fetch(:auth_user)
end

def subdirectory
File.join('flight','accounts')
end
def auth_token
__data__.fetch(:auth_token)
end

def load
files = config.home.glob('config.yml')
if files.first
YAML.load_file(files.first)
else
{}
end
end
def username
__data__.fetch(:auth_user, default: Etc.getlogin)
end

def save
unless Dir.exists?(config.home.to_s)
FileUtils.mkdir_p(config.home.to_s, mode: 0700)
end
File.write(config_file, data.to_yaml)
File.chmod(0600, config_file) # File may contain auth token so should not be world-readable!
def set(method, value)
if value
__data__.set(method, value: value)
else
__data__.delete(method)
end
end

def config_file
File.join(config.home.to_s,'config.yml')
end
def path
File.expand_path('~/.config/flight/accounts/config.yml')
end
end
end
Expand Down

0 comments on commit 6359cdb

Please sign in to comment.