diff --git a/.gitignore b/.gitignore index 32196a2..3e10ef6 100644 --- a/.gitignore +++ b/.gitignore @@ -43,4 +43,5 @@ tags [._]*.un~ cv.pdf +cv.json spruce diff --git a/create.go b/create.go new file mode 100644 index 0000000..45a7305 --- /dev/null +++ b/create.go @@ -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 +} diff --git a/generate.go b/generate.go index bdd17d9..e63dc2a 100644 --- a/generate.go +++ b/generate.go @@ -63,7 +63,7 @@ func (g *GenerateCommand) Run() error { // tex generates the CV document as a Tex file. func tex(input, tempDir string, historyLimit time.Time) (string, error) { - c, err := cv.NewCV(input) + c, err := cv.NewCVFromFile(input) if err != nil { return "", fmt.Errorf("unable to create a new CV value from %s; %w", input, err) } diff --git a/internal/cv/cv.go b/internal/cv/cv.go index 92db668..ee574a9 100644 --- a/internal/cv/cv.go +++ b/internal/cv/cv.go @@ -6,12 +6,13 @@ import ( "os" ) -// NewCV returns a new CV value from the given JSON file. -func NewCV(path string) (CV, error) { +// 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) @@ -23,3 +24,13 @@ func NewCV(path string) (CV, error) { return c, nil } + +func NewCV(firstName, lastName, jobTitle string) CV { + cv := CV{ + FirstName: firstName, + LastName: lastName, + JobTitle: jobTitle, + } + + return cv +} diff --git a/magefiles/mage.go b/magefiles/mage.go index bed0ff7..7178ebc 100644 --- a/magefiles/mage.go +++ b/magefiles/mage.go @@ -45,7 +45,7 @@ func Install() error { // Clean cleans the workspace func Clean() error { - files := []string{binary, "cv.pdf"} + files := []string{binary, "cv.pdf", "cv.json"} for i := range files { if err := sh.Rm(files[i]); err != nil { diff --git a/main.go b/main.go index 4822b51..e1c2e98 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,7 @@ func main() { commandMap := map[string]Runner{ "version": NewVersionCommand(), "generate": NewGenerateCommand(), + "create": NewCreateCommand(), } subcommand := os.Args[1]