From 587c8bf41568c9624f9874bb4e2549227c1e3b63 Mon Sep 17 00:00:00 2001 From: KatolaZ Date: Tue, 1 Aug 2017 08:13:42 +0100 Subject: sources passed thorugh gometalinter --- commits.go | 25 ++++++++++------------- exec.go | 4 ++-- scorshd.go | 22 +++++++++++--------- spooler.go | 4 ++-- types.go | 27 +++++++++++++------------ workers.go | 68 +++++++++++++++++++++++++++++++++----------------------------- 6 files changed, 77 insertions(+), 73 deletions(-) diff --git a/commits.go b/commits.go index 3381a86..a9c98f7 100644 --- a/commits.go +++ b/commits.go @@ -11,18 +11,15 @@ import ( // "log" ) -func commitToString(commit *git.Commit) string { - - var ret string - - ret += fmt.Sprintf("type: %s\n", commit.Type()) - ret += fmt.Sprintf("Id: %s\n", commit.Id()) - ret += fmt.Sprintf("Author: %s\n", commit.Author()) - ret += fmt.Sprintf("Message: %s\n", commit.Message()) - ret += fmt.Sprintf("Parent-count: %d\n", commit.ParentCount()) - - return ret -} +// func commitToString(commit *git.Commit) string { +// var ret string +// ret += fmt.Sprintf("type: %s\n", commit.Type()) +// ret += fmt.Sprintf("Id: %s\n", commit.Id()) +// ret += fmt.Sprintf("Author: %s\n", commit.Author()) +// ret += fmt.Sprintf("Message: %s\n", commit.Message()) +// ret += fmt.Sprintf("Parent-count: %d\n", commit.ParentCount()) +// return ret +// } // FIXME: RETURN THE ENTITY PROVIDED BY THE CHECK, OR nil func checkSignature(commit *git.Commit, keyring *openpgp.KeyRing) (signature, signed string, err error) { @@ -130,7 +127,7 @@ func walkCommits(msg SCORSHmsg, w *SCORSHworker) error { return SCORSHerr(SCORSH_ERR_NO_REPO) } - oldRevOid, err := git.NewOid(oldRev) + oldRevOid, _ := git.NewOid(oldRev) oldrevCommit, err := repo.LookupCommit(oldRevOid) if err != nil { @@ -138,7 +135,7 @@ func walkCommits(msg SCORSHmsg, w *SCORSHworker) error { return SCORSHerr(SCORSH_ERR_NO_COMMIT) } - newRevOid, err := git.NewOid(newRev) + newRevOid, _ := git.NewOid(newRev) newrevCommit, err := repo.LookupCommit(newRevOid) if err != nil { diff --git a/exec.go b/exec.go index 88f878f..7a3b0cc 100644 --- a/exec.go +++ b/exec.go @@ -26,7 +26,7 @@ func execLocalFile(cmdURL *url.URL, args, env []string) error { buff := bufio.NewScanner(stdout) log.Printf("[%s - stout follows: ]\n", cmd.Path) for buff.Scan() { - log.Printf(buff.Text()) // write each line to your log, or anything you need + log.Print(buff.Text()) // write each line to your log, or anything you need } err = cmd.Wait() } @@ -97,7 +97,7 @@ func setEnvironment(msg *SCORSHmsg, tag, author, committer string) []string { env = append(env, fmt.Sprintf("SCORSH_BRANCH=%s", msg.Branch)) env = append(env, fmt.Sprintf("SCORSH_OLDREV=%s", msg.OldRev)) env = append(env, fmt.Sprintf("SCORSH_NEWREV=%s", msg.NewRev)) - env = append(env, fmt.Sprintf("SCORSH_ID=%s", msg.Id)) + env = append(env, fmt.Sprintf("SCORSH_ID=%s", msg.ID)) env = append(env, fmt.Sprintf("SCORSH_TAG=%s", tag)) env = append(env, fmt.Sprintf("SCORSH_AUTHOR=%s", author)) env = append(env, fmt.Sprintf("SCORSH_COMMITTER=%s", committer)) diff --git a/scorshd.go b/scorshd.go index c9ad835..b69e501 100644 --- a/scorshd.go +++ b/scorshd.go @@ -22,6 +22,8 @@ func (d debugging) log(format string, args ...interface{}) { var confFile = flag.String("c", "./scorsh.cfg", "Configuration file for SCORSH") +// SCORSHerr converts numeric error values in the corresponding +// description string func SCORSHerr(err int) error { var errStr string @@ -75,31 +77,31 @@ func runMaster(master *SCORSHmaster) { debug.log("[master] matching workers: \n%s\n", matchingWorkers) // add the message to WorkingMsg, if it's not a duplicate! - if _, ok := master.WorkingMsg[pushMsg.Id]; ok { - log.Printf("[master] detected duplicate message %s \n", pushMsg.Id) + if _, ok := master.WorkingMsg[pushMsg.ID]; ok { + log.Printf("[master] detected duplicate message %s \n", pushMsg.ID) } else { - master.WorkingMsg[pushMsg.Id] = 0 + master.WorkingMsg[pushMsg.ID] = 0 // - dispatch the message to all the matching workers for _, w := range matchingWorkers { debug.log("[master] sending msg to worker: %s\n", w.Name) // send the message to the worker w.MsgChan <- pushMsg // increase the counter associated to the message - master.WorkingMsg[pushMsg.Id]++ - debug.log("[master] now WorkingMsg[%s] is: %d\n", pushMsg.Id, master.WorkingMsg[pushMsg.Id]) + master.WorkingMsg[pushMsg.ID]++ + debug.log("[master] now WorkingMsg[%s] is: %d\n", pushMsg.ID, master.WorkingMsg[pushMsg.ID]) } } case doneMsg := <-master.StatusChan: // Here we manage a status message from a worker debug.log("[master] received message from StatusChan: %s\n", doneMsg) - if _, ok := master.WorkingMsg[doneMsg.Id]; ok && master.WorkingMsg[doneMsg.Id] > 0 { - master.WorkingMsg[doneMsg.Id]-- - if master.WorkingMsg[doneMsg.Id] == 0 { - delete(master.WorkingMsg, doneMsg.Id) + if _, ok := master.WorkingMsg[doneMsg.ID]; ok && master.WorkingMsg[doneMsg.ID] > 0 { + master.WorkingMsg[doneMsg.ID]-- + if master.WorkingMsg[doneMsg.ID] == 0 { + delete(master.WorkingMsg, doneMsg.ID) master.Spooler <- doneMsg } } else { - log.Printf("[master] received completion event for non-existing message name: %s\n", doneMsg.Id) + log.Printf("[master] received completion event for non-existing message name: %s\n", doneMsg.ID) } } } diff --git a/spooler.go b/spooler.go index b25167b..e5fe4be 100644 --- a/spooler.go +++ b/spooler.go @@ -76,12 +76,12 @@ func startSpooler(master *SCORSHmaster) error { watcher, err := fsnotify.NewWatcher() if err != nil { - return fmt.Errorf("Error creating watcher: %s\n", err) + return fmt.Errorf("error creating watcher: %s", err) } err = watcher.Add(master.Spooldir) if err != nil { - return fmt.Errorf("Error adding folder: %s\n", err) + return fmt.Errorf("error adding folder: %s", err) } go spooler(watcher, master.Spooler) diff --git a/types.go b/types.go index bbd2ecc..2663c07 100644 --- a/types.go +++ b/types.go @@ -6,6 +6,7 @@ import ( "golang.org/x/crypto/openpgp" ) +// error constants const ( SCORSH_ERR_NO_FILE = -(1 << iota) SCORSH_ERR_KEYRING @@ -14,10 +15,10 @@ const ( SCORSH_ERR_SIGNATURE ) -// the SCORSHmsg type represents messages received from the spool and +// SCORSHmsg type represents messages received from the spool and // sent to workers type SCORSHmsg struct { - Id string `yaml:"m_id"` + ID string `yaml:"m_id"` Repo string `yaml:"m_repo"` Branch string `yaml:"m_branch"` OldRev string `yaml:"m_oldrev"` @@ -25,18 +26,20 @@ type SCORSHmsg struct { Path string } +// SCORSHcmd represents commands configured on the server side type SCORSHcmd struct { URL string `yaml:"c_url"` Hash string `yaml:"c_hash"` } +// SCORSHtagCfg represents tags configured on the server side type SCORSHtagCfg struct { Name string `yaml:"t_name"` Keyrings []string `yaml:"t_keyrings"` Commands []SCORSHcmd `yaml:"t_commands"` } -// Configuration of a worker +// SCORSHworkerCfg represents the static configuration of a worker type SCORSHworkerCfg struct { Name string `yaml:"w_name"` Repos []string `yaml:"w_repos"` @@ -48,21 +51,20 @@ type SCORSHworkerCfg struct { TagKeys map[string]map[string]bool } -// State of a worker +// SCORSHworkerState represents the runtime state of a worker type SCORSHworkerState struct { Keys map[string]openpgp.KeyRing MsgChan chan SCORSHmsg StatusChan chan SCORSHmsg } -// The type SCORSHworker represents the configuration and state of a -// worker +// SCORSHworker represents the configuration and state of a worker type SCORSHworker struct { SCORSHworkerCfg `yaml:",inline"` SCORSHworkerState } -// Configuration of the master +// SCORSHmasterCfg represents the static configuration of the master type SCORSHmasterCfg struct { Spooldir string `yaml:"s_spooldir"` Logfile string `yaml:"s_logfile"` @@ -70,7 +72,7 @@ type SCORSHmasterCfg struct { Workers []SCORSHworker `yaml:"s_workers"` } -// State of the master +// SCORSHmasterState represents the runtime state of the master type SCORSHmasterState struct { Spooler chan SCORSHmsg StatusChan chan SCORSHmsg @@ -78,20 +80,19 @@ type SCORSHmasterState struct { WorkingMsg map[string]int } -// The type SCORSHmaster represents the configuration and state of the -// master +// SCORSHmaster represents the configuration and state of the master type SCORSHmaster struct { SCORSHmasterCfg `yaml:",inline"` SCORSHmasterState } -// client commands - +// SCORSHtag is the type of commands sent by clients type SCORSHtag struct { Tag string `yaml:"s_tag"` Args []string `yaml:"s_args"` } +// SCORSHclientMsg is the list of commands sent by a client type SCORSHclientMsg struct { Tags []SCORSHtag `yaml:"scorsh"` } @@ -117,7 +118,7 @@ func (cfg *SCORSHmaster) String() string { func (msg *SCORSHmsg) String() string { var buff bytes.Buffer - fmt.Fprintf(&buff, "Id: %s\n", msg.Id) + fmt.Fprintf(&buff, "Id: %s\n", msg.ID) fmt.Fprintf(&buff, "Repo: %s\n", msg.Repo) fmt.Fprintf(&buff, "Branch: %s\n", msg.Branch) fmt.Fprintf(&buff, "OldRev: %s\n", msg.OldRev) diff --git a/workers.go b/workers.go index 2562b68..84bd670 100644 --- a/workers.go +++ b/workers.go @@ -11,23 +11,27 @@ import ( "strings" ) -func (worker *SCORSHworker) Matches(repo, branch string) bool { +// Matches returns true if the configured repo:branch of the worker +// matches the repo and branch provided as arguments +func (w *SCORSHworker) Matches(repo, branch string) bool { - for _, r := range worker.Repos { + for _, r := range w.Repos { parts := strings.SplitN(r, ":", 2) - repo_pattern := parts[0] - branch_pattern := parts[1] - repo_match, _ := regexp.MatchString(repo_pattern, repo) - branch_match, _ := regexp.MatchString(branch_pattern, branch) - debug.log("[worker.Matches] repo_match: %s\n", repo_match) - debug.log("[worker.Matches] branch_match: %s\n", branch_match) - if repo_match && branch_match { + repoPattern := parts[0] + branchPattern := parts[1] + repoMatch, _ := regexp.MatchString(repoPattern, repo) + branchMatch, _ := regexp.MatchString(branchPattern, branch) + debug.log("[worker.Matches] repo_match: %s\n", repoMatch) + debug.log("[worker.Matches] branch_match: %s\n", branchMatch) + if repoMatch && branchMatch { return true } } return false } +// LoadKeyrings loads the configured keyrings for all the commands +// managed by the worker func (w *SCORSHworker) LoadKeyrings() error { w.Keys = make(map[string]openpgp.KeyRing) @@ -43,39 +47,39 @@ func (w *SCORSHworker) LoadKeyrings() error { w.TagKeys[t.Name][keyring] = true continue } - k_file := fmt.Sprintf("%s/%s", w.Folder, keyring) - debug.log("[worker: %s] Trying to open keyring at %s\n", w.Name, k_file) - f, err_file := os.Open(k_file) - if err_file != nil { - log.Printf("[worker] cannot open keyring: %s", err_file) - f.Close() + kfile := fmt.Sprintf("%s/%s", w.Folder, keyring) + debug.log("[worker: %s] Trying to open keyring at %s\n", w.Name, kfile) + f, errFile := os.Open(kfile) + if errFile != nil { + log.Printf("[worker] cannot open keyring: %s", errFile) + _ = f.Close() } // load the keyring - kr, err_key := openpgp.ReadArmoredKeyRing(f) + kr, errKey := openpgp.ReadArmoredKeyRing(f) - if err_key != nil { - log.Printf("[worker] cannot load keyring: %s", err_key) - f.Close() + if errKey != nil { + log.Printf("[worker] cannot load keyring: %s", errKey) + _ = f.Close() //return fmt.Errorf("Unable to load keyring: ", err_key) } w.Keys[keyring] = kr w.TagKeys[t.Name][keyring] = true - f.Close() + _ = f.Close() } } return nil } -// Still to be implemented +// LoadTags loads all the configured commands for the worker func (w *SCORSHworker) LoadTags() error { - w_tags, err := ioutil.ReadFile(w.Tagfile) + wTags, err := ioutil.ReadFile(w.Tagfile) if err != nil { return fmt.Errorf("Cannot read worker config: %s", err) } - err = yaml.Unmarshal(w_tags, w) + err = yaml.Unmarshal(wTags, w) //err = yaml.Unmarshal(w_tags, tags) if err != nil { @@ -85,7 +89,7 @@ func (w *SCORSHworker) LoadTags() error { return nil } -// FIXME--- still needs some work... +// func runWorker(w *SCORSHworker) { var msg SCORSHmsg @@ -99,7 +103,7 @@ func runWorker(w *SCORSHworker) { for { select { case msg = <-w.MsgChan: - debug.log("[worker: %s] received message %s\n", w.Name, msg.Id) + debug.log("[worker: %s] received message %s\n", w.Name, msg.ID) // process message err := walkCommits(msg, w) if err != nil { @@ -115,13 +119,13 @@ func runWorker(w *SCORSHworker) { // configuration and fills in the SCORSHmaster struct func startWorkers(master *SCORSHmaster) error { - num_workers := len(master.Workers) + numWorkers := len(master.Workers) // We should now start each worker - log.Printf("num_workers: %d\n", num_workers) + log.Printf("num_workers: %d\n", numWorkers) - for w := 0; w < num_workers; w++ { + for w := 0; w < numWorkers; w++ { worker := &(master.Workers[w]) // Set the Status and Msg channels @@ -132,19 +136,19 @@ func startWorkers(master *SCORSHmaster) error { err := worker.LoadTags() if err != nil { close(worker.MsgChan) - return fmt.Errorf("[Starting worker: %s] Unable to load tags: %s\n", worker.Name, err) + return fmt.Errorf("[Starting worker: %s] Unable to load tags: %s", worker.Name, err) } // Load worker keyrings -- this must be called *after* LoadTags!!!! err = worker.LoadKeyrings() if err != nil { close(worker.MsgChan) - return fmt.Errorf("[Starting worker: %s] Unable to load keyrings: %s\n", worker.Name, err) + return fmt.Errorf("[Starting worker: %s] Unable to load keyrings: %s", worker.Name, err) } // Add the repos definitions to the map master.Repos - for _, repo_name := range worker.Repos { - master.Repos[repo_name] = append(master.Repos[repo_name], worker) + for _, repoName := range worker.Repos { + master.Repos[repoName] = append(master.Repos[repoName], worker) } go runWorker(worker) <-master.StatusChan -- cgit v1.2.3