Merge branch 'create-subcommand'

This commit is contained in:
Dan Anglin 2023-08-12 09:51:35 +01:00
commit 54d5fa1831
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
6 changed files with 139 additions and 4 deletions

1
.gitignore vendored
View file

@ -43,4 +43,5 @@ tags
[._]*.un~ [._]*.un~
cv.pdf cv.pdf
cv.json
spruce spruce

122
create.go Normal file
View file

@ -0,0 +1,122 @@
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"time"
"codeflow.dananglin.me.uk/apollo/spruce/internal/cv"
)
type CreateCommand struct {
*flag.FlagSet
firstName string
lastName string
jobTitle string
filename string
}
func NewCreateCommand() *CreateCommand {
cc := CreateCommand{
FlagSet: flag.NewFlagSet("create", flag.ExitOnError),
}
cc.StringVar(&cc.firstName, "first-name", "", "specify your first name")
cc.StringVar(&cc.lastName, "last-name", "", "specify your last name")
cc.StringVar(&cc.jobTitle, "job-title", "", "specify your preferred job title")
cc.StringVar(&cc.filename, "filename", "cv.json", "specify the filename of the created JSON document")
return &cc
}
func (c *CreateCommand) Run() error {
data := cv.NewCV(c.firstName, c.lastName, c.jobTitle)
data.Contact = map[string]string{
"Email": "",
"Phone": "",
}
data.Links = map[string]string{
"GitHub": "",
"Website": "",
}
data.Summary = make([]string, 2)
data.Skills = []cv.Skills{
{
Category: "",
Values: make([]string, 1),
},
{
Category: "",
Values: make([]string, 1),
},
}
data.Employment = []cv.Experience{
{
Company: "",
Location: "",
JobTitle: "",
Duration: cv.Duration{
Start: cv.Date{
Year: time.Now().Year(),
Month: int(time.Now().Month()),
Day: time.Now().Day(),
},
End: cv.Date{
Year: time.Now().Year(),
Month: int(time.Now().Month()),
Day: time.Now().Day(),
},
Present: false,
},
Details: make([]string, 2),
},
}
data.Education = []cv.Experience{
{
School: "",
Location: "",
Qualification: "",
Duration: cv.Duration{
Start: cv.Date{
Year: time.Now().Year(),
Month: int(time.Now().Month()),
Day: time.Now().Day(),
},
End: cv.Date{
Year: time.Now().Year(),
Month: int(time.Now().Month()),
Day: time.Now().Day(),
},
},
},
}
data.Interests = make([]string, 2)
file, err := os.Create(c.filename)
if err != nil {
return fmt.Errorf("unable to open %s; %w", c.filename, err)
}
defer file.Close()
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
if err := encoder.Encode(data); err != nil {
return fmt.Errorf("unable to write the data to %s; %w", c.filename, err)
}
log.Printf("INFO: Successfully written the new CV to %s\n", file.Name())
return nil
}

View file

@ -63,7 +63,7 @@ func (g *GenerateCommand) Run() error {
// tex generates the CV document as a Tex file. // tex generates the CV document as a Tex file.
func tex(input, tempDir string, historyLimit time.Time) (string, error) { func tex(input, tempDir string, historyLimit time.Time) (string, error) {
c, err := cv.NewCV(input) c, err := cv.NewCVFromFile(input)
if err != nil { if err != nil {
return "", fmt.Errorf("unable to create a new CV value from %s; %w", input, err) return "", fmt.Errorf("unable to create a new CV value from %s; %w", input, err)
} }

View file

@ -6,12 +6,13 @@ import (
"os" "os"
) )
// NewCV returns a new CV value from the given JSON file. // NewCVFromFile returns a new CV value from the given JSON file.
func NewCV(path string) (CV, error) { func NewCVFromFile(path string) (CV, error) {
file, err := os.Open(path) file, err := os.Open(path)
if err != nil { if err != nil {
return CV{}, fmt.Errorf("unable to open %s; %w", path, err) return CV{}, fmt.Errorf("unable to open %s; %w", path, err)
} }
defer file.Close()
decoder := json.NewDecoder(file) decoder := json.NewDecoder(file)
@ -23,3 +24,13 @@ func NewCV(path string) (CV, error) {
return c, nil return c, nil
} }
func NewCV(firstName, lastName, jobTitle string) CV {
cv := CV{
FirstName: firstName,
LastName: lastName,
JobTitle: jobTitle,
}
return cv
}

View file

@ -45,7 +45,7 @@ func Install() error {
// Clean cleans the workspace // Clean cleans the workspace
func Clean() error { func Clean() error {
files := []string{binary, "cv.pdf"} files := []string{binary, "cv.pdf", "cv.json"}
for i := range files { for i := range files {
if err := sh.Rm(files[i]); err != nil { if err := sh.Rm(files[i]); err != nil {

View file

@ -19,6 +19,7 @@ func main() {
commandMap := map[string]Runner{ commandMap := map[string]Runner{
"version": NewVersionCommand(), "version": NewVersionCommand(),
"generate": NewGenerateCommand(), "generate": NewGenerateCommand(),
"create": NewCreateCommand(),
} }
subcommand := os.Args[1] subcommand := os.Args[1]