pass config path in cmd args

This commit is contained in:
Daniel Liu 2022-01-06 18:41:55 -05:00
parent 2b6fe3e1a0
commit fe1d5116ab
3 changed files with 25 additions and 11 deletions

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"net" "net"
"os" "os"
@ -16,10 +17,23 @@ import (
"git.csclub.uwaterloo.ca/public/merlin/sync" "git.csclub.uwaterloo.ca/public/merlin/sync"
) )
// get config path from command args var DEFAULT_CONFIG_PATH = "merlin-config.ini"
var CONFIG_PATH = "merlin-config.ini"
func main() { func main() {
// custom help message
flag.Usage = func() {
w := flag.CommandLine.Output()
fmt.Fprintf(w, "USAGE: merlin [-h | --help] [--config=<config-path>]\n")
flag.PrintDefaults()
}
// parse command args
// if more are added can prob use a library
configPath := flag.String("config", DEFAULT_CONFIG_PATH, "alternate config file")
flag.Parse()
// check that merlin is run as mirror user // check that merlin is run as mirror user
// check that mirror user has pid of 1001 // check that mirror user has pid of 1001
@ -43,7 +57,7 @@ func main() {
repoIdx := 0 repoIdx := 0
loadConfig := func() { loadConfig := func() {
config.LoadConfig(CONFIG_PATH, doneChan, stopChan) config.LoadConfig(*configPath, doneChan, stopChan)
logger.OutLog("Loaded config:\n" + fmt.Sprintf("%+v\n", config.Conf)) logger.OutLog("Loaded config:\n" + fmt.Sprintf("%+v\n", config.Conf))
repoIdx = 0 repoIdx = 0

View File

@ -86,7 +86,7 @@ func cscSyncCDImage(repo *config.Repo) []string {
func cscSyncCeph(repo *config.Repo) []string { func cscSyncCeph(repo *config.Repo) []string {
args := []string { args := []string{
"rsync", "--stats", "--progress", "--quiet", "-4", "--address=" + config.Conf.IPv4Address, "rsync", "--stats", "--progress", "--quiet", "-4", "--address=" + config.Conf.IPv4Address,
repo.RsyncHost + "::ceph", repo.RsyncHost + "::ceph",
"--recursive", "--times", "--links", "--hard-links", "--recursive", "--times", "--links", "--hard-links",
@ -259,7 +259,7 @@ func getSyncCommand(repo *config.Repo) (args []string) {
return cscSyncBadPerms(repo) return cscSyncBadPerms(repo)
case "csc-sync-cdimage": case "csc-sync-cdimage":
return cscSyncCDImage(repo) return cscSyncCDImage(repo)
case "csc-sync-ceph": case "csc-sync-ceph":
return cscSyncCeph(repo) return cscSyncCeph(repo)
case "csc-sync-chmod": case "csc-sync-chmod":
return cscSyncChmod(repo) return cscSyncChmod(repo)

View File

@ -2,8 +2,8 @@ package sync
import ( import (
"path/filepath" "path/filepath"
"testing"
"reflect" "reflect"
"testing"
"git.csclub.uwaterloo.ca/public/merlin/config" "git.csclub.uwaterloo.ca/public/merlin/config"
"git.csclub.uwaterloo.ca/public/merlin/logger" "git.csclub.uwaterloo.ca/public/merlin/logger"
@ -16,10 +16,10 @@ func dummyRepoConf(name string, syncType string, frequencyStr string, localDir s
doneChan := make(chan config.SyncResult) doneChan := make(chan config.SyncResult)
stopChan := make(chan struct{}) stopChan := make(chan struct{})
repoLogFile := filepath.Join("test_files", name, name + ".log") repoLogFile := filepath.Join("test_files", name, name+".log")
logger := logger.NewLogger(name, repoLogFile) logger := logger.NewLogger(name, repoLogFile)
return &config.Repo { return &config.Repo{
Name: name, Name: name,
SyncType: syncType, SyncType: syncType,
FrequencyStr: frequencyStr, FrequencyStr: frequencyStr,
@ -50,9 +50,9 @@ func TestGetSyncCommand(t *testing.T) {
config.Conf.DownloadDir = "test_files" config.Conf.DownloadDir = "test_files"
testData := []struct{ testData := []struct {
repoConf *config.Repo repoConf *config.Repo
expected []string expected []string
}{ }{
{ {
repoConf: dummyRepoConf("ubuntu", "csc-sync-debian", "bi-hourly", "ubuntu", "archive.ubuntu.com", "ubuntu"), repoConf: dummyRepoConf("ubuntu", "csc-sync-debian", "bi-hourly", "ubuntu", "archive.ubuntu.com", "ubuntu"),