11using System ;
22using System . IO ;
3+ using System . Linq ;
34using System . Text ;
5+ using NLog ;
46
57namespace Torch . Utils
68{
79 public static class DotEnv
810 {
11+ static readonly ILogger Log = LogManager . GetCurrentClassLogger ( ) ;
12+
913 public static void Load ( )
1014 {
1115 const string filePath = ".env" ;
@@ -14,21 +18,28 @@ public static void Load()
1418 var txt = File . ReadAllText ( filePath , Encoding . Default ) ;
1519 var newline = new [ ] { "\r \n " , Environment . NewLine } ;
1620 var equal = new [ ] { "=" } ;
17- foreach ( var cmp in txt . Split ( newline , StringSplitOptions . RemoveEmptyEntries ) )
21+ foreach ( var line in txt . Split ( newline , StringSplitOptions . RemoveEmptyEntries ) )
1822 {
19- var strArray = cmp . Split ( equal , StringSplitOptions . None ) ;
20- switch ( strArray . Length )
23+ var pair = line . Split ( equal , StringSplitOptions . None ) ;
24+ if ( pair . Length == 0 ) continue ;
25+
26+ var key = pair [ 0 ] . Trim ( ) ;
27+ if ( pair . Length == 1 )
2128 {
22- case 0 :
23- continue ;
24- case 1 :
25- Environment . SetEnvironmentVariable ( strArray [ 0 ] . Trim ( ) , null ) ;
26- continue ;
27- default :
28- Environment . SetEnvironmentVariable ( strArray [ 0 ] . Trim ( ) , strArray [ 1 ] . Trim ( ) ) ;
29- continue ;
29+ Environment . SetEnvironmentVariable ( key , null ) ;
30+ Log . Info ( $ "{ key } (no value)") ;
31+ continue ;
3032 }
33+
34+ var value = pair [ 1 ] . Trim ( ) ;
35+ Log . Info ( $ "{ key } = { Mask ( value ) } (masked)") ;
36+ Environment . SetEnvironmentVariable ( key , value ) ;
3137 }
3238 }
39+
40+ static string Mask ( string value )
41+ {
42+ return string . Join ( "" , Enumerable . Repeat ( '*' , value . Length ) ) ;
43+ }
3344 }
3445}
0 commit comments