Skip to content

Commit

Permalink
Merge pull request #15 from kyoshidajp/add_ignore_option
Browse files Browse the repository at this point in the history
Add an ignore option
  • Loading branch information
kyoshidajp authored Oct 27, 2023
2 parents d3abcfa + 7d5d72c commit ae86bd6
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 11 deletions.
5 changes: 4 additions & 1 deletion cmd/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
parser_io "github.com/aquasecurity/go-dep-parser/pkg/io"
"github.com/aquasecurity/go-dep-parser/pkg/ruby/bundler"
"github.com/kyoshidajp/dep-doctor/cmd/github"
"golang.org/x/exp/slices"
)

type BundlerDoctor struct {
Expand All @@ -15,7 +16,7 @@ func NewBundlerDoctor() *BundlerDoctor {
return &BundlerDoctor{}
}

func (b *BundlerDoctor) Diagnose(r parser_io.ReadSeekerAt, year int) map[string]Diagnosis {
func (b *BundlerDoctor) Diagnose(r parser_io.ReadSeekerAt, year int, ignores []string) map[string]Diagnosis {
diagnoses := make(map[string]Diagnosis)
slicedNameWithOwners := [][]github.NameWithOwner{}
nameWithOwners := b.NameWithOwners(r)
Expand All @@ -32,10 +33,12 @@ func (b *BundlerDoctor) Diagnose(r parser_io.ReadSeekerAt, year int) map[string]
for _, nameWithOwners := range slicedNameWithOwners {
repos := github.FetchFromGitHub(nameWithOwners)
for _, r := range repos {
isIgnore := slices.Contains(ignores, r.Name)
diagnosis := Diagnosis{
Name: r.Name,
Url: r.Url,
Archived: r.Archived,
Ignored: isIgnore,
Diagnosed: true,
IsActive: r.IsActive(year),
}
Expand Down
31 changes: 25 additions & 6 deletions cmd/diagnose.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ import (
const MAX_YEAR_TO_BE_BLANK = 5

type Doctor interface {
Diagnose(r io.ReadSeekerAt, year int) map[string]Diagnosis
Diagnose(r io.ReadSeekerAt, year int, ignores []string) map[string]Diagnosis
NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithOwner
}

type Diagnosis struct {
Name string
Url string
Archived bool
Ignored bool
Diagnosed bool
IsActive bool
}
Expand All @@ -40,13 +41,18 @@ func NewDepartment(d Doctor) *Department {
}
}

func (d *Department) Diagnose(r io.ReadSeekCloserAt, year int) map[string]Diagnosis {
return d.doctor.Diagnose(r, year)
func (d *Department) Diagnose(r io.ReadSeekCloserAt, year int, ignores []string) map[string]Diagnosis {
return d.doctor.Diagnose(r, year, ignores)
}

type Options struct {
packageManager string
lockFilePath string
ignores string
}

func (o *Options) Ignores() []string {
return strings.Split(o.ignores, " ")
}

var (
Expand Down Expand Up @@ -85,7 +91,7 @@ var diagnoseCmd = &cobra.Command{
}

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

func Report(diagnoses map[string]Diagnosis) error {
errMessages := []string{}
warnMessages := []string{}
ignoredMessages := []string{}
errCount := 0
unDiagnosedCount := 0
ignoredCount := 0
for _, diagnosis := range diagnoses {
if diagnosis.Ignored {
ignoredMessages = append(ignoredMessages, fmt.Sprintf("[info] %s (ignored):", diagnosis.Name))
ignoredCount += 1
continue
}

if !diagnosis.Diagnosed {
warnMessages = append(warnMessages, fmt.Sprintf("[warn] %s (unknown):", diagnosis.Name))
unDiagnosedCount += 1
Expand All @@ -120,6 +135,9 @@ func Report(diagnoses map[string]Diagnosis) error {
}

fmt.Printf("\n")
if len(ignoredMessages) > 0 {
fmt.Println(strings.Join(ignoredMessages, "\n"))
}
if len(warnMessages) > 0 {
color.Yellow(strings.Join(warnMessages, "\n"))
}
Expand All @@ -129,10 +147,11 @@ func Report(diagnoses map[string]Diagnosis) error {

color.Green(heredoc.Docf(`
Diagnose complete! %d dependencies.
%d error, %d unknown`,
%d error, %d unknown, %d ignored`,
len(diagnoses),
errCount,
unDiagnosedCount),
unDiagnosedCount,
ignoredCount),
)

if len(errMessages) > 0 {
Expand Down
9 changes: 8 additions & 1 deletion cmd/diagnose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,47 @@ func TestDiagnose(t *testing.T) {
Name: "faker",
Url: "https://github.com/faker-ruby/faker",
Archived: false,
Ignored: false,
Diagnosed: true,
IsActive: true,
},
"concurrent-ruby": {
Name: "concurrent-ruby",
Url: "https://github.com/ruby-concurrency/concurrent-ruby",
Archived: false,
Ignored: false,
Diagnosed: true,
IsActive: true,
},
"i18n": {
Name: "i18n",
Url: "https://github.com/ruby-i18n/i18n",
Archived: false,
Ignored: true,
Diagnosed: true,
IsActive: true,
},
"method_source": {
Name: "method_source",
Url: "https://github.com/banister/method_source",
Archived: false,
Ignored: false,
Diagnosed: true,
IsActive: true,
},
"paperclip": {
Name: "paperclip",
Url: "https://github.com/thoughtbot/paperclip",
Archived: true,
Ignored: false,
Diagnosed: true,
IsActive: false,
},
"dotenv": {
Name: "dotenv",
Url: "https://github.com/bkeepers/dotenv",
Archived: false,
Ignored: false,
Diagnosed: true,
IsActive: true,
},
Expand All @@ -60,7 +66,8 @@ func TestDiagnose(t *testing.T) {
defer f.Close()

doctor := NewDepartment(NewBundlerDoctor())
diagnoses := doctor.Diagnose(f, 2)
ignores := []string{"i18n"}
diagnoses := doctor.Diagnose(f, 2, ignores)
assert.Equal(t, expect, diagnoses)
})
}
2 changes: 1 addition & 1 deletion cmd/npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewNPMDoctor() *NPMDoctor {
return &NPMDoctor{}
}

func (d *NPMDoctor) Diagnose(r parser_io.ReadSeekerAt, year int) map[string]Diagnosis {
func (d *NPMDoctor) Diagnose(r parser_io.ReadSeekerAt, year int, ignores []string) map[string]Diagnosis {
diagnoses := make(map[string]Diagnosis)
slicedNameWithOwners := [][]github.NameWithOwner{}
nameWithOwners := d.NameWithOwners(r)
Expand Down
2 changes: 1 addition & 1 deletion cmd/pip.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewPipDoctor() *PipDoctor {
return &PipDoctor{}
}

func (d *PipDoctor) Diagnose(r parser_io.ReadSeekerAt, year int) map[string]Diagnosis {
func (d *PipDoctor) Diagnose(r parser_io.ReadSeekerAt, year int, ignores []string) map[string]Diagnosis {
diagnoses := make(map[string]Diagnosis)
slicedNameWithOwners := [][]github.NameWithOwner{}
nameWithOwners := d.NameWithOwners(r)
Expand Down
2 changes: 1 addition & 1 deletion cmd/yarn.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewYarnDoctor() *YarnDoctor {
return &YarnDoctor{}
}

func (d *YarnDoctor) Diagnose(r parser_io.ReadSeekerAt, year int) map[string]Diagnosis {
func (d *YarnDoctor) Diagnose(r parser_io.ReadSeekerAt, year int, ignores []string) map[string]Diagnosis {
diagnoses := make(map[string]Diagnosis)
slicedNameWithOwners := [][]github.NameWithOwner{}
nameWithOwners := d.NameWithOwners(r)
Expand Down

0 comments on commit ae86bd6

Please sign in to comment.