Adds command line option for config file

main
Yasen Pramatarov 2024-10-07 11:51:36 +03:00
parent a49f16c584
commit 0743113cef
3 changed files with 17 additions and 3 deletions

View File

@ -18,6 +18,7 @@ All notable changes to this project will be documented in this file.
- Added SysV init script
- Added Systemd service file
- Added installation script
- Added command line option for config file
---

View File

@ -5,7 +5,7 @@ After=network.target
[Service]
ExecStart=/usr/local/bin/jilo-agent -c /usr/local/etc/jilo-agent.conf
PIDFile=/run/jilo-agent.pid
Restart=always
Restart=on-failure
SyslogIdentifier=jilo-agent
User=root
Group=root

17
main.go
View File

@ -12,6 +12,7 @@ Version: 0.1
package main
import (
"flag"
"fmt"
"encoding/json"
"gopkg.in/yaml.v2"
@ -290,8 +291,20 @@ func corsMiddleware(next http.Handler) http.Handler {
// main sets up the http server and the routes
func main() {
// load the configuration
config := loadConfig("jilo-agent.conf")
// Define a flag for the config file
configFile := flag.String("c", "./jilo-agent.conf", "Specify the agent config file")
// Parse the flags
flag.Parse()
// Check if the file exists, fallback to default config file if not
if _, err := os.Stat(*configFile); os.IsNotExist(err) {
fmt.Println("Config file not found, using default values")
}
// Load the configuration from the specified file (option -c) or the default config file name
config := loadConfig(*configFile)
secretKey = []byte(config.SecretKey)
mux := http.NewServeMux()