All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fi.jubic.easyconfig.providers.DotenvProvider Maven / Gradle / Ivy

There is a newer version: 0.10.4
Show newest version
package fi.jubic.easyconfig.providers;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.github.cdimascio.dotenv.Dotenv;
import io.github.cdimascio.dotenv.DotenvEntry;

import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@SuppressFBWarnings("CT_CONSTRUCTOR_THROW")
public class DotenvProvider extends EnvProvider {
    private final Dotenv dotenv;

    public DotenvProvider() {
        String dotenvDir = System.getenv("EASYCONFIG_DOTENV_DIR");
        if (dotenvDir != null) {
            dotenv = Dotenv.configure()
                    .ignoreIfMissing()
                    .directory(dotenvDir)
                    .load();
        }
        else {
            dotenv = Dotenv.configure()
                    .ignoreIfMissing()
                    .load();
        }
    }

    public DotenvProvider(Dotenv dotenv) {
        this.dotenv = dotenv;
    }

    @Override
    public Optional getVariable(String name) {
        return Optional.ofNullable(dotenv.get(name));
    }

    @Override
    protected Stream getNames() {
        return dotenv.entries()
                .stream()
                .map(DotenvEntry::getKey);
    }

    @Override
    public Map getVariables() {
        return dotenv.entries()
                .stream()
                .collect(Collectors.toMap(
                        DotenvEntry::getKey,
                        DotenvEntry::getValue
                ));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy