Home | History | Annotate | Download | only in repodiff
      1 package main
      2 
      3 import (
      4 	"fmt"
      5 	"repodiff/constants"
      6 	"repodiff/controllers"
      7 	ent "repodiff/entities"
      8 	"repodiff/handlers"
      9 	"repodiff/persistence/filesystem"
     10 )
     11 
     12 const configFile = "config.json"
     13 
     14 type controllerFunc func(ent.ApplicationConfig) error
     15 
     16 func main() {
     17 	appConfig, err := loadConfig()
     18 	if err != nil {
     19 		panic(formattedError(err))
     20 	}
     21 	statusChannel := make(chan ent.StatusMessage)
     22 	go handlers.StartHTTP(appConfig.Port, statusChannel)
     23 	go run(appConfig, statusChannel)
     24 	select {}
     25 }
     26 
     27 func run(appConfig ent.ApplicationConfig, statusChannel chan ent.StatusMessage) {
     28 	statusChannel <- ent.StatusMessage{
     29 		JobStatus: constants.JobStatusRunning,
     30 	}
     31 
     32 	for _, controllerFn := range getEnabledControllers() {
     33 		if err := controllerFn(appConfig); err != nil {
     34 			topLevelErrorHandle(err, statusChannel)
     35 			return
     36 		}
     37 	}
     38 	statusChannel <- ent.StatusMessage{
     39 		JobStatus: constants.JobStatusComplete,
     40 	}
     41 }
     42 
     43 func getEnabledControllers() []controllerFunc {
     44 	enabled := getEnabledOperations()
     45 	return []controllerFunc{
     46 		disabledFnNullified(controllers.ExecuteDifferentials, enabled.Diff),
     47 		disabledFnNullified(controllers.DenormalizeData, enabled.Denorm),
     48 		disabledFnNullified(controllers.GenerateCommitReport, enabled.Report),
     49 	}
     50 }
     51 
     52 func disabledFnNullified(original controllerFunc, enabled bool) controllerFunc {
     53 	if enabled {
     54 		return original
     55 	}
     56 	return func(ent.ApplicationConfig) error {
     57 		return nil
     58 	}
     59 }
     60 
     61 func topLevelErrorHandle(err error, statusChannel chan ent.StatusMessage) {
     62 	statusChannel <- ent.StatusMessage{
     63 		JobStatus: constants.JobStatusFailed,
     64 		Meta:      formattedError(err),
     65 	}
     66 	fmt.Println(formattedError(err))
     67 }
     68 
     69 func loadConfig() (ent.ApplicationConfig, error) {
     70 	var appConfig ent.ApplicationConfig
     71 	err := filesystem.ReadFileAsJson(configFile, &appConfig)
     72 	if err != nil {
     73 		return appConfig, err
     74 	}
     75 	return appConfig, nil
     76 }
     77 
     78 func formattedError(err error) string {
     79 	return fmt.Sprintf("%+v", err)
     80 }
     81