Skip to content

Commit 99f9cf3

Browse files
authored
Merge pull request #31 from kyoshidajp/refactor_package_name
Refactor package names and structures
2 parents fd8476a + 9693270 commit 99f9cf3

28 files changed

+442
-117
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33
.env
44
.envrc
55

6+
cover.html
7+
cover.out
8+
69
dep-doctor

README.md

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
# dep-doctor
22

3-
`dep-doctor` is a tool to diagnose whether your software dependency packages are maintained.
3+
`dep-doctor` is a tool to diagnose whether your software dependency libraries are maintained.
44

5-
Today, most software relies heavily on external packages. Vulnerabilities in those packages can be detected by vulnerability scanners ([dependabot](https://docs.github.com/en/code-security/dependabot), [trivy](https://aquasecurity.github.io/trivy), [Grype](https://github.com/anchore/grype), etc) if they are publicly available.
5+
Today, most software relies heavily on external libraries. Vulnerabilities in those libraries can be detected by vulnerability scanners ([dependabot](https://docs.github.com/en/code-security/dependabot), [trivy](https://aquasecurity.github.io/trivy), [Grype](https://github.com/anchore/grype), etc) if they are publicly available.
66

7-
However, some packages have archived their source code repositories or have had their development stopped, although not explicitly. `dep-doctor` will notify you of those packages in the dependencies files.
7+
However, some libraries have archived their source code repositories or have had their development stopped, although not explicitly. `dep-doctor` will notify you of those libraries in the dependencies file.
88

99
![overview](doc/images/dep-doctor_overview.png "dep-doctor overview")
1010

1111
## Support dependencies files
1212

13-
| language | package manager | file (e.g.) | status |
13+
| language | package manager | dependencies file (e.g.) | status |
1414
| -------- | ------------- | -- | :----: |
1515
| Go | golang | go.mod | :heavy_check_mark: |
1616
| JavaScript | npm | package-lock.json | :heavy_check_mark: |
@@ -49,11 +49,12 @@ Usage:
4949
dep-doctor diagnose [flags]
5050

5151
Flags:
52-
-h, --help help for diagnose
53-
-i, --ignores string ignore dependencies (separated by a space)
54-
-f, --lock_file string lock file path (default "Gemfile.lock")
55-
-p, --package string package manager (default "bundler")
56-
-y, --year int max years of inactivity (default 5)
52+
-f, --file string dependencies file path (default "Gemfile.lock")
53+
-h, --help help for diagnose
54+
-i, --ignores string ignore dependencies (separated by a space)
55+
-p, --package string package manager (default "bundler")
56+
-y, --year int max years of inactivity (default 5)
57+
5758
```
5859

5960
For example:
@@ -80,7 +81,7 @@ Diagnosis completed! 6 dependencies.
8081
| *warn* | Source code repository is not active or unknown. |
8182
| *info* | Other reasons. (specified to be ignored) | |
8283

83-
## How works
84+
## How it works
8485

8586
![how_works](doc/images/how_works.png "dep-doctor how works")
8687

cmd/cocoapods_test.go

-50
This file was deleted.

cmd/diagnose.go

+60-33
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ import (
1515
"github.com/aquasecurity/go-dep-parser/pkg/types"
1616
"github.com/fatih/color"
1717
"github.com/kyoshidajp/dep-doctor/cmd/github"
18+
"github.com/kyoshidajp/dep-doctor/cmd/golang"
19+
"github.com/kyoshidajp/dep-doctor/cmd/nodejs"
20+
"github.com/kyoshidajp/dep-doctor/cmd/php"
21+
"github.com/kyoshidajp/dep-doctor/cmd/python"
22+
"github.com/kyoshidajp/dep-doctor/cmd/ruby"
23+
"github.com/kyoshidajp/dep-doctor/cmd/rust"
24+
"github.com/kyoshidajp/dep-doctor/cmd/swift"
1825
"github.com/spf13/cobra"
1926
"golang.org/x/exp/slices"
2027
)
@@ -42,24 +49,24 @@ func (d *Diagnosis) ErrorMessage() string {
4249
return fmt.Sprintf("%s", d.Error)
4350
}
4451

45-
type MedicalTechnician interface {
52+
type Doctor interface {
4653
Libraries(r parser_io.ReadSeekerAt) []types.Library
4754
SourceCodeURL(lib types.Library) (string, error)
4855
}
4956

5057
type RepositoryParams []github.FetchRepositoryParam
5158

52-
func (p RepositoryParams) CanSearchParams() []github.FetchRepositoryParam {
59+
func (p RepositoryParams) SearchableParams() []github.FetchRepositoryParam {
5360
params := []github.FetchRepositoryParam{}
5461
for _, param := range p {
55-
if param.CanSearch {
62+
if param.Searchable {
5663
params = append(params, param)
5764
}
5865
}
5966
return params
6067
}
6168

62-
func FetchRepositoryParams(libs []types.Library, g MedicalTechnician) RepositoryParams {
69+
func FetchRepositoryParams(libs []types.Library, d Doctor) RepositoryParams {
6370
var params []github.FetchRepositoryParam
6471
var wg sync.WaitGroup
6572
sem := make(chan struct{}, FETCH_REPOS_PER_ONCE)
@@ -73,12 +80,12 @@ func FetchRepositoryParams(libs []types.Library, g MedicalTechnician) Repository
7380

7481
fmt.Printf("%s\n", lib.Name)
7582

76-
url, err := g.SourceCodeURL(lib)
83+
url, err := d.SourceCodeURL(lib)
7784
if err != nil {
7885
params = append(params,
7986
github.FetchRepositoryParam{
8087
PackageName: lib.Name,
81-
CanSearch: false,
88+
Searchable: false,
8289
Error: err,
8390
},
8491
)
@@ -90,7 +97,7 @@ func FetchRepositoryParams(libs []types.Library, g MedicalTechnician) Repository
9097
params = append(params,
9198
github.FetchRepositoryParam{
9299
PackageName: lib.Name,
93-
CanSearch: false,
100+
Searchable: false,
94101
Error: err,
95102
},
96103
)
@@ -102,7 +109,7 @@ func FetchRepositoryParams(libs []types.Library, g MedicalTechnician) Repository
102109
Repo: repo.Repo,
103110
Owner: repo.Owner,
104111
PackageName: lib.Name,
105-
CanSearch: true,
112+
Searchable: true,
106113
},
107114
)
108115
}(lib)
@@ -113,20 +120,20 @@ func FetchRepositoryParams(libs []types.Library, g MedicalTechnician) Repository
113120
return params
114121
}
115122

116-
func Diagnose(d MedicalTechnician, r io.ReadSeekCloserAt, year int, ignores []string) map[string]Diagnosis {
123+
func Diagnose(d Doctor, r io.ReadSeekCloserAt, year int, ignores []string) map[string]Diagnosis {
117124
diagnoses := make(map[string]Diagnosis)
118125
slicedParams := [][]github.FetchRepositoryParam{}
119126
libs := d.Libraries(r)
120127
fetchRepositoryParams := FetchRepositoryParams(libs, d)
121-
canSearchRepositoryParams := fetchRepositoryParams.CanSearchParams()
122-
sliceSize := len(canSearchRepositoryParams)
128+
searchableRepositoryParams := fetchRepositoryParams.SearchableParams()
129+
sliceSize := len(searchableRepositoryParams)
123130

124131
for i := 0; i < sliceSize; i += github.SEARCH_REPOS_PER_ONCE {
125132
end := i + github.SEARCH_REPOS_PER_ONCE
126133
if sliceSize < end {
127134
end = sliceSize
128135
}
129-
slicedParams = append(slicedParams, canSearchRepositoryParams[i:end])
136+
slicedParams = append(slicedParams, searchableRepositoryParams[i:end])
130137
}
131138

132139
var wg sync.WaitGroup
@@ -157,7 +164,7 @@ func Diagnose(d MedicalTechnician, r io.ReadSeekCloserAt, year int, ignores []st
157164
wg.Wait()
158165

159166
for _, fetchRepositoryParam := range fetchRepositoryParams {
160-
if fetchRepositoryParam.CanSearch {
167+
if fetchRepositoryParam.Searchable {
161168
continue
162169
}
163170

@@ -173,7 +180,7 @@ func Diagnose(d MedicalTechnician, r io.ReadSeekCloserAt, year int, ignores []st
173180

174181
type Options struct {
175182
packageManager string
176-
lockFilePath string
183+
filePath string
177184
ignores string
178185
year int
179186
}
@@ -186,15 +193,32 @@ var (
186193
o = &Options{}
187194
)
188195

189-
var doctors = map[string]MedicalTechnician{
190-
"bundler": NewBundlerDoctor(),
191-
"yarn": NewYarnDoctor(),
192-
"pip": NewPipDoctor(),
193-
"npm": NewNPMDoctor(),
194-
"composer": NewComposerDoctor(),
195-
"golang": NewGolangDoctor(),
196-
"cargo": NewCargoDoctor(),
197-
"cocoapods": NewCococaPodsDoctor(),
196+
type Doctors map[string]Doctor
197+
198+
func (d Doctors) PackageManagers() []string {
199+
packages := []string{}
200+
for p := range d {
201+
packages = append(packages, p)
202+
}
203+
sort.Strings(packages)
204+
return packages
205+
}
206+
207+
func (d Doctors) UnknownErrorMessage(packageManager string) string {
208+
return fmt.Sprintf("Unknown package manager: %s. You can choose from [%s]",
209+
packageManager,
210+
strings.Join(d.PackageManagers(), ", "))
211+
}
212+
213+
var doctors = Doctors{
214+
"bundler": ruby.NewBundlerDoctor(),
215+
"yarn": nodejs.NewYarnDoctor(),
216+
"pip": python.NewPipDoctor(),
217+
"npm": nodejs.NewNPMDoctor(),
218+
"composer": php.NewComposerDoctor(),
219+
"golang": golang.NewGolangDoctor(),
220+
"cargo": rust.NewCargoDoctor(),
221+
"cocoapods": swift.NewCococaPodsDoctor(),
198222
}
199223

200224
var diagnoseCmd = &cobra.Command{
@@ -203,21 +227,17 @@ var diagnoseCmd = &cobra.Command{
203227
Run: func(cmd *cobra.Command, args []string) {
204228
doctor, ok := doctors[o.packageManager]
205229
if !ok {
206-
packages := []string{}
207-
for p := range doctors {
208-
packages = append(packages, p)
209-
}
210-
m := fmt.Sprintf("Unknown package manager: %s. You can choose from [%s]", o.packageManager, strings.Join(packages, ", "))
230+
m := doctors.UnknownErrorMessage(o.packageManager)
211231
log.Fatal(m)
212232
}
213233

214-
lockFilePath := o.lockFilePath
215-
f, err := os.Open(lockFilePath)
234+
filePath := o.filePath
235+
f, err := os.Open(filePath)
216236
defer func() {
217237
_ = f.Close()
218238
}()
219239
if err != nil {
220-
m := fmt.Sprintf("Can't open: %s.", o.lockFilePath)
240+
m := fmt.Sprintf("Can't open: %s.", o.filePath)
221241
log.Fatal(m)
222242
}
223243

@@ -230,10 +250,17 @@ var diagnoseCmd = &cobra.Command{
230250

231251
func init() {
232252
rootCmd.AddCommand(diagnoseCmd)
233-
diagnoseCmd.Flags().StringVarP(&o.packageManager, "package", "p", "bundler", "package manager")
234-
diagnoseCmd.Flags().StringVarP(&o.lockFilePath, "lock_file", "f", "Gemfile.lock", "lock file path")
253+
diagnoseCmd.Flags().StringVarP(&o.packageManager, "package", "p", "", "package manager")
254+
diagnoseCmd.Flags().StringVarP(&o.filePath, "file", "f", "", "dependencies file path")
235255
diagnoseCmd.Flags().StringVarP(&o.ignores, "ignores", "i", "", "ignore dependencies (separated by a space)")
236256
diagnoseCmd.Flags().IntVarP(&o.year, "year", "y", MAX_YEAR_TO_BE_BLANK, "max years of inactivity")
257+
258+
if err := diagnoseCmd.MarkFlagRequired("package"); err != nil {
259+
fmt.Println(err.Error())
260+
}
261+
if err := diagnoseCmd.MarkFlagRequired("file"); err != nil {
262+
fmt.Println(err.Error())
263+
}
237264
}
238265

239266
func Report(diagnoses map[string]Diagnosis) error {

0 commit comments

Comments
 (0)