spruce/internal/cv/utils.go
Dan Anglin 71d62ecaf6
refactor: add golangci-lint with code refactoring
Add golangci-lint for linting and refactor the code based on the
feedback from running it.

Changes:

- Add configuration for golangci-lint.
- Break the large function in create.go into smaller ones.
- Rename internal/templateFuncs to internal/templatefuncs to remove
  upper case letters in the package name.
- Add a mage target for lint tests.
2023-08-21 03:07:06 +01:00

63 lines
1.4 KiB
Go

package cv
import (
"encoding/json"
"fmt"
"os"
"time"
)
// NewCVFromFile returns a new CV value from the given JSON file.
func NewCVFromFile(path string) (CV, error) {
file, err := os.Open(path)
if err != nil {
return CV{}, fmt.Errorf("unable to open %s; %w", path, err)
}
defer file.Close()
decoder := json.NewDecoder(file)
var output CV
if err = decoder.Decode(&output); err != nil {
return CV{}, fmt.Errorf("unable to decode JSON data; %w", err)
}
return output, nil
}
// NewCV returns a new value of type CV.
func NewCV(firstName, lastName, jobTitle string) CV {
output := CV{
FirstName: firstName,
LastName: lastName,
JobTitle: jobTitle,
}
return output
}
// After returns true if the Duration's end date is set after the earliest experience date.
// An error is returned if the end date is not parsed successfully.
func (d Duration) After(earliestExperienceDate time.Time) (bool, error) {
endDate, err := d.End.Parse()
if err != nil {
return false, err
}
return endDate.After(earliestExperienceDate), nil
}
// Parse parses Date and returns a value of type time.Time.
// An error is returned if the parsing fails.
func (d Date) Parse() (time.Time, error) {
dateStr := fmt.Sprintf("%d-%02d-%02d", d.Year, d.Month, d.Day)
date, err := time.Parse(time.DateOnly, dateStr)
if err != nil {
return time.Time{}, fmt.Errorf("unable to parse the date; %w", err)
}
return date, nil
}