diff --git a/Gemfile b/Gemfile index 491a8f5..18c7df9 100644 --- a/Gemfile +++ b/Gemfile @@ -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' + diff --git a/Gemfile.lock b/Gemfile.lock index 4021889..65fe6b5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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: @@ -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) @@ -85,6 +93,7 @@ DEPENDENCIES activesupport! colorize! commander! + flight_config! html2text! http! paint! @@ -96,4 +105,4 @@ DEPENDENCIES zxcvbn-ruby! BUNDLED WITH - 1.15.3 + 1.17.3 diff --git a/lib/alces/account/commands/account.rb b/lib/alces/account/commands/account.rb index 5c80a0d..0950692 100644 --- a/lib/alces/account/commands/account.rb +++ b/lib/alces/account/commands/account.rb @@ -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}" @@ -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 diff --git a/lib/alces/account/config.rb b/lib/alces/account/config.rb index 50e4ea0..9077bcd 100644 --- a/lib/alces/account/config.rb +++ b/lib/alces/account/config.rb @@ -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