public static Option <bool, Error> LoadEnvironmentVariables(IEnvironment environment, string configFilePath, IEnumerable <string> envFiles)
        {
            if (environment == null)
            {
                throw new ArgumentNullException(nameof(environment));
            }
            if (configFilePath == null)
            {
                throw new ArgumentNullException(nameof(configFilePath));
            }

            // .env file  in a current folder
            var defaultEnvFile = Path.Combine(environment.GetCurrentDirectory(), Constants.Default.DotEnvFileName);

            if (environment.FileExists(defaultEnvFile))
            {
                DotNetEnv.Env.Load(defaultEnvFile);
            }
            // .env.local file  in a current folder
            var defaultEnvLocalFile = Path.Combine(environment.GetCurrentDirectory(), Constants.Default.DotEnvLocalFileName);

            if (environment.FileExists(defaultEnvLocalFile))
            {
                DotNetEnv.Env.Load(defaultEnvLocalFile);
            }

            // .env file next to a dbup.yml
            var configFileEnv = Path.Combine(new FileInfo(configFilePath).DirectoryName, Constants.Default.DotEnvFileName);

            if (environment.FileExists(configFileEnv))
            {
                DotNetEnv.Env.Load(configFileEnv);
            }
            // .env.local file next to a dbup.yml
            var configFileEnvLocal = Path.Combine(new FileInfo(configFilePath).DirectoryName, Constants.Default.DotEnvLocalFileName);

            if (environment.FileExists(configFileEnvLocal))
            {
                DotNetEnv.Env.Load(configFileEnvLocal);
            }

            if (envFiles != null)
            {
                foreach (var file in envFiles)
                {
                    Error error = null;
                    ConfigLoader.GetFilePath(environment, file)
                    .Match(
                        some: path => DotNetEnv.Env.Load(path),
                        none: err => error = err);

                    if (error != null)
                    {
                        return(Option.None <bool, Error>(error));
                    }
                }
            }

            return(true.Some <bool, Error>());
        }
Beispiel #2
0
 private Option <int, Error> RunStatusCommand(StatusOptions opts) =>
 ConfigurationHelper.LoadEnvironmentVariables(Environment, opts.File, opts.EnvFiles)
 .Match(
     some: _ => ConfigLoader.LoadMigration(ConfigLoader.GetFilePath(Environment, opts.File))
     .Match(
         some: x =>
         ConfigurationHelper
         .SelectDbProvider(x.Provider, x.ConnectionString, x.ConnectionTimeoutSec)
         .SelectJournal(x.JournalTo)
         .SelectTransaction(x.Transaction)
         .SelectLogOptions(Logger, VerbosityLevel.Min)
         .SelectScripts(x.Scripts)
         .AddVariables(x.Vars)
         .OverrideConnectionFactory(ConnectionFactory)
         .Match(
             some: builder =>
 {
     var engine = builder.Build();
     if (!engine.TryConnect(out var message))
     {
         return(Option.None <int, Error>(Error.Create(message)));