Skip to content

Commit

Permalink
Merge pull request #10 from kyoshidajp/refactor_strategy_methods
Browse files Browse the repository at this point in the history
Refactor names
  • Loading branch information
kyoshidajp authored Oct 26, 2023
2 parents bec2bf4 + a06d857 commit 5b1f322
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 172 deletions.
58 changes: 29 additions & 29 deletions cmd/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,17 @@ type GemResponse struct {
HomepageUri string `json:"homepage_uri"`
}

func FetchFromRubyGems(name string) (string, error) {
url := fmt.Sprintf(RUBYGEMS_ORG_API, name)
req, _ := http.NewRequest(http.MethodGet, url, nil)
client := new(http.Client)
resp, _ := client.Do(req)
body, _ := io.ReadAll(resp.Body)

var Gem GemResponse
err := json.Unmarshal(body, &Gem)
if err != nil {
return "", errors.New("error: Unknown response")
}

if Gem.SourceCodeUri != "" {
return Gem.SourceCodeUri, nil
} else if Gem.HomepageUri != "" {
return Gem.HomepageUri, nil
}

return "", nil
}

type BundlerStrategy struct {
type BundlerDoctor struct {
}

func NewBundlerStrategy() *BundlerStrategy {
return &BundlerStrategy{}
func NewBundlerDoctor() *BundlerDoctor {
return &BundlerDoctor{}
}

func (s *BundlerStrategy) Diagnose(r parser_io.ReadSeekerAt, year int) map[string]Diagnosis {
func (b *BundlerDoctor) Diagnose(r parser_io.ReadSeekerAt, year int) map[string]Diagnosis {
diagnoses := make(map[string]Diagnosis)
slicedNameWithOwners := [][]github.NameWithOwner{}
nameWithOwners := s.getNameWithOwners(r)
nameWithOwners := b.NameWithOwners(r)
sliceSize := len(nameWithOwners)

for i := 0; i < sliceSize; i += GITHUB_SEARCH_REPO_COUNT_PER_ONCE {
Expand Down Expand Up @@ -94,15 +72,37 @@ func (s *BundlerStrategy) Diagnose(r parser_io.ReadSeekerAt, year int) map[strin
return diagnoses
}

func (s *BundlerStrategy) getNameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithOwner {
func (d *BundlerDoctor) fetchURLFromRepository(name string) (string, error) {
url := fmt.Sprintf(RUBYGEMS_ORG_API, name)
req, _ := http.NewRequest(http.MethodGet, url, nil)
client := new(http.Client)
resp, _ := client.Do(req)
body, _ := io.ReadAll(resp.Body)

var Gem GemResponse
err := json.Unmarshal(body, &Gem)
if err != nil {
return "", errors.New("error: Unknown response")
}

if Gem.SourceCodeUri != "" {
return Gem.SourceCodeUri, nil
} else if Gem.HomepageUri != "" {
return Gem.HomepageUri, nil
}

return "", nil
}

func (d *BundlerDoctor) NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithOwner {
var nameWithOwners []github.NameWithOwner
p := &bundler.Parser{}
libs, _, _ := p.Parse(r)

for _, lib := range libs {
fmt.Printf("%s\n", lib.Name)

githubUrl, err := FetchFromRubyGems(lib.Name)
githubUrl, err := d.fetchURLFromRepository(lib.Name)
if err != nil {
continue
}
Expand Down
46 changes: 46 additions & 0 deletions cmd/bundler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cmd

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestBundlerDoctor_fetchURLFromRepository(t *testing.T) {
tests := []struct {
name string
gem_name string
}{
{
name: "source_code_uri exists",
gem_name: "rails",
},
{
name: "no source_code_uri, but homepage_uri exists",
gem_name: "minitest",
},
}
expects := []struct {
name string
url string
}{
{
name: "source_code_uri exists",
url: "https://github.com/rails/rails",
},
{
name: "no source_code_uri, but homepage_uri exists",
url: "https://github.com/minitest/minitest",
},
}

for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := BundlerDoctor{}
r, _ := s.fetchURLFromRepository(tt.gem_name)
expect := expects[i]
assert.Equal(t, true, strings.HasPrefix(r, expect.url))
})
}
}
36 changes: 20 additions & 16 deletions cmd/diagnose.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ import (

"github.com/MakeNowJust/heredoc"
"github.com/aquasecurity/go-dep-parser/pkg/io"
parser_io "github.com/aquasecurity/go-dep-parser/pkg/io"
"github.com/fatih/color"
"github.com/kyoshidajp/dep-doctor/cmd/github"
"github.com/spf13/cobra"
)

const MAX_YEAR_TO_BE_BLANK = 5

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

type Diagnosis struct {
Expand All @@ -27,23 +31,18 @@ type Diagnosis struct {
IsActive bool
}

type Doctor struct {
strategy DiagnoseStrategy
type Department struct {
doctor Doctor
}

func NewDoctor(strategy DiagnoseStrategy) *Doctor {
return &Doctor{
strategy: strategy,
func NewDepartment(d Doctor) *Department {
return &Department{
doctor: d,
}
}

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

var DoctorWithStrategy = map[string]DiagnoseStrategy{
"bundler": NewBundlerStrategy(),
"yarn": NewYarnStrategy(),
func (d *Department) Diagnose(r io.ReadSeekCloserAt, year int) map[string]Diagnosis {
return d.doctor.Diagnose(r, year)
}

type Options struct {
Expand All @@ -55,6 +54,11 @@ var (
o = &Options{}
)

var doctors = map[string]Doctor{
"bundler": NewBundlerDoctor(),
"yarn": NewYarnDoctor(),
}

var diagnoseCmd = &cobra.Command{
Use: "diagnose",
Short: "Diagnose dependencies",
Expand All @@ -63,13 +67,13 @@ var diagnoseCmd = &cobra.Command{
f, _ := os.Open(lockFilePath)
defer f.Close()

packageManagerStrategy, ok := DoctorWithStrategy[o.packageManagerName]
doctor, ok := doctors[o.packageManagerName]
if !ok {
log.Fatal("unknown package manager")
}

doctor := NewDoctor(packageManagerStrategy)
diagnoses := doctor.Diagnose(f, MAX_YEAR_TO_BE_BLANK)
department := NewDepartment(doctor)
diagnoses := department.Diagnose(f, MAX_YEAR_TO_BE_BLANK)
err := Report(diagnoses)
if err != nil {
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion cmd/diagnose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestDiagnose(t *testing.T) {
require.NoError(t, err)
defer f.Close()

doctor := NewDoctor(NewBundlerStrategy())
doctor := NewDepartment(NewBundlerDoctor())
diagnoses := doctor.Diagnose(f, 2)
assert.Equal(t, expect, diagnoses)
})
Expand Down
94 changes: 0 additions & 94 deletions cmd/ruby/bundler/bundler.go

This file was deleted.

Loading

0 comments on commit 5b1f322

Please sign in to comment.