2014-04-26 09:18:56 +00:00
package Configuration
2014-10-19 17:19:11 +00:00
import (
"encoding/json"
2015-06-21 18:20:27 +00:00
"fmt"
2014-10-19 17:19:11 +00:00
"github.com/SommerEngineering/Ocean/Log"
"github.com/SommerEngineering/Ocean/Log/Meta"
"os"
"path/filepath"
)
2014-04-26 09:18:56 +00:00
2015-06-17 15:44:52 +00:00
// Function to read the configuration file.
2014-04-26 09:18:56 +00:00
func readConfiguration ( ) {
if isInit {
Log . LogFull ( senderName , Meta . CategorySYSTEM , Meta . LevelWARN , Meta . SeverityNone , Meta . ImpactNone , Meta . MessageNameINIT , ` The configuration package is already init! ` )
return
} else {
Log . LogShort ( senderName , Meta . CategorySYSTEM , Meta . LevelINFO , Meta . MessageNameCONFIGURATION , ` Init of configuration starting. ` )
}
2015-06-17 15:44:52 +00:00
// Access to the working directory?
2014-04-26 09:18:56 +00:00
currentDir , dirError := os . Getwd ( )
if dirError != nil {
2015-06-21 18:20:27 +00:00
fmt . Printf ( "[Error] Was not able to read the working directory. %s\n" , dirError . Error ( ) )
os . Exit ( 0 )
2014-04-26 09:18:56 +00:00
}
2015-06-17 15:44:52 +00:00
// Access to the configuration file?
2014-04-26 09:18:56 +00:00
currentPath := filepath . Join ( currentDir , filename )
if _ , errFile := os . Stat ( currentPath ) ; errFile != nil {
if os . IsNotExist ( errFile ) {
2015-06-21 18:20:27 +00:00
fmt . Printf ( "[Error] Cannot read the project name file 'configuration.json': File not found! Please read https://github.com/SommerEngineering/Ocean\n" )
2014-04-26 09:18:56 +00:00
} else {
2015-06-21 18:20:27 +00:00
fmt . Printf ( "[Error] Cannot read the project name file 'configuration.json': %s. Please read https://github.com/SommerEngineering/Ocean\n" , errFile . Error ( ) )
2014-04-26 09:18:56 +00:00
}
2015-06-21 18:20:27 +00:00
os . Exit ( 0 )
2014-04-26 09:18:56 +00:00
}
2015-06-17 15:44:52 +00:00
// Open the file:
2014-04-26 09:18:56 +00:00
file , fileError := os . Open ( currentPath )
defer file . Close ( )
if fileError != nil {
2015-06-21 18:20:27 +00:00
fmt . Printf ( "[Error] The configuration file 'configuration.json' is not accessible: %s. Please read https://github.com/SommerEngineering/Ocean\n" , fileError . Error ( ) )
os . Exit ( 0 )
2014-04-26 09:18:56 +00:00
}
2015-06-17 15:44:52 +00:00
// Try to decode / parse the file:
2014-04-26 09:18:56 +00:00
decoder := json . NewDecoder ( file )
decError := decoder . Decode ( & configuration )
if decError != nil {
2015-06-21 18:20:27 +00:00
fmt . Printf ( "[Error] Decoding of the configuration file 'configuration.json' was not possible: %s. Please read https://github.com/SommerEngineering/Ocean\n" , decError . Error ( ) )
os . Exit ( 0 )
2014-04-26 09:18:56 +00:00
}
Log . LogShort ( senderName , Meta . CategorySYSTEM , Meta . LevelINFO , Meta . MessageNameINIT , ` Init of configuration is done. ` )
isInit = true
return
}