-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feature/config file(#15)
- Loading branch information
Showing
22 changed files
with
250 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,30 @@ | ||
# Mongo-csv-importer | ||
Imports csv items into mongo | ||
|
||
# Environment variables | ||
#Injecting environment variables | ||
|
||
Environment variables can be injected in 2 ways. | ||
1. A config file. | ||
2. The system environment variables. | ||
|
||
When using the config file, create a config file `application.conf` located under | ||
`src/main/resources` | ||
|
||
## Required Environment variables | ||
``` | ||
mongo_address=; | ||
mongo_port=; | ||
mongo_db_name=; | ||
mongo_address= | ||
mongo_port= | ||
mongo_db_name= | ||
``` | ||
|
||
## Optional auth environment variables | ||
``` | ||
mongo_auth_uname=; | ||
mongo_auth_pw=; | ||
mongo_auth_uname= | ||
mongo_auth_pw= | ||
``` | ||
|
||
## Usage | ||
When the user is prompted for their input to the path, the Raw path to the files should be | ||
# Usage | ||
When the user gets prompted for their input to the path, the raw path to the files should be | ||
entered. | ||
eg: `c:/users/username/desktop/csvFiles` | ||
The file under this directory should be `.csv` | ||
The files under this directory should be `.csv` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mongo-address=localhost | ||
mongo-port=27017 | ||
mongo-auth-uname=john.doe | ||
mongo-auth-pw=alessonadaykeepsthedocktoraway | ||
mongo-db-name=northwind |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package util | ||
|
||
import com.typesafe.config.Config | ||
import util.models.OrderedFile | ||
|
||
import scala.reflect.io.File | ||
|
||
object ImplicitConversions { | ||
implicit class listFiles(files : List[(File, Int)]) { | ||
def toOrderedFile: List[OrderedFile] = { | ||
files.map { case (file, index) => | ||
OrderedFile(index, file) | ||
} | ||
} | ||
} | ||
|
||
implicit class RichConfig(val underlying: Config) extends AnyVal { | ||
def getOptionalString(path: String): Option[String] = if (underlying.hasPath(path)) { | ||
Some(underlying.getString(path)) | ||
} else { | ||
None | ||
} | ||
} | ||
} | ||
|
2 changes: 1 addition & 1 deletion
2
src/main/scala/Util/Logging.scala → src/main/scala/util/Logging.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package Util | ||
package util | ||
|
||
import com.typesafe.scalalogging.Logger | ||
import org.slf4j.LoggerFactory | ||
|
10 changes: 5 additions & 5 deletions
10
src/main/scala/Util/UserPrompt.scala → src/main/scala/util/UserPrompt.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package util.config | ||
|
||
import util.databaseProvider.SystemConfigProperties | ||
import util.models.SystemConfigPropertiesResponse | ||
import com.typesafe.config.ConfigFactory | ||
import util.ErrorHandler._ | ||
import cats.implicits._ | ||
import util.Logging.log | ||
import util.ImplicitConversions.RichConfig | ||
import util.models.enums.MongoDbEnvironmentNames | ||
|
||
case object ConfigFile extends SystemConfigProperties { | ||
/** validate of the configeration method is present | ||
* | ||
* @return Boolean, true if it exists | ||
*/ | ||
override def exists: Boolean = { | ||
val exist = !ConfigFactory.load().isEmpty | ||
log.info(s"Check if config file: application.conf exists: $exist") | ||
exist | ||
} | ||
|
||
/** extracts the config variables | ||
* | ||
* @return SystemConfigPropertiesResponse | ||
*/ | ||
override def extractConfig: Either[Exception, SystemConfigPropertiesResponse] = { | ||
try { | ||
SystemConfigPropertiesResponse( | ||
ConfigFactory.load().getString(MongoDbEnvironmentNames.address.value), | ||
ConfigFactory.load().getInt(MongoDbEnvironmentNames.port.value), | ||
ConfigFactory.load().getString(MongoDbEnvironmentNames.name.value), | ||
ConfigFactory.load().getOptionalString(MongoDbEnvironmentNames.username.value), | ||
ConfigFactory.load().getOptionalString(MongoDbEnvironmentNames.password.value).map(_.toCharArray) | ||
).asRight[Exception] | ||
|
||
} catch { | ||
case e: Exception => errorL(e) | ||
} | ||
|
||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package util.config | ||
|
||
import util.models.SystemConfigPropertiesResponse | ||
import util.ErrorHandler._ | ||
import util.databaseProvider.SystemConfigProperties | ||
|
||
case object ConfigHandler { | ||
def init: Either[Exception, SystemConfigPropertiesResponse] = { | ||
val environments: List[SystemConfigProperties] = List(ConfigFile, EnvironmentVariables) | ||
|
||
getEnvironment(environments) | ||
} | ||
|
||
private def getEnvironment(environments: List[SystemConfigProperties]): Either[Exception, SystemConfigPropertiesResponse] = { | ||
environments.find(_.exists).map(_.extractConfig) | ||
.getOrElse(errorL("No environments are defined")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package util.config | ||
|
||
import util.ErrorHandler.error | ||
import util.databaseProvider.SystemConfigProperties | ||
import util.models.SystemConfigPropertiesResponse | ||
import cats.implicits._ | ||
import util.Logging.log | ||
import util.models.enums.MongoDbEnvironmentNames | ||
|
||
case object EnvironmentVariables extends SystemConfigProperties { | ||
/** validate of the configeration method is present | ||
* | ||
* @return Boolean, true if it exists | ||
*/ | ||
override def exists: Boolean = { | ||
val exist = extractConfig.isRight | ||
log.info(s"Check if env vars exists: $exist") | ||
exist | ||
} | ||
|
||
/** extracts the config variables | ||
* | ||
* @return SystemConfigPropertiesResponse | ||
*/ | ||
override def extractConfig: Either[Exception, SystemConfigPropertiesResponse] = { | ||
for { | ||
address <- Either.fromOption(sys.env.get(MongoDbEnvironmentNames.address.value), error("mongo address not found")) | ||
port <- Either.fromOption(sys.env.get(MongoDbEnvironmentNames.port.value).map(_.toInt), error("mongo port not found")) | ||
databaseName <- Either.fromOption(sys.env.get(MongoDbEnvironmentNames.name.value), error("environment variables not set for db name")) | ||
} yield { | ||
val user = sys.env.get(MongoDbEnvironmentNames.username.value) | ||
val password = sys.env.get(MongoDbEnvironmentNames.password.value).map(_.toCharArray) | ||
|
||
SystemConfigPropertiesResponse(address, port, databaseName, user, password) | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 5 additions & 5 deletions
10
...n/scala/Util/DataBuilder/Processing.scala → ...n/scala/util/dataBuilder/Processing.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.