sources passed thorugh gometalinter

lint-refactor
KatolaZ 8 years ago
parent 3fcaf22bbb
commit 587c8bf415
  1. 25
      commits.go
  2. 4
      exec.go
  3. 22
      scorshd.go
  4. 4
      spooler.go
  5. 27
      types.go
  6. 68
      workers.go

@ -11,18 +11,15 @@ import (
// "log" // "log"
) )
func commitToString(commit *git.Commit) string { // func commitToString(commit *git.Commit) string {
// var ret string
var ret string // ret += fmt.Sprintf("type: %s\n", commit.Type())
// ret += fmt.Sprintf("Id: %s\n", commit.Id())
ret += fmt.Sprintf("type: %s\n", commit.Type()) // ret += fmt.Sprintf("Author: %s\n", commit.Author())
ret += fmt.Sprintf("Id: %s\n", commit.Id()) // ret += fmt.Sprintf("Message: %s\n", commit.Message())
ret += fmt.Sprintf("Author: %s\n", commit.Author()) // ret += fmt.Sprintf("Parent-count: %d\n", commit.ParentCount())
ret += fmt.Sprintf("Message: %s\n", commit.Message()) // return ret
ret += fmt.Sprintf("Parent-count: %d\n", commit.ParentCount()) // }
return ret
}
// FIXME: RETURN THE ENTITY PROVIDED BY THE CHECK, OR nil // FIXME: RETURN THE ENTITY PROVIDED BY THE CHECK, OR nil
func checkSignature(commit *git.Commit, keyring *openpgp.KeyRing) (signature, signed string, err error) { 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) return SCORSHerr(SCORSH_ERR_NO_REPO)
} }
oldRevOid, err := git.NewOid(oldRev) oldRevOid, _ := git.NewOid(oldRev)
oldrevCommit, err := repo.LookupCommit(oldRevOid) oldrevCommit, err := repo.LookupCommit(oldRevOid)
if err != nil { if err != nil {
@ -138,7 +135,7 @@ func walkCommits(msg SCORSHmsg, w *SCORSHworker) error {
return SCORSHerr(SCORSH_ERR_NO_COMMIT) return SCORSHerr(SCORSH_ERR_NO_COMMIT)
} }
newRevOid, err := git.NewOid(newRev) newRevOid, _ := git.NewOid(newRev)
newrevCommit, err := repo.LookupCommit(newRevOid) newrevCommit, err := repo.LookupCommit(newRevOid)
if err != nil { if err != nil {

@ -26,7 +26,7 @@ func execLocalFile(cmdURL *url.URL, args, env []string) error {
buff := bufio.NewScanner(stdout) buff := bufio.NewScanner(stdout)
log.Printf("[%s - stout follows: ]\n", cmd.Path) log.Printf("[%s - stout follows: ]\n", cmd.Path)
for buff.Scan() { 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() 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_BRANCH=%s", msg.Branch))
env = append(env, fmt.Sprintf("SCORSH_OLDREV=%s", msg.OldRev)) 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_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_TAG=%s", tag))
env = append(env, fmt.Sprintf("SCORSH_AUTHOR=%s", author)) env = append(env, fmt.Sprintf("SCORSH_AUTHOR=%s", author))
env = append(env, fmt.Sprintf("SCORSH_COMMITTER=%s", committer)) env = append(env, fmt.Sprintf("SCORSH_COMMITTER=%s", committer))

@ -22,6 +22,8 @@ func (d debugging) log(format string, args ...interface{}) {
var confFile = flag.String("c", "./scorsh.cfg", "Configuration file for SCORSH") 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 { func SCORSHerr(err int) error {
var errStr string var errStr string
@ -75,31 +77,31 @@ func runMaster(master *SCORSHmaster) {
debug.log("[master] matching workers: \n%s\n", matchingWorkers) debug.log("[master] matching workers: \n%s\n", matchingWorkers)
// add the message to WorkingMsg, if it's not a duplicate! // add the message to WorkingMsg, if it's not a duplicate!
if _, ok := master.WorkingMsg[pushMsg.Id]; ok { if _, ok := master.WorkingMsg[pushMsg.ID]; ok {
log.Printf("[master] detected duplicate message %s \n", pushMsg.Id) log.Printf("[master] detected duplicate message %s \n", pushMsg.ID)
} else { } else {
master.WorkingMsg[pushMsg.Id] = 0 master.WorkingMsg[pushMsg.ID] = 0
// - dispatch the message to all the matching workers // - dispatch the message to all the matching workers
for _, w := range matchingWorkers { for _, w := range matchingWorkers {
debug.log("[master] sending msg to worker: %s\n", w.Name) debug.log("[master] sending msg to worker: %s\n", w.Name)
// send the message to the worker // send the message to the worker
w.MsgChan <- pushMsg w.MsgChan <- pushMsg
// increase the counter associated to the message // increase the counter associated to the message
master.WorkingMsg[pushMsg.Id]++ master.WorkingMsg[pushMsg.ID]++
debug.log("[master] now WorkingMsg[%s] is: %d\n", 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: case doneMsg := <-master.StatusChan:
// Here we manage a status message from a worker // Here we manage a status message from a worker
debug.log("[master] received message from StatusChan: %s\n", doneMsg) debug.log("[master] received message from StatusChan: %s\n", doneMsg)
if _, ok := master.WorkingMsg[doneMsg.Id]; ok && master.WorkingMsg[doneMsg.Id] > 0 { if _, ok := master.WorkingMsg[doneMsg.ID]; ok && master.WorkingMsg[doneMsg.ID] > 0 {
master.WorkingMsg[doneMsg.Id]-- master.WorkingMsg[doneMsg.ID]--
if master.WorkingMsg[doneMsg.Id] == 0 { if master.WorkingMsg[doneMsg.ID] == 0 {
delete(master.WorkingMsg, doneMsg.Id) delete(master.WorkingMsg, doneMsg.ID)
master.Spooler <- doneMsg master.Spooler <- doneMsg
} }
} else { } 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)
} }
} }
} }

@ -76,12 +76,12 @@ func startSpooler(master *SCORSHmaster) error {
watcher, err := fsnotify.NewWatcher() watcher, err := fsnotify.NewWatcher()
if err != nil { 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) err = watcher.Add(master.Spooldir)
if err != nil { 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) go spooler(watcher, master.Spooler)

@ -6,6 +6,7 @@ import (
"golang.org/x/crypto/openpgp" "golang.org/x/crypto/openpgp"
) )
// error constants
const ( const (
SCORSH_ERR_NO_FILE = -(1 << iota) SCORSH_ERR_NO_FILE = -(1 << iota)
SCORSH_ERR_KEYRING SCORSH_ERR_KEYRING
@ -14,10 +15,10 @@ const (
SCORSH_ERR_SIGNATURE 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 // sent to workers
type SCORSHmsg struct { type SCORSHmsg struct {
Id string `yaml:"m_id"` ID string `yaml:"m_id"`
Repo string `yaml:"m_repo"` Repo string `yaml:"m_repo"`
Branch string `yaml:"m_branch"` Branch string `yaml:"m_branch"`
OldRev string `yaml:"m_oldrev"` OldRev string `yaml:"m_oldrev"`
@ -25,18 +26,20 @@ type SCORSHmsg struct {
Path string Path string
} }
// SCORSHcmd represents commands configured on the server side
type SCORSHcmd struct { type SCORSHcmd struct {
URL string `yaml:"c_url"` URL string `yaml:"c_url"`
Hash string `yaml:"c_hash"` Hash string `yaml:"c_hash"`
} }
// SCORSHtagCfg represents tags configured on the server side
type SCORSHtagCfg struct { type SCORSHtagCfg struct {
Name string `yaml:"t_name"` Name string `yaml:"t_name"`
Keyrings []string `yaml:"t_keyrings"` Keyrings []string `yaml:"t_keyrings"`
Commands []SCORSHcmd `yaml:"t_commands"` Commands []SCORSHcmd `yaml:"t_commands"`
} }
// Configuration of a worker // SCORSHworkerCfg represents the static configuration of a worker
type SCORSHworkerCfg struct { type SCORSHworkerCfg struct {
Name string `yaml:"w_name"` Name string `yaml:"w_name"`
Repos []string `yaml:"w_repos"` Repos []string `yaml:"w_repos"`
@ -48,21 +51,20 @@ type SCORSHworkerCfg struct {
TagKeys map[string]map[string]bool TagKeys map[string]map[string]bool
} }
// State of a worker // SCORSHworkerState represents the runtime state of a worker
type SCORSHworkerState struct { type SCORSHworkerState struct {
Keys map[string]openpgp.KeyRing Keys map[string]openpgp.KeyRing
MsgChan chan SCORSHmsg MsgChan chan SCORSHmsg
StatusChan chan SCORSHmsg StatusChan chan SCORSHmsg
} }
// The type SCORSHworker represents the configuration and state of a // SCORSHworker represents the configuration and state of a worker
// worker
type SCORSHworker struct { type SCORSHworker struct {
SCORSHworkerCfg `yaml:",inline"` SCORSHworkerCfg `yaml:",inline"`
SCORSHworkerState SCORSHworkerState
} }
// Configuration of the master // SCORSHmasterCfg represents the static configuration of the master
type SCORSHmasterCfg struct { type SCORSHmasterCfg struct {
Spooldir string `yaml:"s_spooldir"` Spooldir string `yaml:"s_spooldir"`
Logfile string `yaml:"s_logfile"` Logfile string `yaml:"s_logfile"`
@ -70,7 +72,7 @@ type SCORSHmasterCfg struct {
Workers []SCORSHworker `yaml:"s_workers"` Workers []SCORSHworker `yaml:"s_workers"`
} }
// State of the master // SCORSHmasterState represents the runtime state of the master
type SCORSHmasterState struct { type SCORSHmasterState struct {
Spooler chan SCORSHmsg Spooler chan SCORSHmsg
StatusChan chan SCORSHmsg StatusChan chan SCORSHmsg
@ -78,20 +80,19 @@ type SCORSHmasterState struct {
WorkingMsg map[string]int WorkingMsg map[string]int
} }
// The type SCORSHmaster represents the configuration and state of the // SCORSHmaster represents the configuration and state of the master
// master
type SCORSHmaster struct { type SCORSHmaster struct {
SCORSHmasterCfg `yaml:",inline"` SCORSHmasterCfg `yaml:",inline"`
SCORSHmasterState SCORSHmasterState
} }
// client commands // SCORSHtag is the type of commands sent by clients
type SCORSHtag struct { type SCORSHtag struct {
Tag string `yaml:"s_tag"` Tag string `yaml:"s_tag"`
Args []string `yaml:"s_args"` Args []string `yaml:"s_args"`
} }
// SCORSHclientMsg is the list of commands sent by a client
type SCORSHclientMsg struct { type SCORSHclientMsg struct {
Tags []SCORSHtag `yaml:"scorsh"` Tags []SCORSHtag `yaml:"scorsh"`
} }
@ -117,7 +118,7 @@ func (cfg *SCORSHmaster) String() string {
func (msg *SCORSHmsg) String() string { func (msg *SCORSHmsg) String() string {
var buff bytes.Buffer 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, "Repo: %s\n", msg.Repo)
fmt.Fprintf(&buff, "Branch: %s\n", msg.Branch) fmt.Fprintf(&buff, "Branch: %s\n", msg.Branch)
fmt.Fprintf(&buff, "OldRev: %s\n", msg.OldRev) fmt.Fprintf(&buff, "OldRev: %s\n", msg.OldRev)

@ -11,23 +11,27 @@ import (
"strings" "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) parts := strings.SplitN(r, ":", 2)
repo_pattern := parts[0] repoPattern := parts[0]
branch_pattern := parts[1] branchPattern := parts[1]
repo_match, _ := regexp.MatchString(repo_pattern, repo) repoMatch, _ := regexp.MatchString(repoPattern, repo)
branch_match, _ := regexp.MatchString(branch_pattern, branch) branchMatch, _ := regexp.MatchString(branchPattern, branch)
debug.log("[worker.Matches] repo_match: %s\n", repo_match) debug.log("[worker.Matches] repo_match: %s\n", repoMatch)
debug.log("[worker.Matches] branch_match: %s\n", branch_match) debug.log("[worker.Matches] branch_match: %s\n", branchMatch)
if repo_match && branch_match { if repoMatch && branchMatch {
return true return true
} }
} }
return false return false
} }
// LoadKeyrings loads the configured keyrings for all the commands
// managed by the worker
func (w *SCORSHworker) LoadKeyrings() error { func (w *SCORSHworker) LoadKeyrings() error {
w.Keys = make(map[string]openpgp.KeyRing) w.Keys = make(map[string]openpgp.KeyRing)
@ -43,39 +47,39 @@ func (w *SCORSHworker) LoadKeyrings() error {
w.TagKeys[t.Name][keyring] = true w.TagKeys[t.Name][keyring] = true
continue continue
} }
k_file := fmt.Sprintf("%s/%s", w.Folder, keyring) kfile := fmt.Sprintf("%s/%s", w.Folder, keyring)
debug.log("[worker: %s] Trying to open keyring at %s\n", w.Name, k_file) debug.log("[worker: %s] Trying to open keyring at %s\n", w.Name, kfile)
f, err_file := os.Open(k_file) f, errFile := os.Open(kfile)
if err_file != nil { if errFile != nil {
log.Printf("[worker] cannot open keyring: %s", err_file) log.Printf("[worker] cannot open keyring: %s", errFile)
f.Close() _ = f.Close()
} }
// load the keyring // load the keyring
kr, err_key := openpgp.ReadArmoredKeyRing(f) kr, errKey := openpgp.ReadArmoredKeyRing(f)
if err_key != nil { if errKey != nil {
log.Printf("[worker] cannot load keyring: %s", err_key) log.Printf("[worker] cannot load keyring: %s", errKey)
f.Close() _ = f.Close()
//return fmt.Errorf("Unable to load keyring: ", err_key) //return fmt.Errorf("Unable to load keyring: ", err_key)
} }
w.Keys[keyring] = kr w.Keys[keyring] = kr
w.TagKeys[t.Name][keyring] = true w.TagKeys[t.Name][keyring] = true
f.Close() _ = f.Close()
} }
} }
return nil return nil
} }
// Still to be implemented // LoadTags loads all the configured commands for the worker
func (w *SCORSHworker) LoadTags() error { func (w *SCORSHworker) LoadTags() error {
w_tags, err := ioutil.ReadFile(w.Tagfile) wTags, err := ioutil.ReadFile(w.Tagfile)
if err != nil { if err != nil {
return fmt.Errorf("Cannot read worker config: %s", err) 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) //err = yaml.Unmarshal(w_tags, tags)
if err != nil { if err != nil {
@ -85,7 +89,7 @@ func (w *SCORSHworker) LoadTags() error {
return nil return nil
} }
// FIXME--- still needs some work... //
func runWorker(w *SCORSHworker) { func runWorker(w *SCORSHworker) {
var msg SCORSHmsg var msg SCORSHmsg
@ -99,7 +103,7 @@ func runWorker(w *SCORSHworker) {
for { for {
select { select {
case msg = <-w.MsgChan: 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 // process message
err := walkCommits(msg, w) err := walkCommits(msg, w)
if err != nil { if err != nil {
@ -115,13 +119,13 @@ func runWorker(w *SCORSHworker) {
// configuration and fills in the SCORSHmaster struct // configuration and fills in the SCORSHmaster struct
func startWorkers(master *SCORSHmaster) error { func startWorkers(master *SCORSHmaster) error {
num_workers := len(master.Workers) numWorkers := len(master.Workers)
// We should now start each worker // 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]) worker := &(master.Workers[w])
// Set the Status and Msg channels // Set the Status and Msg channels
@ -132,19 +136,19 @@ func startWorkers(master *SCORSHmaster) error {
err := worker.LoadTags() err := worker.LoadTags()
if err != nil { if err != nil {
close(worker.MsgChan) 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!!!! // Load worker keyrings -- this must be called *after* LoadTags!!!!
err = worker.LoadKeyrings() err = worker.LoadKeyrings()
if err != nil { if err != nil {
close(worker.MsgChan) 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 // Add the repos definitions to the map master.Repos
for _, repo_name := range worker.Repos { for _, repoName := range worker.Repos {
master.Repos[repo_name] = append(master.Repos[repo_name], worker) master.Repos[repoName] = append(master.Repos[repoName], worker)
} }
go runWorker(worker) go runWorker(worker)
<-master.StatusChan <-master.StatusChan

Loading…
Cancel
Save