spruce/internal/cv/cv.go
Dan Anglin 36acb1a324
feat: add create subcommand and FlagSet
- Add a new subcommand and FlagSet for creating new CV JSON files.
- fix: close the file after reading the CV.
2023-08-12 09:43:45 +01:00

36 lines
625 B
Go

package cv
import (
"encoding/json"
"fmt"
"os"
)
// 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 c CV
if err = decoder.Decode(&c); err != nil {
return CV{}, fmt.Errorf("unable to decode JSON data; %w", err)
}
return c, nil
}
func NewCV(firstName, lastName, jobTitle string) CV {
cv := CV{
FirstName: firstName,
LastName: lastName,
JobTitle: jobTitle,
}
return cv
}