Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command line arguments for config files #36

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion data.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func loadData() {
// Read data from 'devices.json' file

devicesFile, fileErr := os.Open("devices.json")
devicesFile, fileErr := os.Open(args.DevicesPath)
if fileErr != nil {
log.Fatalf("Error loading devices.json file. \"%s\"", fileErr)
}
Expand Down
36 changes: 34 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"flag"
"log"
"net"
"net/http"
Expand All @@ -18,9 +19,15 @@ import (
// Global variables
var appConfig AppConfig
var appData AppData
var args Args

func main() {
type Args struct {
ConfigPath string
DevicesPath string
}

func main() {
processArgs()
setWorkingDir()
loadConfig()
loadData()
Expand All @@ -42,7 +49,7 @@ func setWorkingDir() {

func loadConfig() {

err := cleanenv.ReadConfig("config.json", &appConfig)
err := cleanenv.ReadConfig(args.ConfigPath, &appConfig)
if err != nil {
log.Fatalf("Error loading config.json file. \"%s\"", err)
}
Expand Down Expand Up @@ -104,3 +111,28 @@ func CacheControlWrapper(h http.Handler) http.Handler {
h.ServeHTTP(w, r)
})
}

func processArgs() {
var configPath string
var devicesPath string

f := flag.NewFlagSet("wolweb", 1)

f.StringVar(&configPath, "c", "config.json", "Path to configuration file")
f.StringVar(&devicesPath, "d", "devices.json", "Path to devices file")

f.Parse(os.Args[1:])

configPath, err := filepath.Abs(configPath)
if err != nil {
log.Fatal(err)
}

devicesPath, err = filepath.Abs(devicesPath)
if err != nil {
log.Fatal(err)
}

args.ConfigPath = configPath
args.DevicesPath = devicesPath
}