Skip to content

Commit ae86bd6

Browse files
authored
Merge pull request #15 from kyoshidajp/add_ignore_option
Add an ignore option
2 parents d3abcfa + 7d5d72c commit ae86bd6

File tree

6 files changed

+40
-11
lines changed

6 files changed

+40
-11
lines changed

cmd/bundler.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
parser_io "github.com/aquasecurity/go-dep-parser/pkg/io"
77
"github.com/aquasecurity/go-dep-parser/pkg/ruby/bundler"
88
"github.com/kyoshidajp/dep-doctor/cmd/github"
9+
"golang.org/x/exp/slices"
910
)
1011

1112
type BundlerDoctor struct {
@@ -15,7 +16,7 @@ func NewBundlerDoctor() *BundlerDoctor {
1516
return &BundlerDoctor{}
1617
}
1718

18-
func (b *BundlerDoctor) Diagnose(r parser_io.ReadSeekerAt, year int) map[string]Diagnosis {
19+
func (b *BundlerDoctor) Diagnose(r parser_io.ReadSeekerAt, year int, ignores []string) map[string]Diagnosis {
1920
diagnoses := make(map[string]Diagnosis)
2021
slicedNameWithOwners := [][]github.NameWithOwner{}
2122
nameWithOwners := b.NameWithOwners(r)
@@ -32,10 +33,12 @@ func (b *BundlerDoctor) Diagnose(r parser_io.ReadSeekerAt, year int) map[string]
3233
for _, nameWithOwners := range slicedNameWithOwners {
3334
repos := github.FetchFromGitHub(nameWithOwners)
3435
for _, r := range repos {
36+
isIgnore := slices.Contains(ignores, r.Name)
3537
diagnosis := Diagnosis{
3638
Name: r.Name,
3739
Url: r.Url,
3840
Archived: r.Archived,
41+
Ignored: isIgnore,
3942
Diagnosed: true,
4043
IsActive: r.IsActive(year),
4144
}

cmd/diagnose.go

+25-6
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ import (
1818
const MAX_YEAR_TO_BE_BLANK = 5
1919

2020
type Doctor interface {
21-
Diagnose(r io.ReadSeekerAt, year int) map[string]Diagnosis
21+
Diagnose(r io.ReadSeekerAt, year int, ignores []string) map[string]Diagnosis
2222
NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithOwner
2323
}
2424

2525
type Diagnosis struct {
2626
Name string
2727
Url string
2828
Archived bool
29+
Ignored bool
2930
Diagnosed bool
3031
IsActive bool
3132
}
@@ -40,13 +41,18 @@ func NewDepartment(d Doctor) *Department {
4041
}
4142
}
4243

43-
func (d *Department) Diagnose(r io.ReadSeekCloserAt, year int) map[string]Diagnosis {
44-
return d.doctor.Diagnose(r, year)
44+
func (d *Department) Diagnose(r io.ReadSeekCloserAt, year int, ignores []string) map[string]Diagnosis {
45+
return d.doctor.Diagnose(r, year, ignores)
4546
}
4647

4748
type Options struct {
4849
packageManager string
4950
lockFilePath string
51+
ignores string
52+
}
53+
54+
func (o *Options) Ignores() []string {
55+
return strings.Split(o.ignores, " ")
5056
}
5157

5258
var (
@@ -85,7 +91,7 @@ var diagnoseCmd = &cobra.Command{
8591
}
8692

8793
department := NewDepartment(doctor)
88-
diagnoses := department.Diagnose(f, MAX_YEAR_TO_BE_BLANK)
94+
diagnoses := department.Diagnose(f, MAX_YEAR_TO_BE_BLANK, o.Ignores())
8995
if err := Report(diagnoses); err != nil {
9096
os.Exit(1)
9197
}
@@ -96,14 +102,23 @@ func init() {
96102
rootCmd.AddCommand(diagnoseCmd)
97103
diagnoseCmd.Flags().StringVarP(&o.packageManager, "package", "p", "bundler", "package manager")
98104
diagnoseCmd.Flags().StringVarP(&o.lockFilePath, "lock_file", "f", "Gemfile.lock", "lock file path")
105+
diagnoseCmd.Flags().StringVarP(&o.ignores, "ignores", "i", "", "ignore dependencies")
99106
}
100107

101108
func Report(diagnoses map[string]Diagnosis) error {
102109
errMessages := []string{}
103110
warnMessages := []string{}
111+
ignoredMessages := []string{}
104112
errCount := 0
105113
unDiagnosedCount := 0
114+
ignoredCount := 0
106115
for _, diagnosis := range diagnoses {
116+
if diagnosis.Ignored {
117+
ignoredMessages = append(ignoredMessages, fmt.Sprintf("[info] %s (ignored):", diagnosis.Name))
118+
ignoredCount += 1
119+
continue
120+
}
121+
107122
if !diagnosis.Diagnosed {
108123
warnMessages = append(warnMessages, fmt.Sprintf("[warn] %s (unknown):", diagnosis.Name))
109124
unDiagnosedCount += 1
@@ -120,6 +135,9 @@ func Report(diagnoses map[string]Diagnosis) error {
120135
}
121136

122137
fmt.Printf("\n")
138+
if len(ignoredMessages) > 0 {
139+
fmt.Println(strings.Join(ignoredMessages, "\n"))
140+
}
123141
if len(warnMessages) > 0 {
124142
color.Yellow(strings.Join(warnMessages, "\n"))
125143
}
@@ -129,10 +147,11 @@ func Report(diagnoses map[string]Diagnosis) error {
129147

130148
color.Green(heredoc.Docf(`
131149
Diagnose complete! %d dependencies.
132-
%d error, %d unknown`,
150+
%d error, %d unknown, %d ignored`,
133151
len(diagnoses),
134152
errCount,
135-
unDiagnosedCount),
153+
unDiagnosedCount,
154+
ignoredCount),
136155
)
137156

138157
if len(errMessages) > 0 {

cmd/diagnose_test.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,47 @@ func TestDiagnose(t *testing.T) {
1414
Name: "faker",
1515
Url: "https://github.com/faker-ruby/faker",
1616
Archived: false,
17+
Ignored: false,
1718
Diagnosed: true,
1819
IsActive: true,
1920
},
2021
"concurrent-ruby": {
2122
Name: "concurrent-ruby",
2223
Url: "https://github.com/ruby-concurrency/concurrent-ruby",
2324
Archived: false,
25+
Ignored: false,
2426
Diagnosed: true,
2527
IsActive: true,
2628
},
2729
"i18n": {
2830
Name: "i18n",
2931
Url: "https://github.com/ruby-i18n/i18n",
3032
Archived: false,
33+
Ignored: true,
3134
Diagnosed: true,
3235
IsActive: true,
3336
},
3437
"method_source": {
3538
Name: "method_source",
3639
Url: "https://github.com/banister/method_source",
3740
Archived: false,
41+
Ignored: false,
3842
Diagnosed: true,
3943
IsActive: true,
4044
},
4145
"paperclip": {
4246
Name: "paperclip",
4347
Url: "https://github.com/thoughtbot/paperclip",
4448
Archived: true,
49+
Ignored: false,
4550
Diagnosed: true,
4651
IsActive: false,
4752
},
4853
"dotenv": {
4954
Name: "dotenv",
5055
Url: "https://github.com/bkeepers/dotenv",
5156
Archived: false,
57+
Ignored: false,
5258
Diagnosed: true,
5359
IsActive: true,
5460
},
@@ -60,7 +66,8 @@ func TestDiagnose(t *testing.T) {
6066
defer f.Close()
6167

6268
doctor := NewDepartment(NewBundlerDoctor())
63-
diagnoses := doctor.Diagnose(f, 2)
69+
ignores := []string{"i18n"}
70+
diagnoses := doctor.Diagnose(f, 2, ignores)
6471
assert.Equal(t, expect, diagnoses)
6572
})
6673
}

cmd/npm.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func NewNPMDoctor() *NPMDoctor {
1515
return &NPMDoctor{}
1616
}
1717

18-
func (d *NPMDoctor) Diagnose(r parser_io.ReadSeekerAt, year int) map[string]Diagnosis {
18+
func (d *NPMDoctor) Diagnose(r parser_io.ReadSeekerAt, year int, ignores []string) map[string]Diagnosis {
1919
diagnoses := make(map[string]Diagnosis)
2020
slicedNameWithOwners := [][]github.NameWithOwner{}
2121
nameWithOwners := d.NameWithOwners(r)

cmd/pip.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func NewPipDoctor() *PipDoctor {
1515
return &PipDoctor{}
1616
}
1717

18-
func (d *PipDoctor) Diagnose(r parser_io.ReadSeekerAt, year int) map[string]Diagnosis {
18+
func (d *PipDoctor) Diagnose(r parser_io.ReadSeekerAt, year int, ignores []string) map[string]Diagnosis {
1919
diagnoses := make(map[string]Diagnosis)
2020
slicedNameWithOwners := [][]github.NameWithOwner{}
2121
nameWithOwners := d.NameWithOwners(r)

cmd/yarn.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func NewYarnDoctor() *YarnDoctor {
1515
return &YarnDoctor{}
1616
}
1717

18-
func (d *YarnDoctor) Diagnose(r parser_io.ReadSeekerAt, year int) map[string]Diagnosis {
18+
func (d *YarnDoctor) Diagnose(r parser_io.ReadSeekerAt, year int, ignores []string) map[string]Diagnosis {
1919
diagnoses := make(map[string]Diagnosis)
2020
slicedNameWithOwners := [][]github.NameWithOwner{}
2121
nameWithOwners := d.NameWithOwners(r)

0 commit comments

Comments
 (0)