Skip to content

Commit

Permalink
add comments for all methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed Nov 12, 2015
1 parent 03fb267 commit 62e7e1f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions command.go
Expand Up @@ -12,6 +12,7 @@ type Command struct {
Bin string
}

// Exec runs the specified command and returns its output and exit code
func (c *Command) Exec(cmds ...string) (string, int) {
cmd := exec.Command(c.Bin, cmds...)

Expand Down
16 changes: 12 additions & 4 deletions hound.go
Expand Up @@ -18,29 +18,35 @@ type Hound struct {
Skips []string `yaml:"skip"`
}

func (h *Hound) New() (error, bool) {
// New innitializes a new Hound instance by parsing regexp patterns from a
// local configuration file to prepare for diff sniffing
func (h *Hound) New() (bool, error) {
config, err := h.LoadConfig()
if err != nil {
return err, false
return false, err
}

err = h.Parse(config)
if err != nil {
return err, false
return false, err
}

return nil, true
return true, nil
}

// LoadConfig loads a local configuration file of regexp patterns
func (h *Hound) LoadConfig() ([]byte, error) {
filename, _ := filepath.Abs(h.Config)
return ioutil.ReadFile(filename)
}

// Parse parses a configuration byte array
func (h *Hound) Parse(data []byte) error {
return yaml.Unmarshal(data, h)
}

// Sniff matches the passed git-diff hunk against all regexp patterns that
// were parsed from the local configuration
func (h *Hound) Sniff(fileName string, hunk *diff.Hunk) error {
r1, _ := regexp.Compile(`^\w+\/`)
fileName = r1.ReplaceAllString(fileName, "")
Expand Down Expand Up @@ -70,6 +76,7 @@ func (h *Hound) Sniff(fileName string, hunk *diff.Hunk) error {
return nil
}

// Match matches a byte array against a regexp pattern
func (h *Hound) Match(pattern string, subject []byte) (bool, error) {
r, err := regexp.Compile(pattern)
if err != nil {
Expand All @@ -83,6 +90,7 @@ func (h *Hound) Match(pattern string, subject []byte) (bool, error) {
return false, nil
}

// MatchPatterns matches a byte array against an array of regexp patterns
func (h *Hound) MatchPatterns(patterns []string, subject []byte) (string, bool) {
for _, pattern := range patterns {
if match, _ := h.Match(pattern, subject); match {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Expand Up @@ -22,7 +22,7 @@ func main() {
hound := &Hound{Config: *config}
git := &Command{Bin: *bin}

if _, ok := hound.New(); ok {
if ok, _ := hound.New(); ok {
out, _ := git.Exec("diff", "-U0", "--staged")
fileDiffs, err := diff.ParseMultiFileDiff([]byte(out))
if err != nil {
Expand Down

0 comments on commit 62e7e1f

Please sign in to comment.