Skip to content

Commit af833a7

Browse files
Do: Return tuple ([]UnsatisfiedRule, error) (#10)
* Use `log.Fatalln` * `Do`: Return tuple `([]UnsatisfiedRule, error)` * Update main.go * Add `UnsatisfiedRules.String()` * `[]UnsatisfiedRule` to `UnsatisfiedRules`
1 parent 41e4fed commit af833a7

File tree

2 files changed

+49
-24
lines changed

2 files changed

+49
-24
lines changed

cli/main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,14 @@ func action(ctx *cli.Context) error {
7676
exclude := ctx.StringSlice("exclude")
7777
extMapPath := ctx.String("ext_map")
7878

79-
if err := difflint.Do(ctx.App.Reader, include, exclude, extMapPath); err != nil {
79+
unsatisfiedRules, err := difflint.Do(ctx.App.Reader, include, exclude, extMapPath)
80+
if err != nil {
8081
return err
8182
}
8283

84+
if len(unsatisfiedRules) > 0 {
85+
return cli.Exit(unsatisfiedRules.String(), 1)
86+
}
87+
8388
return nil
8489
}

difflint.go

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package difflint
22

33
import (
4+
"fmt"
45
"io"
5-
"log"
66
"path/filepath"
77
"strings"
88

@@ -87,10 +87,39 @@ type UnsatisfiedRule struct {
8787
UnsatisfiedTargets map[int]struct{}
8888
}
8989

90+
type UnsatisfiedRules []UnsatisfiedRule
91+
92+
// String returns a string representation of the unsatisfied rules.
93+
func (r *UnsatisfiedRules) String() string {
94+
var b strings.Builder
95+
for _, rule := range *r {
96+
b.WriteString("rule (")
97+
b.WriteString(rule.Rule.Hunk.File)
98+
b.WriteString(":")
99+
b.WriteString(fmt.Sprintf("%d", rule.Rule.Hunk.Range.Start))
100+
b.WriteString(",")
101+
b.WriteString(rule.Rule.Hunk.File)
102+
b.WriteString(":")
103+
b.WriteString(fmt.Sprintf("%d", rule.Rule.Hunk.Range.End))
104+
b.WriteString(") not satisfied for targets:\n")
105+
for i, target := range rule.Targets {
106+
if _, ok := rule.UnsatisfiedTargets[i]; !ok {
107+
continue
108+
}
109+
110+
key := TargetKey(rule.Rule.Hunk.File, target)
111+
b.WriteString(" ")
112+
b.WriteString(key)
113+
b.WriteString("\n")
114+
}
115+
}
116+
return b.String()
117+
}
118+
90119
// Result of a linting operation.
91120
type LintResult struct {
92121
// List of rules that were not satisfied.
93-
UnsatisfiedRules []UnsatisfiedRule
122+
UnsatisfiedRules UnsatisfiedRules
94123
}
95124

96125
// Lint lints the given hunks against the given rules and returns the result.
@@ -147,8 +176,8 @@ func isRelativeToCurrentDirectory(path string) bool {
147176
}
148177

149178
// Check returns the list of unsatisfied rules for the given map of rules.
150-
func Check(rulesMap map[string][]Rule) ([]UnsatisfiedRule, error) {
151-
var unsatisfiedRules []UnsatisfiedRule
179+
func Check(rulesMap map[string][]Rule) (UnsatisfiedRules, error) {
180+
var unsatisfiedRules UnsatisfiedRules
152181
for pathnameA, rulesA := range rulesMap {
153182
outer:
154183
for i, ruleA := range rulesA {
@@ -193,7 +222,7 @@ func Check(rulesMap map[string][]Rule) ([]UnsatisfiedRule, error) {
193222
}
194223

195224
// Entrypoint for the difflint command.
196-
func Do(r io.Reader, include, exclude []string, extMapPath string) error {
225+
func Do(r io.Reader, include, exclude []string, extMapPath string) (UnsatisfiedRules, error) {
197226
// Parse options.
198227
extMap := NewExtMap(extMapPath)
199228

@@ -207,38 +236,29 @@ func Do(r io.Reader, include, exclude []string, extMapPath string) error {
207236
FileExtMap: extMap.FileExtMap,
208237
})
209238
if err != nil {
210-
return errors.Wrap(err, "failed to lint hunks")
239+
return nil, errors.Wrap(err, "failed to lint hunks")
211240
}
212241

213242
// If there are no unsatisfied rules, return nil.
214243
if len(result.UnsatisfiedRules) == 0 {
215-
return nil
244+
return nil, nil
216245
}
217246

218247
// Print the unsatisfied rules.
248+
var included bool
219249
for _, rule := range result.UnsatisfiedRules {
220250
// Skip if the rule is not intended to be included in the output.
221-
if ok, err := Include(rule.Hunk.File, include, exclude); err != nil {
222-
return errors.Wrap(err, "failed to check if file is included")
223-
} else if !ok {
224-
continue
251+
included, err = Include(rule.Hunk.File, include, exclude)
252+
if err != nil {
253+
return nil, errors.Wrap(err, "failed to check if file is included")
225254
}
226255

227-
// Print the unsatisfied rule.
228-
log.Printf("Rule (%s:%d,%s:%d) unsatisfied", rule.Rule.Hunk.File, rule.Rule.Hunk.Range.Start, rule.Rule.Hunk.File, rule.Rule.Hunk.Range.End)
229-
230-
// Print the unsatisfied target keys.
231-
for i, target := range rule.Targets {
232-
if _, ok := rule.UnsatisfiedTargets[i]; !ok {
233-
continue
234-
}
235-
236-
key := TargetKey(rule.Hunk.File, target)
237-
log.Printf(" %s", key)
256+
if !included {
257+
continue
238258
}
239259
}
240260

241-
return nil
261+
return result.UnsatisfiedRules, nil
242262
}
243263

244264
// ParseHunks parses the input diff and returns the extracted file paths along

0 commit comments

Comments
 (0)