|
6 | 6 | "aunefyren/poenskelisten/controllers"
|
7 | 7 | "aunefyren/poenskelisten/database"
|
8 | 8 | "aunefyren/poenskelisten/middlewares"
|
| 9 | + "aunefyren/poenskelisten/models" |
9 | 10 | "aunefyren/poenskelisten/utilities"
|
| 11 | + "flag" |
10 | 12 | "fmt"
|
11 | 13 | "log"
|
12 | 14 | "net/http"
|
@@ -59,12 +61,20 @@ func main() {
|
59 | 61 | log.Println("Configuration file loaded.")
|
60 | 62 | fmt.Println("Configuration file loaded.")
|
61 | 63 |
|
| 64 | + // Change the config to respect flags |
| 65 | + Config, err = parseFlags(Config) |
| 66 | + if err != nil { |
| 67 | + log.Println("Failed to parse input flags. Error: " + err.Error()) |
| 68 | + fmt.Println("Failed to parse input flags. Error: " + err.Error()) |
| 69 | + |
| 70 | + os.Exit(1) |
| 71 | + } |
| 72 | + |
62 | 73 | // Set time zone from config if it is not empty
|
63 | 74 | if Config.Timezone != "" {
|
64 | 75 | loc, err := time.LoadLocation(Config.Timezone)
|
65 | 76 | if err != nil {
|
66 | 77 | fmt.Println("Failed to set time zone from config. Error: " + err.Error())
|
67 |
| - fmt.Println(err) |
68 | 78 | fmt.Println("Removing value...")
|
69 | 79 |
|
70 | 80 | log.Println("Failed to set time zone from config. Error: " + err.Error())
|
@@ -250,3 +260,80 @@ func initRouter() *gin.Engine {
|
250 | 260 |
|
251 | 261 | return router
|
252 | 262 | }
|
| 263 | + |
| 264 | +func parseFlags(Config *models.ConfigStruct) (*models.ConfigStruct, error) { |
| 265 | + |
| 266 | + // Define flag variables with the configuration file as default values |
| 267 | + var port int |
| 268 | + flag.IntVar(&port, "port", Config.PoenskelistenPort, "The port Pønskelisten is listening on.") |
| 269 | + |
| 270 | + var timezone string |
| 271 | + flag.StringVar(&timezone, "timezone", Config.Timezone, "The timezone Pønskelisten is running in.") |
| 272 | + |
| 273 | + var dbPort int |
| 274 | + flag.IntVar(&dbPort, "dbport", Config.DBPort, "The port the database is listening on.") |
| 275 | + |
| 276 | + var dbUsername string |
| 277 | + flag.StringVar(&dbUsername, "dbusername", Config.DBUsername, "The username used to interact with the database.") |
| 278 | + |
| 279 | + var dbPassword string |
| 280 | + flag.StringVar(&dbPassword, "dbpassword", Config.DBPassword, "The password used to interact with the database.") |
| 281 | + |
| 282 | + var dbName string |
| 283 | + flag.StringVar(&dbName, "dbname", Config.DBName, "The database table used within the database.") |
| 284 | + |
| 285 | + var dbIP string |
| 286 | + flag.StringVar(&dbIP, "dbip", Config.DBIP, "The IP address used to reach the database.") |
| 287 | + |
| 288 | + // Parse the flags from input |
| 289 | + flag.Parse() |
| 290 | + |
| 291 | + // Respect the flag if config is empty |
| 292 | + if Config.PoenskelistenPort == 0 { |
| 293 | + Config.PoenskelistenPort = port |
| 294 | + } |
| 295 | + |
| 296 | + // Respect the flag if config is empty |
| 297 | + if Config.Timezone == "" { |
| 298 | + Config.Timezone = timezone |
| 299 | + } |
| 300 | + |
| 301 | + // Respect the flag if config is empty |
| 302 | + if Config.DBPort == 0 { |
| 303 | + Config.DBPort = dbPort |
| 304 | + } |
| 305 | + |
| 306 | + // Respect the flag if config is empty |
| 307 | + if Config.DBUsername == "" { |
| 308 | + Config.DBUsername = dbUsername |
| 309 | + } |
| 310 | + |
| 311 | + // Respect the flag if config is empty |
| 312 | + if Config.DBPassword == "" { |
| 313 | + Config.DBPassword = dbPassword |
| 314 | + } |
| 315 | + |
| 316 | + // Respect the flag if config is empty |
| 317 | + if Config.DBName == "" { |
| 318 | + Config.DBName = dbName |
| 319 | + } |
| 320 | + |
| 321 | + // Respect the flag if config is empty |
| 322 | + if Config.DBIP == "" { |
| 323 | + Config.DBIP = dbIP |
| 324 | + } |
| 325 | + |
| 326 | + // Failsafe, if port is 0, set to default 8080 |
| 327 | + if Config.PoenskelistenPort == 0 { |
| 328 | + Config.PoenskelistenPort = 8080 |
| 329 | + } |
| 330 | + |
| 331 | + // Save the new config |
| 332 | + err := config.SaveConfig(Config) |
| 333 | + if err != nil { |
| 334 | + return &models.ConfigStruct{}, err |
| 335 | + } |
| 336 | + |
| 337 | + return Config, nil |
| 338 | + |
| 339 | +} |
0 commit comments