spruce/internal/cmd/create.go
Dan Anglin c53978cd91
feat: add a field for location type, code refactor
- feat: add a field for the type of work location (e.g. hybrid)
- refactor: move the Tex and PDF generating code to a new internal
  package which also moves the templates there as well.
- fix: add a default value for the --output field for the generate
  command.
- fix: add an error for when the user does not specify an input file
  when generating the PDF.
- fix: the package name for each of the files in the templateFuncs
  package.
2023-08-15 17:25:00 +01:00

127 lines
2.5 KiB
Go

package cmd
import (
"encoding/json"
"flag"
"fmt"
"log/slog"
"os"
"time"
"codeflow.dananglin.me.uk/apollo/spruce/internal/cv"
)
type CreateCommand struct {
*flag.FlagSet
summary string
firstName string
lastName string
jobTitle string
filename string
}
func NewCreateCommand() *CreateCommand {
cc := CreateCommand{
FlagSet: flag.NewFlagSet("create", flag.ExitOnError),
summary: "Create a new CV JSON file.",
}
cc.StringVar(&cc.filename, "filepath", "cv.json", "specify the path where the CV JSON document should be created.")
cc.StringVar(&cc.firstName, "first-name", "", "specify your first name.")
cc.StringVar(&cc.jobTitle, "job-title", "", "specify your current job title.")
cc.StringVar(&cc.lastName, "last-name", "", "specify your last name.")
cc.Usage = usageFunc(cc.Name(), cc.summary, cc.FlagSet)
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: "",
LocationType: "",
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)
}
slog.Info("CV successfully created", "filename", file.Name())
return nil
}