public static void Load(LoadOptions options) => Load(Path.Combine(Directory.GetCurrentDirectory(), ".env"), options);
public static void Load(LoadOptions options) => Load(Path.Combine(Directory.GetCurrentDirectory(), DEFAULT_ENVFILENAME), options);
public static IEnumerable <KeyValuePair <string, string> > Load(string path = null, LoadOptions options = null) { if (options == null) { options = LoadOptions.DEFAULT; } var file = Path.GetFileName(path); if (file == null || file == string.Empty) { file = DEFAULT_ENVFILENAME; } var dir = Path.GetDirectoryName(path); if (dir == null || dir == string.Empty) { dir = Directory.GetCurrentDirectory(); } path = Path.Combine(dir, file); if (options.OnlyExactPath) { if (!File.Exists(path)) { path = null; } } else { while (!File.Exists(path)) { var parent = Directory.GetParent(dir); if (parent == null) { path = null; break; } dir = parent.FullName; path = Path.Combine(dir, file); } } // in production, there should be no .env file, so this should be the common code path if (path == null) { return(Enumerable.Empty <KeyValuePair <string, string> >()); } return(LoadContents(File.ReadAllText(path), options)); }
public static IEnumerable <KeyValuePair <string, string> > LoadContents(string contents, LoadOptions options = null) { if (options == null) { options = LoadOptions.DEFAULT; } if (options.SetEnvVars) { if (options.ClobberExistingVars) { return(Parsers.ParseDotenvFile(contents, Parsers.SetEnvVar)); } else { return(Parsers.ParseDotenvFile(contents, Parsers.NoClobberSetEnvVar)); } } else { return(Parsers.ParseDotenvFile(contents, Parsers.DoNotSetEnvVar)); } }
public static LoadOptions TraversePath() => LoadOptions.TraversePath();
public static IEnumerable <KeyValuePair <string, string> > LoadMulti(string[] paths, LoadOptions options = null) { return(paths.Aggregate( Enumerable.Empty <KeyValuePair <string, string> >(), (kvps, path) => kvps.Concat(Load(path, options)) )); }
public static LoadOptions NoClobber() => LoadOptions.NoClobber();
public static LoadOptions NoEnvVars() => LoadOptions.NoEnvVars();
public static LoadOptions TraversePath(LoadOptions options = null) => options == null?DEFAULT.TraversePath() : options.TraversePath();
public static LoadOptions NoClobber(LoadOptions options = null) => options == null?DEFAULT.NoClobber() : options.NoClobber();
public static LoadOptions NoEnvVars(LoadOptions options = null) => options == null?DEFAULT.NoEnvVars() : options.NoEnvVars();