generated from skanehira/go-cli-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from kyoshidajp/support_npm
Support npm
- Loading branch information
Showing
6 changed files
with
353 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
parser_io "github.com/aquasecurity/go-dep-parser/pkg/io" | ||
"github.com/aquasecurity/go-dep-parser/pkg/nodejs/npm" | ||
"github.com/kyoshidajp/dep-doctor/cmd/github" | ||
) | ||
|
||
type NPMDoctor struct { | ||
} | ||
|
||
func NewNPMDoctor() *NPMDoctor { | ||
return &NPMDoctor{} | ||
} | ||
|
||
func (d *NPMDoctor) Diagnose(r parser_io.ReadSeekerAt, year int) map[string]Diagnosis { | ||
diagnoses := make(map[string]Diagnosis) | ||
slicedNameWithOwners := [][]github.NameWithOwner{} | ||
nameWithOwners := d.NameWithOwners(r) | ||
sliceSize := len(nameWithOwners) | ||
|
||
for i := 0; i < sliceSize; i += github.SEARCH_REPOS_PER_ONCE { | ||
end := i + github.SEARCH_REPOS_PER_ONCE | ||
if sliceSize < end { | ||
end = sliceSize | ||
} | ||
slicedNameWithOwners = append(slicedNameWithOwners, nameWithOwners[i:end]) | ||
} | ||
|
||
for _, nameWithOwners := range slicedNameWithOwners { | ||
repos := github.FetchFromGitHub(nameWithOwners) | ||
for _, r := range repos { | ||
diagnosis := Diagnosis{ | ||
Name: r.Name, | ||
Url: r.Url, | ||
Archived: r.Archived, | ||
Diagnosed: true, | ||
IsActive: r.IsActive(year), | ||
} | ||
diagnoses[r.Name] = diagnosis | ||
} | ||
} | ||
|
||
for _, nameWithOwner := range nameWithOwners { | ||
if nameWithOwner.CanSearch { | ||
continue | ||
} | ||
|
||
diagnosis := Diagnosis{ | ||
Name: nameWithOwner.PackageName, | ||
Diagnosed: false, | ||
} | ||
diagnoses[nameWithOwner.PackageName] = diagnosis | ||
} | ||
return diagnoses | ||
} | ||
|
||
func (d *NPMDoctor) NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithOwner { | ||
var nameWithOwners []github.NameWithOwner | ||
libs, _, _ := npm.NewParser().Parse(r) | ||
|
||
nodejs := Nodejs{} | ||
for _, lib := range libs { | ||
fmt.Printf("%s\n", lib.Name) | ||
|
||
githubUrl, err := nodejs.fetchURLFromRegistry(lib.Name) | ||
if err != nil { | ||
nameWithOwners = append(nameWithOwners, | ||
github.NameWithOwner{ | ||
PackageName: lib.Name, | ||
CanSearch: false, | ||
}, | ||
) | ||
continue | ||
} | ||
|
||
repo, err := github.ParseGitHubUrl(githubUrl) | ||
if err != nil { | ||
nameWithOwners = append(nameWithOwners, | ||
github.NameWithOwner{ | ||
PackageName: lib.Name, | ||
CanSearch: false, | ||
}, | ||
) | ||
continue | ||
} | ||
|
||
nameWithOwners = append(nameWithOwners, | ||
github.NameWithOwner{ | ||
Repo: repo.Repo, | ||
Owner: repo.Owner, | ||
PackageName: lib.Name, | ||
CanSearch: true, | ||
}, | ||
) | ||
} | ||
|
||
return nameWithOwners | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.