Skip to content

Commit

Permalink
add more error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
zeucapua committed Sep 9, 2024
1 parent 545974e commit 583c49a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
42 changes: 31 additions & 11 deletions cmd/generate/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,20 @@ func run(opts *Options) error {
// Open repo
repo, err := git.PlainOpen(opts.path)
if err != nil {
return fmt.Errorf("error opening repo: %w", err)
return fmt.Errorf("Error opening repo: %w", err)
}

commitIter, err := repo.CommitObjects()

if err != nil {
return fmt.Errorf("Error opening repo commits: %w", err)
}

var uniqueEmails []string
commitIter.ForEach(func(c *object.Commit) error {
err = commitIter.ForEach(func(c *object.Commit) error {
name := c.Author.Name
email := c.Author.Email

// TODO: edge case- same email multiple names
// eg: '[email protected]' = 'zeudev' & 'Zeu Capua'

if !opts.isInteractive {
doesEmailExist := slices.Contains(attributionMap[name], email)
if !doesEmailExist {
Expand All @@ -111,18 +112,31 @@ func run(opts *Options) error {
return nil
})

// TODO: INTERACTIVE: per unique email, set a name (existing or new or ignore)
if err != nil {
return fmt.Errorf("Error iterating over repo commits: %w", err)
}

// INTERACTIVE: per unique email, set a name (existing or new or ignore)
if opts.isInteractive {
program := tea.NewProgram(initialModel(opts, uniqueEmails))
if _, err := program.Run(); err != nil {
return fmt.Errorf(err.Error())
return fmt.Errorf("Error running interactive mode: %w", err)
}
} else {
// generate an output file
// default: `./.sauced.yaml`
// fallback for home directories
if opts.outputPath == "~/" {
homeDir, _ := os.UserHomeDir()
generateOutputFile(filepath.Join(homeDir, ".sauced.yaml"), attributionMap)
err := generateOutputFile(filepath.Join(homeDir, ".sauced.yaml"), attributionMap)
if err != nil {
return fmt.Errorf("Error generating output file: %w", err)
}
} else {
generateOutputFile(filepath.Join(opts.outputPath, ".sauced.yaml"), attributionMap)
err := generateOutputFile(filepath.Join(opts.outputPath, ".sauced.yaml"), attributionMap)
if err != nil {
return fmt.Errorf("Error generating output file: %w", err)
}
}
}

Expand Down Expand Up @@ -242,9 +256,15 @@ func runOutputGeneration(opts *Options, attributionMap map[string][]string) tea.
return func() tea.Msg {
if opts.outputPath == "~/" {
homeDir, _ := os.UserHomeDir()
generateOutputFile(filepath.Join(homeDir, ".sauced.yaml"), attributionMap)
err := generateOutputFile(filepath.Join(homeDir, ".sauced.yaml"), attributionMap)
if err != nil {
return fmt.Errorf("Error generating output file: %w", err)
}
} else {
generateOutputFile(filepath.Join(opts.outputPath, ".sauced.yaml"), attributionMap)
err := generateOutputFile(filepath.Join(opts.outputPath, ".sauced.yaml"), attributionMap)
if err != nil {
return fmt.Errorf("Error generating output file: %w", err)
}
}

return tea.Quit()
Expand Down
10 changes: 7 additions & 3 deletions cmd/generate/config/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func generateOutputFile(outputPath string, attributionMap map[string][]string) error {
file, err := os.Create(outputPath)
if err != nil {
return fmt.Errorf("error creating %s file: %w", outputPath, err)
return fmt.Errorf("Error creating %s file: %w", outputPath, err)
}
defer file.Close()

Expand All @@ -22,10 +22,14 @@ func generateOutputFile(outputPath string, attributionMap map[string][]string) e
yaml, err := utils.OutputYAML(config)

if err != nil {
return fmt.Errorf("Failed to turn into YAML")
return fmt.Errorf("Failed to turn into YAML: %w", err)
}

file.WriteString(yaml)
_, err = file.WriteString(yaml)

if err != nil {
return fmt.Errorf("Failed to turn into YAML: %w", err)
}

return nil
}

0 comments on commit 583c49a

Please sign in to comment.