Skip to content

Commit

Permalink
Merge pull request #12 from kyoshidajp/validations
Browse files Browse the repository at this point in the history
Add option validations
  • Loading branch information
kyoshidajp authored Oct 26, 2023
2 parents 859426c + d4564b6 commit 24b98b5
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions cmd/diagnose.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func (d *Department) Diagnose(r io.ReadSeekCloserAt, year int) map[string]Diagno
}

type Options struct {
packageManagerName string
lockFilePath string
packageManager string
lockFilePath string
}

var (
Expand All @@ -64,34 +64,45 @@ var diagnoseCmd = &cobra.Command{
Use: "diagnose",
Short: "Diagnose dependencies",
Run: func(cmd *cobra.Command, args []string) {
lockFilePath := o.lockFilePath
f, _ := os.Open(lockFilePath)
defer f.Close()

doctor, ok := doctors[o.packageManagerName]
doctor, ok := doctors[o.packageManager]
if !ok {
log.Fatal("unknown package manager")
packages := []string{}
for p := range doctors {
packages = append(packages, p)
}
m := fmt.Sprintf("Unknown package manager: %s. You can choose from [%s]", o.packageManager, strings.Join(packages, ", "))
log.Fatal(m)
}

lockFilePath := o.lockFilePath
f, err := os.Open(lockFilePath)
defer func() {
_ = f.Close()
}()
if err != nil {
fmt.Fprintln(os.Stderr, err)
m := fmt.Sprintf("Can't open: %s.", o.lockFilePath)
log.Fatal(m)
}

department := NewDepartment(doctor)
diagnoses := department.Diagnose(f, MAX_YEAR_TO_BE_BLANK)
err := Report(diagnoses)
if err != nil {
if err := Report(diagnoses); err != nil {
os.Exit(1)
}
},
}

func init() {
rootCmd.AddCommand(diagnoseCmd)
diagnoseCmd.Flags().StringVarP(&o.packageManagerName, "package", "p", "bundler", "package manager")
diagnoseCmd.Flags().StringVarP(&o.packageManager, "package", "p", "bundler", "package manager")
diagnoseCmd.Flags().StringVarP(&o.lockFilePath, "lock_file", "f", "Gemfile.lock", "lock file path")
}

func Report(diagnoses map[string]Diagnosis) error {
errMessages := []string{}
warnMessages := []string{}
errorCount := 0
errCount := 0
unDiagnosedCount := 0
for _, diagnosis := range diagnoses {
if !diagnosis.Diagnosed {
Expand All @@ -101,11 +112,11 @@ func Report(diagnoses map[string]Diagnosis) error {
}
if diagnosis.Archived {
errMessages = append(errMessages, fmt.Sprintf("[error] %s (archived): %s", diagnosis.Name, diagnosis.Url))
errorCount += 1
errCount += 1
}
if !diagnosis.IsActive {
errMessages = append(errMessages, fmt.Sprintf("[error] %s (not-maintained): %s", diagnosis.Name, diagnosis.Url))
errorCount += 1
errCount += 1
}
}

Expand All @@ -121,7 +132,7 @@ func Report(diagnoses map[string]Diagnosis) error {
Diagnose complete! %d dependencies.
%d error, %d unknown`,
len(diagnoses),
errorCount,
errCount,
unDiagnosedCount),
)

Expand Down

0 comments on commit 24b98b5

Please sign in to comment.