From fe1d5116ab8fad97efc421bc90d6f877df49934d Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Thu, 6 Jan 2022 18:41:55 -0500 Subject: [PATCH] pass config path in cmd args --- merlin/merlin.go | 20 +++++++++++++++++--- merlin/sync/command.go | 4 ++-- merlin/sync/command_test.go | 12 ++++++------ 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/merlin/merlin.go b/merlin/merlin.go index b8e3cff..e02247b 100644 --- a/merlin/merlin.go +++ b/merlin/merlin.go @@ -1,6 +1,7 @@ package main import ( + "flag" "fmt" "net" "os" @@ -16,10 +17,23 @@ import ( "git.csclub.uwaterloo.ca/public/merlin/sync" ) -// get config path from command args -var CONFIG_PATH = "merlin-config.ini" +var DEFAULT_CONFIG_PATH = "merlin-config.ini" func main() { + + // custom help message + flag.Usage = func() { + w := flag.CommandLine.Output() + + fmt.Fprintf(w, "USAGE: merlin [-h | --help] [--config=]\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 mirror user has pid of 1001 @@ -43,7 +57,7 @@ func main() { repoIdx := 0 loadConfig := func() { - config.LoadConfig(CONFIG_PATH, doneChan, stopChan) + config.LoadConfig(*configPath, doneChan, stopChan) logger.OutLog("Loaded config:\n" + fmt.Sprintf("%+v\n", config.Conf)) repoIdx = 0 diff --git a/merlin/sync/command.go b/merlin/sync/command.go index e0453a7..d3a107f 100644 --- a/merlin/sync/command.go +++ b/merlin/sync/command.go @@ -86,7 +86,7 @@ func cscSyncCDImage(repo *config.Repo) []string { func cscSyncCeph(repo *config.Repo) []string { - args := []string { + args := []string{ "rsync", "--stats", "--progress", "--quiet", "-4", "--address=" + config.Conf.IPv4Address, repo.RsyncHost + "::ceph", "--recursive", "--times", "--links", "--hard-links", @@ -259,7 +259,7 @@ func getSyncCommand(repo *config.Repo) (args []string) { return cscSyncBadPerms(repo) case "csc-sync-cdimage": return cscSyncCDImage(repo) - case "csc-sync-ceph": + case "csc-sync-ceph": return cscSyncCeph(repo) case "csc-sync-chmod": return cscSyncChmod(repo) diff --git a/merlin/sync/command_test.go b/merlin/sync/command_test.go index f791497..05d3083 100644 --- a/merlin/sync/command_test.go +++ b/merlin/sync/command_test.go @@ -2,8 +2,8 @@ package sync import ( "path/filepath" - "testing" "reflect" + "testing" "git.csclub.uwaterloo.ca/public/merlin/config" "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) 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) - return &config.Repo { + return &config.Repo{ Name: name, SyncType: syncType, FrequencyStr: frequencyStr, @@ -50,9 +50,9 @@ func TestGetSyncCommand(t *testing.T) { config.Conf.DownloadDir = "test_files" - testData := []struct{ - repoConf *config.Repo - expected []string + testData := []struct { + repoConf *config.Repo + expected []string }{ { repoConf: dummyRepoConf("ubuntu", "csc-sync-debian", "bi-hourly", "ubuntu", "archive.ubuntu.com", "ubuntu"),