44require 'yaml'
55require 'json'
66require 'logger'
7- require 'ostruct'
87require_relative '../lib/docker'
98
109# NOTE: dotenv/load must come after ../lib/docker
1110require 'dotenv/load'
12- # I think I need to only call this if we're NOT running in a container:
13- # require 'dotenv/load' unless Docker.running_in_container?
1411
1512# Config class to hold/manage our configuration
1613class Config
17- # Load the ENV vars
18- Docker ::Secret . setup_environment!
14+ # Define your Structs for Secrets:
15+ Ucpath = Struct . new ( :root , :id , :key , keyword_init : true )
16+ Sis = Struct . new ( :root , :id , :key , keyword_init : true )
17+ Ldap = Struct . new ( :host , :pass , keyword_init : true )
18+ Secrets = Struct . new ( :ucpath , :sis , :ldap , keyword_init : true )
1919
20- # Secrets (passwords, api keys, etc...): Uses ERB for ENV variables
21- @secrets = JSON . parse ( YAML . safe_load ( ERB . new ( File . read ( 'config/secrets.yml' ) ) . result ) . to_json ,
22- object_class : OpenStruct )
20+ class << self
21+ attr_reader :secrets , :settings , :help
22+
23+ def load!
24+ # Load the ENV vars
25+ Docker ::Secret . setup_environment!
26+
27+ load_settings! ( 'config/settings.yml' )
28+ load_secrets! ( 'config/secrets.yml' )
29+ end
30+
31+ # Returns specified field value from settings.yml
32+ def setting ( field )
33+ @settings [ field . to_sym ]
34+ end
35+
36+ def ucpath_employee_fields
37+ @ucpath_fields [ 'employee' ] [ 'fields' ]
38+ end
39+
40+ def sis_fields
41+ @sis_fields [ 'SIS' ] [ 'fields' ]
42+ end
43+
44+ def ucpath_job_fields
45+ @ucpath_fields [ 'job' ] [ 'fields' ]
46+ end
47+
48+ def student_affiated? ( affiliation )
49+ @ldap_fields [ 'student_affiliation' ] . include? affiliation
50+ end
51+
52+ def ldap_attributes
53+ @ldap_fields [ 'attributes' ]
54+ end
55+
56+ def check_ucpath_code ( type , value )
57+ @ucpath_codes [ type ] . include? value
58+ end
59+
60+ private
61+
62+ def load_settings! ( path )
63+ raw = yaml_with_erb ( path )
64+
65+ # Load those config settings from the yaml hash:
66+ @settings = create_struct_from_hash (
67+ name : 'Settings' ,
68+ hash : raw . fetch ( 'settings' )
69+ )
70+
71+ # keep help separate...it's just a string
72+ @help = raw [ 'help' ]
73+ end
74+
75+ def load_secrets! ( path )
76+ raw = yaml_with_erb ( path )
77+
78+ @secrets = Secrets . new (
79+ ucpath : Ucpath . new ( **symbolize ( raw . fetch ( 'ucpath' ) ) ) ,
80+ sis : Sis . new ( **symbolize ( raw . fetch ( 'sis' ) ) ) ,
81+ ldap : Ldap . new ( **symbolize ( raw . fetch ( 'ldap' ) ) )
82+ )
83+
84+ # Over-ride the LDAP host if we're in CI land... JUST to make
85+ # sure we don't hit the actual host when we're running rspec!
86+ @secrets . ldap . host = 'ldap.fake.edu' if ENV [ 'CI' ]
87+ end
88+
89+ def symbolize ( hash )
90+ hash . transform_keys ( &:to_sym )
91+ end
92+
93+ def yaml_with_erb ( path )
94+ YAML . safe_load ( ERB . new ( File . read ( path ) ) . result )
95+ end
96+
97+ # Since settings isn't nested hash of hashes easy enough to create this struct dynamically:
98+ def create_struct_from_hash ( name :, hash :)
99+ # Since Structs need symbols....
100+ sym_hash = hash . transform_keys ( &:to_sym )
101+
102+ struct_class = if const_defined? ( name , false )
103+ const_get ( name )
104+ else
105+ const_set ( name , Struct . new ( *sym_hash . keys , keyword_init : true ) )
106+ end
107+
108+ struct_class . new ( **sym_hash )
109+ end
110+ end
23111
24- # General Settings: Uses ERB for ENV variables
25- @settings = JSON . parse ( YAML . safe_load ( ERB . new ( File . read ( 'config/settings.yml' ) ) . result ) . to_json ,
26- object_class : OpenStruct )
112+ # Let's do this!!!!
113+ load!
27114
28115 ucpath_contents = File . read ( 'config/ucpath_fields.yml' )
29116 @ucpath_fields = YAML . safe_load ( ERB . new ( ucpath_contents ) . result )
@@ -36,42 +123,4 @@ class Config
36123
37124 sis_contents = File . read ( 'config/sis_fields.yml' )
38125 @sis_fields = YAML . safe_load ( ERB . new ( sis_contents ) . result )
39-
40- # Over-ride the LDAP host if we're in CI land... JUST to make
41- # sure we don't hit the actual host when we're running rspec!
42- @secrets . ldap . host = 'ldap.fake.edu' if ENV [ 'CI' ]
43-
44- # Returns ostruct of the secrets yaml file
45- class << self
46- attr_reader :secrets
47- end
48-
49- def self . ucpath_employee_fields
50- @ucpath_fields [ 'employee' ] [ 'fields' ]
51- end
52-
53- def self . sis_fields
54- @sis_fields [ 'SIS' ] [ 'fields' ]
55- end
56-
57- def self . ucpath_job_fields
58- @ucpath_fields [ 'job' ] [ 'fields' ]
59- end
60-
61- def self . student_affiated? ( affiliation )
62- @ldap_fields [ 'student_affiliation' ] . include? affiliation
63- end
64-
65- def self . check_ucpath_code ( type , value )
66- @ucpath_codes [ type ] . include? value
67- end
68-
69- # Returns specified field value from settings.yml
70- def self . setting ( field )
71- @settings . settings [ field ] || nil
72- end
73-
74- def self . help
75- @settings . help
76- end
77126end
0 commit comments