Beispiel #1
0
 public static void Load(LoadOptions options)
 => Load(Path.Combine(Directory.GetCurrentDirectory(), ".env"), options);
Beispiel #2
0
 public static void Load(LoadOptions options)
 => Load(Path.Combine(Directory.GetCurrentDirectory(), DEFAULT_ENVFILENAME), options);
Beispiel #3
0
        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));
        }
Beispiel #4
0
        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));
            }
        }
Beispiel #5
0
 public static LoadOptions TraversePath() => LoadOptions.TraversePath();
Beispiel #6
0
 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))
                ));
 }
Beispiel #7
0
 public static LoadOptions NoClobber() => LoadOptions.NoClobber();
Beispiel #8
0
 public static LoadOptions NoEnvVars() => LoadOptions.NoEnvVars();
Beispiel #9
0
 public static LoadOptions TraversePath(LoadOptions options = null) =>
 options == null?DEFAULT.TraversePath() : options.TraversePath();
Beispiel #10
0
 public static LoadOptions NoClobber(LoadOptions options = null) =>
 options == null?DEFAULT.NoClobber() : options.NoClobber();
Beispiel #11
0
 public static LoadOptions NoEnvVars(LoadOptions options = null) =>
 options == null?DEFAULT.NoEnvVars() : options.NoEnvVars();