Dan Anglin
36acb1a324
- Add a new subcommand and FlagSet for creating new CV JSON files. - fix: close the file after reading the CV.
36 lines
625 B
Go
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
|
|
}
|