@@ -54,9 +54,43 @@ def _get_custom_session(self):
54
54
return self ._custom_session
55
55
56
56
57
- def load_config ():
58
- with open (CONFIG_FILE ) as fp :
59
- return yaml .load (fp , Loader = yaml .Loader )
57
+ class ConfigLoader (object ):
58
+ @staticmethod
59
+ def load ():
60
+ config = {
61
+ 'aws' : {},
62
+ 'zulip' : {},
63
+ }
64
+ if os .path .exists (CONFIG_FILE ):
65
+ config = ConfigLoader ._load_file ()
66
+ config = ConfigLoader ._apply_env (config )
67
+ return config
68
+
69
+ @staticmethod
70
+ def _load_file ():
71
+ with open (CONFIG_FILE ) as fp :
72
+ return yaml .load (fp , Loader = yaml .Loader )
73
+
74
+ @staticmethod
75
+ def _apply_env (config ):
76
+ if 'AWS_ACCOUNT_ID' in os .environ :
77
+ config ['aws' ]['account_id' ] = os .environ ['AWS_ACCOUNT_ID' ]
78
+ if 'ZULIP_SITE' in os .environ :
79
+ config ['zulip' ]['site' ] = os .environ ['ZULIP_SITE' ]
80
+ if 'ZULIP_EMAIL' in os .environ :
81
+ config ['zulip' ]['email' ] = os .environ ['ZULIP_EMAIL' ]
82
+ if 'ZULIP_API_KEY' in os .environ :
83
+ config ['zulip' ]['api_key' ] = os .environ ['ZULIP_API_KEY' ]
84
+ if 'ZULIP_TYPE' in os .environ :
85
+ config ['zulip' ]['type' ] = os .environ ['ZULIP_TYPE' ]
86
+ if 'ZULIP_TO' in os .environ :
87
+ config ['zulip' ]['to' ] = os .environ ['ZULIP_TO' ]
88
+ if 'ZULIP_TOPIC' in os .environ :
89
+ config ['zulip' ]['topic' ] = os .environ ['ZULIP_TOPIC' ]
90
+ if 'ZULIP_MESSAGE' in os .environ :
91
+ config ['zulip' ]['message' ] = os .environ ['ZULIP_MESSAGE' ]
92
+ return config
93
+
60
94
61
95
def send_message (config , message ):
62
96
client = zulip .Client (site = config ['zulip' ]['site' ],
@@ -77,7 +111,7 @@ def format_message(config, cost, forecast, nserver):
77
111
cost = cost , forecast = forecast , nserver = nserver )
78
112
79
113
def main (aws_profile_name = 'default' , use_aws_default_session = False , dryrun = False ):
80
- config = load_config ()
114
+ config = ConfigLoader . load ()
81
115
82
116
aws_usage = AWSUsage (config ['aws' ]['account_id' ], use_aws_default_session )
83
117
if not use_aws_default_session :
0 commit comments